[sqlalchemy] Re: result as the dict()
MyBase = type(MyBase, (Base, MyMixin), {}) Is this any different than just doing class MyBase(Base, MyMixin): pass ? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
On Jul 6, 2008, at 1:16 AM, Empty wrote: You might want to look into the new instrument_declarative function in 0.5. It allows you to wrap a class with the declarative functionality without using the metaclass. This should permit you to work with a subclass, although I haven't tried it. it also accepts cls as a keyword argument which is used as the base class: Base = declarative_base(..., cls=MyMixin) you can also add a mixin to any class in Python like: Base = declarative_base(...) MyBase = type(MyBase, (Base, MyMixin), {}) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
it also accepts cls as a keyword argument which is used as the base class: Base = declarative_base(..., cls=MyMixin) you can also add a mixin to any class in Python like: Base = declarative_base(...) MyBase = type(MyBase, (Base, MyMixin), {}) Thanks! Both are good to know and better than mixing each time I create a class. I didn't know about the second use of type()... that could be very useful. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
Spoke a bit to soon... looks like that cls argument is only available in 0.5, and the type() mixin trick still complains about not having a mapped_table specifed. SQLA 0.4.6 is unhappy with the snippet below... import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base class _BaseORMMixin(): pass _OrmBaseBase = declarative_base() _ORM_BASE = type(_ORM_BASE, (_OrmBaseBase, _BaseORMMixin), {}) Even trying to add a bogus __tablename__ doesn't seem to work. Dropping back to mixing the mixin with every class derived from the declarative base restores functionality. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
I was just looking for the solution to a similar problem... I think the way you're thinking about it actually does work...at least, it does for me! See below: y = select([table]).execute().fetchall() for x in y: ... print x.__dict__ ... {'_RowProxy__parent': sqlalchemy.engine.base.ResultProxy object at 0x18e5c90, '_RowProxy__row': ('data', 'more_data', datetime.datetime(2008, 7, 2, 14, 20, 22, 999023), 1)} mylist = [dict(r) for r in y] mylist [{u'description': 'data', u'other': 'more_data', u'number' : 1, u'changed' : datetime.datetime(2008, 7, 2, 14, 20, 22, 999023)}] On Jul 5, 2:39 pm, zipito [EMAIL PROTECTED] wrote: Good day community, there was similar theme - which ended without answer. How can I convert mine query results to list of dicts. I.e. I want the following select = session.select(MineTableObject) res = select.fetchall() res_list_dict = [dict(r) for r in res] but that doesn't works. dict(r) method returns the array of column_number-column_value not the dictionary column_name-column_value. Is there a possibility to have this ?? best regards, Ilya Dyoshin --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
I also had this issue and searched around for an answer, but found nothing. In my case I;m using the orm and wanted to have those object properties that map to a table be wrapped up in a dict. The new SQLA book is enroute and I figured I'd look in there when I get it, but in the mean time I just added a cheap/crude method for conversion as per below. import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base _ORM_BASE = declarative_base() class _BaseORMMixin(): def ToDict(self, ExclusionList = []): Converts fields in object to a dict, unless field names are in ExclusionList. ret = {} for c in self.__mapper__.columns.keys(): if not c in ExclusionList: ret[c] = getattr(self, c) return ret class ORM_User(_BaseORMMixin, _ORM_BASE): __tablename__ = user snip I figure there must be a better way.. I had to dip into __mapper__, which didn't seem right, but __dict__ obviously has more than what I wanted. In addition, I had to use a mixin because with ext.declaritive (which I like because it is less verbose) I couldn't make a base class to derive from because it complained that I needed __tablename__ in the base class. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
Hi Russell, On Sun, Jul 6, 2008 at 12:32 AM, Russell Warren [EMAIL PROTECTED] wrote: I also had this issue and searched around for an answer, but found nothing. In my case I;m using the orm and wanted to have those object properties that map to a table be wrapped up in a dict. The new SQLA book is enroute and I figured I'd look in there when I get it, but in the mean time I just added a cheap/crude method for conversion as per below. I figure there must be a better way.. I had to dip into __mapper__, which didn't seem right, but __dict__ obviously has more than what I wanted. You could use: class_mapper(self).columns.keys() but I don't really know how that's any better. In addition, I had to use a mixin because with ext.declaritive (which I like because it is less verbose) I couldn't make a base class to derive from because it complained that I needed __tablename__ in the base class. You might want to look into the new instrument_declarative function in 0.5. It allows you to wrap a class with the declarative functionality without using the metaclass. This should permit you to work with a subclass, although I haven't tried it. Michael Trier blog.michaeltrier.com --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: result as the dict()
On Jul 5, 2008, at 4:39 PM, zipito wrote: Good day community, there was similar theme - which ended without answer. How can I convert mine query results to list of dicts. I.e. I want the following select = session.select(MineTableObject) res = select.fetchall() res_list_dict = [dict(r) for r in res] but that doesn't works. dict(r) method returns the array of column_number-column_value not the dictionary column_name-column_value. Is there a possibility to have this ?? dict(r) works for me - from sqlalchemy import * e = create_engine('sqlite://') r = e.execute(select 1 as foo, 2 as bar).fetchall() for x in r: ... print dict(x) ... {u'foo': 1, u'bar': 2} --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---