[python-win32] advice on architecting or program design in python:

mark.a.brand mark.a.brand at gmail.com
Tue Mar 11 13:08:54 CET 2008


Hi Tim:

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.

eg.

class Process()
   ...
class Product()
  ...
class Event()
  ...
class Service()
   ....

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)

  for product in c.Win32_Products (['ProductId', 'Caption']):
    d = dict ((p, getattr (process, p)) for p in product.properties)
    Product (**d)

  for event in c.Win32_NTEvent (['EventId', 'Caption']):
    d = dict ((p, getattr (event, p)) for p in event.properties)
    Event (**d)

  for service in c.Win32_Service (['ServiceId', 'Caption']):
    d = dict ((p, getattr (service, p)) for p in service.properties)
    Service (**d)


  ....
  etc etc.
  session.flush ()
  session.close ()

Thanks for your patience.

Cheers
Mark

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
> is
> > achievable ...
> >
> > so to build up these database tables, i have to have some queries
> happening
> > 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
> those
> > "for loops".
>
>
> Certainly possible at several levels. There are a number
> of ORM packages around, but the general favourite is probably
> sqlalchemy [1] and its spin-off Elixir [2]. That said, there's
> nothing to stop you working straight at the DBAPI [3] 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 ()
>
> </code>
>
> 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
>
> :)
>
> TJG
>
> [1] http://sqlalchemy.org
> [2] http://elixir.ematia.de/trac/wiki
> [3] http://www.python.org/dev/peps/pep-0249/
>
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20080311/1aa65e56/attachment.htm 


More information about the python-win32 mailing list