[python-win32] advice on architecting or program design in python:
Tim Golden
mail at timgolden.me.uk
Tue Mar 11 12:11:45 CET 2008
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/
More information about the python-win32
mailing list