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

mark.a.brand mark.a.brand at gmail.com
Tue Mar 11 13:15:51 CET 2008


Hi Tim:

Thanks, that was what I was after, but maybe you are right a couple of extra
for loops is not looking too bad after all.

Cheers
Mark

On 11/03/2008, Tim Golden <mail at timgolden.me.uk> wrote:
>
> Tim Golden wrote:
> [... snip Elixir-based solution ...]
>
> I realise, rereading my post that it doesn't really solve
> your concern about multiple loops etc. I doubt it's really
> a problem, but in the spirit of the question, here's a raw
> sqlite solution, again Noddy and untested.
>
> You've said you're newish to Python and while
> there's no black magic here, I am being fairly free with
> generator expressions and getattr-fu. If you think the
> approach might be useful and need further explanation
> feel free to ask.
>
> <code>
> import os, sys
> import sqlite3
> import wmi
>
> os.remove ("c:/temp/wmi.db")
> db = sqlite3.connect ("c:/temp/wmi.db")
> classes_wanted = ["Win32_Process", "Win32_Service"]
>
> #
> # Generate a dictionary of class name -> WMI class
> #
> c = wmi.WMI ()
> wmi_classes = dict (
>    (class_wanted, getattr (c, class_wanted)) \
>      for class_wanted in classes_wanted
> )
>
> #
> # Create a simplistic Sqlite table for each WMI
> # class with one (untyped) column per attribute.
> #
> for class_name, wmi_class in wmi_classes.items ():
>    db.execute ("CREATE TABLE %s (%s)" % (
>      class_name,
>      ", ".join (p for p in wmi_class.properties)
>    ))
>
> db.commit ()
>
> #
> # For each WMI class, add all its instances as
> # rows to the corresponding sqlite table.
> #
> for class_name, wmi_class in wmi_classes.items ():
>    db.executemany (
>      "INSERT INTO %s (%s) VALUES (%s)" % (
>        class_name,
>        ", ".join (p for p in wmi_class.properties),
>        ", ".join ("?" for p in wmi_class.properties)
>      ),
>      [[getattr (c, p) for p in c.properties] for c in wmi_class ()]
>    )
>
> db.commit ()
>
> </code>
>
> _______________________________________________
> 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/d6b9eaef/attachment.htm 


More information about the python-win32 mailing list