[python-win32] advice on architecting or program design in python:
mark.a.brand at gmail.com
Tue Mar 11 13:08:54 CET 2008
So what you are saying is that it would be perfectly acceptable to have a
"for loop" for each Win32_* query and that there is no good way to
generalise that behaviour.
if __name__ == '__main__':
c = wmi.WMI ()
for process in c.Win32_Process (['ProcessId', 'Caption']):
d = dict ((p, getattr (process, p)) for p in process.properties)
for product in c.Win32_Products (['ProductId', 'Caption']):
d = dict ((p, getattr (process, p)) for p in product.properties)
for event in c.Win32_NTEvent (['EventId', 'Caption']):
d = dict ((p, getattr (event, p)) for p in event.properties)
for service in c.Win32_Service (['ServiceId', 'Caption']):
d = dict ((p, getattr (service, p)) for p in service.properties)
Thanks for your patience.
On 11/03/2008, Tim Golden <mail at timgolden.me.uk> wrote:
> mark.a.brand wrote:
> > sorry - client as in customer
> Ah. I see. (Didn't think of that :)
> > i will try and explain better, but its not quite clear in my mind what
> > achievable ...
> > so to build up these database tables, i have to have some queries
> > of the form :
> > for list in Win32_Process()
> > add instance to table-process
> > for list in Win32_Service()
> > add instance to table-service
> > ...
> > etc etc for each Win32_* query i wanted to implement.
> > what I am looking for is an object based / pythonic way to replace all
> > "for loops".
> Certainly possible at several levels. There are a number
> of ORM packages around, but the general favourite is probably
> sqlalchemy  and its spin-off Elixir . That said, there's
> nothing to stop you working straight at the DBAPI  level
> and calling .executemany against the tables you want.
> Certainly you could have your table structure exactly mimic
> the "column" structure of the corresponding WMI class and
> push things along a bit that way. It's fairly trivial to
> turn the WMI attribute values into a dictionary which could
> then be passed on to something like Elixir's row-creation.
> <vague hand-wavy code>
> import os, sys
> from elixir import *
> import wmi
> DB_FILENAME = os.path.abspath ("wmi.db")
> metadata.bind = "sqlite:///%s" % DB_FILENAME
> class Process (Entity):
> ProcessId = Field (Integer, primary_key=True)
> Caption = Field (Unicode (100))
> if __name__ == '__main__':
> setup_all (True)
> c = wmi.WMI ()
> for process in c.Win32_Process (['ProcessId', 'Caption']):
> d = dict ((p, getattr (process, p)) for p in process.properties)
> Process (**d)
> session.flush ()
> session.close ()
> Obviously, this is a mere stub but it does at least work
> and might be a useful basis. There is definite scope for
> factoring things out but you might do better just to have
> a function which took -- or inferred -- the relevant class
> and table names. YMMV.
> > thanks for you assistance tim.
> Glad to be of service
>  http://sqlalchemy.org
>  http://elixir.ematia.de/trac/wiki
>  http://www.python.org/dev/peps/pep-0249/
> python-win32 mailing list
> python-win32 at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the python-win32