[python-win32] Getting multiple properties in one go
Roger Binns
rogerb at rogerbinns.com
Wed Nov 24 07:22:15 CET 2004
I have some code that is reading Outlook contacts, but it is very slow.
I need to get every field/property of each contact. I get the list
of known keys using:
keys=[]
for key in contact._prop_map_get_:
# work out if it is a property or a method (last field is None for properties)
if contact._prop_map_get_[key][-1] is None:
keys.append(key)
Then for each contact, I do this:
record={}
for key in keys:
v=getattr(contact, key)
if v not in (None, "", "\x00\x00"):
if isinstance(v, pywintypes.TimeType): # convert from com time
try:
v=int(v)
except ValueError:
# illegal time value
continue
record[key]=v
According to hotshot, the vast majority of my runtime is consumed by the
_ApplyTypes_ function in the pythoncom wrappers. This is invoked by the
getattr above. And most of the time the vast majority of the fields are
empty.
Is there a better way of getting all the properties without getting them
one by one?
Ideally I would like a C level function that I can give the desired list
to, and have it return a dict, discarding those that are NULL or empty
strings.
There are typically 35 properties per record, and so reading 1,000 records
involves 35,000 calls through the getattr stuff above. On my Athlon XP 2800
that takes around 70 seconds.
Roger
More information about the Python-win32
mailing list