On Dec 18, 2008, at 3:04 PM, Andreas Jung wrote:

> On 10.12.2008 20:36 Uhr, Michael Bayer wrote:
>>
>> On Dec 10, 2008, at 2:27 PM, Andreas Jung wrote:
>>
>>> Hi there,
>>>
>>> is there some more efficient way for dictifying a resultset other  
>>> than
>>>
>>> lst = list()
>>> for row in session.query(...).all():
>>>
>>>     d = self.__dict__.copy()
>>>     for k in d.keys():
>>>         if k.startswith('_sa'):
>>>             del d[k]
>>>
>>>     lst.append(d)
>>>
>>> Especially the loop of the keys takes pretty long for large result
>>> sets
>>> and tables with lots of columns.
>>
>> the most efficient way would be to use a ResultProxy instead of the
>> ORM and to call dict() on each row.    Otherwise a more succinct way
>> of what you're doing there, not necessarily much faster, might be
>>
>>
>> list = [
>>      dict((k, v) for k, v in obj.__dict__.iteritems() if not
>> k.startswith('_sa'))
>>      for obj in session.query.all()
>>
>
> Does SA contain some official API to introspect the list of defined
> synonyms for a particular class? The goal is to take values defined  
> as a
> synonym also into account for the dictification (for backward
> compatiblity reasons for an existing codebase).

the mapper's get_property() method includes a "resolve_synonyms"  
keyword arg that indicates a given key which points to a synonym  
should return the actual referenced property, so a recipe that builds  
upon this would look like:

set([mapper.get_property(p.key, resolve_synonyms=True) for p in  
mapper.iterate_properties])





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to