[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: access the fields in a record as a dict
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 hi ilya, everybody, that time, i was using elixir and not the select() function in sa. elixir.query.filter() returned a list of objects, with their fields as attributes (same as the entity definition in elixir). thus, to have each record transformed in a dict, i have used a transformation function, and an iterator converter, like the following: ~def prep_record(record): ~values = {} ~for field in allfields: #a list of field names ~values[field] = getattr(record, field) ~records = itertools.imap(prep_record, query) now i'm using sa 0.4.6, and when i'm executing a select() on a session, i'm getting a resultproxy object, which may be iterated by rows, and each field of each row may be got by row[fieldname] hope this helps, alex zipito wrote: | good day alex | | | Did you find the solution for this ? | | | Best regards, | Ilya Dyohsin | | On 17 янв, 06:06, alex bodnaru [EMAIL PROTECTED] wrote: | hi mike, | | thanks for your prompt answer. | | it doesn't work anymore, indeed :( | | alex | | | | Michael Bayer wrote: | | what, likedict(row) ? that should workno ? | On Jan 16, 2008, at 6:29 PM, alex bodnaru wrote: | hi friends, | in older sa i could cast a record to adict, in order to access their | fields by their name, especially if the field name is not a valid | python | identifier, like a calculated or fixed numeric column. | how could i do it again please? | tia, | alex -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iQCVAwUBSHCg8dpwN1sq38njAQKIxAP/Y8eKEMm18qI12ZEtmw2nwq+z9HB8ykjY g/w3ErQvfcOp3K9bFqL7/1MHgIIVATy636rQbe1F7wZYkR3NaQlKlkdVt8Z+YSxu zmLeR4EL+q1CgnuJTsRh5+xRxqzdUZf4vEAFKipjGwrOvJT4EglY/CqUNx0AZpwP LTQC456nCPQ= =imB5 -END PGP SIGNATURE- --~--~-~--~~~---~--~~ 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: IDEA: Call-local vs thread-local context
On Jul 4, 3:41 pm, Michael Bayer [EMAIL PROTECTED] wrote: On Jul 4, 2008, at 5:40 AM, Iwan wrote: Assume also that CC.get_context_hash() returns id(CC.get_context()) Session = scoped_session(sessionmaker(), scopefunc=CC.get_context_hash) if get_context_hash returns a dict, thats specifically what would *not* work since its not hashable. Mmm, please note I said it should return id() of the dict... -i --~--~-~--~~~---~--~~ 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: IDEA: Call-local vs thread-local context
On Jul 6, 2008, at 5:08 AM, Iwan wrote: On Jul 4, 3:41 pm, Michael Bayer [EMAIL PROTECTED] wrote: On Jul 4, 2008, at 5:40 AM, Iwan wrote: Assume also that CC.get_context_hash() returns id(CC.get_context()) Session = scoped_session(sessionmaker(), scopefunc=CC.get_context_hash) if get_context_hash returns a dict, thats specifically what would *not* work since its not hashable. Mmm, please note I said it should return id() of the dict... ok, sounds good. have you tried it ? --~--~-~--~~~---~--~~ 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] how query.count() works?
i have some tree-walking query via many2many, having configurable levels of hops to look into. and it returns very different results for len(query) and query.count(): len() stays constant above the depth of the tree (correct), while .count() keeps growing an growing. what's can be wrong? --~--~-~--~~~---~--~~ 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: how query.count() works?
On Jul 6, 2008, at 11:33 AM, [EMAIL PROTECTED] wrote: i have some tree-walking query via many2many, having configurable levels of hops to look into. and it returns very different results for len(query) and query.count(): len() stays constant above the depth of the tree (correct), while .count() keeps growing an growing. what's can be wrong? the generated SQL will tell all, lately ive been partial towards: sess.query(func.count(func.distinct(MyClass.id))).other criterion.first() since the way count() is determined can be controlled accurately. --~--~-~--~~~---~--~~ 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: how query.count() works?
the query is ugly; seems count(1) counts the decart-product or something: SA: INFO SELECT count(1) AS count_1 FROM Otnoshenie, mm_nazn2rabmesto, mm_nazn2pozicia, mm_pozicia2otdel, mm_otdel2otdel AS mm_otdel2otdel, mm_otdel2otdel AS mm_otdel2otdel1, mm_otdel2otdel AS mm_otdel2otdel2, PVOwnership JOIN ParamValue ON PVOwnership.param_value_id = ParamValue.db_id WHERE ParamValue.time_trans = ? AND ParamValue.time_valid = ? AND ParamValue.time_valid = ? AND (Otnoshenie.db_id = PVOwnership._AbstractNaznachenie_id OR Otnoshenie.db_id = mm_nazn2rabmesto.left_id AND mm_nazn2rabmesto.right_id = PVOwnership._Rabotno_miasto_id OR Otnoshenie.db_id = mm_nazn2pozicia.left_id AND (mm_nazn2pozicia.right_id = PVOwnership._Pozicia_id OR mm_nazn2pozicia.right_id = mm_pozicia2otdel.left_id AND (mm_pozicia2otdel.right_id = PVOwnership._Otdel_id OR mm_pozicia2otdel.right_id = mm_otdel2otdel.left_id AND (mm_otdel2otdel.right_id = PVOwnership._Otdel_id OR mm_otdel2otdel.right_id = mm_otdel2otdel1.left_id AND (mm_otdel2otdel1.right_id = PVOwnership._Otdel_id OR mm_otdel2otdel1.right_id = mm_otdel2otdel2.left_id AND mm_otdel2otdel2.right_id = PVOwnership._Otdel_id) AND PVOwnership.param_value_id = ParamValue.db_id AND Otnoshenie.obj_id_id = ? that's 3 levels: 4752 4 levels: 14256 5 levels: 42768 .. 8 levels: 1154736 while there are 57 rows total. anyway, i didnt need count, just tried it. this thing is a instance-level value-inheritance chain, where some parameter can be present anywhere on the nodes, and the nearest visible one should be taken. This as a const-level recursion takes forever for levels=5, i'm wondering should i try nested-sets, or directly to try organise maintain some cache whats visible on each node. On Sunday 06 July 2008 18:47, Michael Bayer wrote: On Jul 6, 2008, at 11:33 AM, [EMAIL PROTECTED] wrote: i have some tree-walking query via many2many, having configurable levels of hops to look into. and it returns very different results for len(query) and query.count(): len() stays constant above the depth of the tree (correct), while .count() keeps growing an growing. what's can be wrong? the generated SQL will tell all, lately ive been partial towards: sess.query(func.count(func.distinct(MyClass.id))).other criterion.first() since the way count() is determined can be controlled accurately. --~--~-~--~~~---~--~~ 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: how query.count() works?
On Jul 6, 2008, at 12:21 PM, [EMAIL PROTECTED] wrote: the query is ugly; seems count(1) counts the decart-product or something: SA: INFO SELECT count(1) AS count_1 FROM Otnoshenie, mm_nazn2rabmesto, mm_nazn2pozicia, mm_pozicia2otdel, mm_otdel2otdel AS mm_otdel2otdel, mm_otdel2otdel AS mm_otdel2otdel1, mm_otdel2otdel AS mm_otdel2otdel2, PVOwnership JOIN ParamValue ON PVOwnership.param_value_id = ParamValue.db_id WHERE ParamValue.time_trans = ? AND ParamValue.time_valid = ? AND ParamValue.time_valid = ? AND (Otnoshenie.db_id = PVOwnership._AbstractNaznachenie_id OR Otnoshenie.db_id = mm_nazn2rabmesto.left_id AND mm_nazn2rabmesto.right_id = PVOwnership._Rabotno_miasto_id OR Otnoshenie.db_id = mm_nazn2pozicia.left_id AND (mm_nazn2pozicia.right_id = PVOwnership._Pozicia_id OR mm_nazn2pozicia.right_id = mm_pozicia2otdel.left_id AND (mm_pozicia2otdel.right_id = PVOwnership._Otdel_id OR mm_pozicia2otdel.right_id = mm_otdel2otdel.left_id AND (mm_otdel2otdel.right_id = PVOwnership._Otdel_id OR mm_otdel2otdel.right_id = mm_otdel2otdel1.left_id AND (mm_otdel2otdel1.right_id = PVOwnership._Otdel_id OR mm_otdel2otdel1.right_id = mm_otdel2otdel2.left_id AND mm_otdel2otdel2.right_id = PVOwnership._Otdel_id) AND PVOwnership.param_value_id = ParamValue.db_id AND Otnoshenie.obj_id_id = ? that's 3 levels: 4752 4 levels: 14256 5 levels: 42768 .. 8 levels: 1154736 while there are 57 rows total. I have no idea what that is. Is that a bunch of join()/filter() you've added to the query before issuing count() ? or are you mapped to a select() statement of that form ? (either way I dont see what choice SA has in the matter...) --~--~-~--~~~---~--~~ 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: how query.count() works?
On Sunday 06 July 2008 21:33:49 Michael Bayer wrote: On Jul 6, 2008, at 12:21 PM, [EMAIL PROTECTED] wrote: the query is ugly; seems count(1) counts the decart-product or something: SA: INFO SELECT count(1) AS count_1 FROM Otnoshenie, mm_nazn2rabmesto, mm_nazn2pozicia, mm_pozicia2otdel, mm_otdel2otdel AS mm_otdel2otdel, mm_otdel2otdel AS mm_otdel2otdel1, mm_otdel2otdel AS mm_otdel2otdel2, PVOwnership JOIN ParamValue ON PVOwnership.param_value_id = ParamValue.db_id WHERE ParamValue.time_trans = ? AND ParamValue.time_valid = ? AND ParamValue.time_valid = ? AND (Otnoshenie.db_id = PVOwnership._AbstractNaznachenie_id OR Otnoshenie.db_id = mm_nazn2rabmesto.left_id AND mm_nazn2rabmesto.right_id = PVOwnership._Rabotno_miasto_id OR Otnoshenie.db_id = mm_nazn2pozicia.left_id AND (mm_nazn2pozicia.right_id = PVOwnership._Pozicia_id OR mm_nazn2pozicia.right_id = mm_pozicia2otdel.left_id AND (mm_pozicia2otdel.right_id = PVOwnership._Otdel_id OR mm_pozicia2otdel.right_id = mm_otdel2otdel.left_id AND (mm_otdel2otdel.right_id = PVOwnership._Otdel_id OR mm_otdel2otdel.right_id = mm_otdel2otdel1.left_id AND (mm_otdel2otdel1.right_id = PVOwnership._Otdel_id OR mm_otdel2otdel1.right_id = mm_otdel2otdel2.left_id AND mm_otdel2otdel2.right_id = PVOwnership._Otdel_id) AND PVOwnership.param_value_id = ParamValue.db_id AND Otnoshenie.obj_id_id = ? that's 3 levels: 4752 4 levels: 14256 5 levels: 42768 .. 8 levels: 1154736 while there are 57 rows total. I have no idea what that is. Is that a bunch of join()/filter() you've added to the query before issuing count() ? or are you mapped to a select() statement of that form ? (either way I dont see what choice SA has in the matter...) bunch of joins+filters, and at the end, i just did .count() instead of .all(). probably what u've suggested - count(query.class_.id), with or without distinct) makes more sense than the default count(1)... --~--~-~--~~~---~--~~ 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] nullable=False by default
Is possible create the fields with nullable=False by default? It's heavy to have to add this to each field. --~--~-~--~~~---~--~~ 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: nullable=False by default
And, why can I add 'default' -- def Column0(*a, **k): return Column(nullable=False, default='', *a, **k) -- it shows the next error: TypeError: _FigureVisitName object got multiple values for keyword argument 'default' On Jul 6, 7:53 pm, [EMAIL PROTECTED] wrote: u can workaround by def Column0( *a,**k): return Column( nullable=False, *a,**k) and use Column0(...) instead of Column... On Sunday 06 July 2008 21:53:52 Kless wrote: Is possible create the fields with nullable=False by default? It's heavy to have to add this to each field. --~--~-~--~~~---~--~~ 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: nullable=False by default
I'm using so: def Column0(*a, **k): return Column(nullable=False, default='', *a, **k) groups_table = Table('foo', metadata, Column0('id', Integer, primary_key=True), Column0('bar', Unicode(16)), ) Note: I'm using SA 0.5 beta. But well, that solved it. Thank you very much. On Jul 6, 9:17 pm, [EMAIL PROTECTED] wrote: how are u using it? probably u have another default=? or there is a non-keyword/positional argument matching that... try something like: def Column0(*a, **k): k.setdefault('default', '') k.setdefault('nullable', False) return Column(*a, **k) thus if u dont specify it explicit, it'll be implicit there. And, why can I add 'default' -- def Column0(*a, **k): return Column(nullable=False, default='', *a, **k) -- it shows the next error: TypeError: _FigureVisitName object got multiple values for keyword argument 'default' On Jul 6, 7:53 pm, [EMAIL PROTECTED] wrote: u can workaround by def Column0( *a,**k): return Column( nullable=False, *a,**k) and use Column0(...) instead of Column... On Sunday 06 July 2008 21:53:52 Kless wrote: Is possible create the fields with nullable=False by default? It's heavy to have to add this to each field. --~--~-~--~~~---~--~~ 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: nullable=False by default
I think that there is a bug. autoincrement doesn't works with *default=''* Any solution to this problem? On Jul 6, 10:46 pm, Kless [EMAIL PROTECTED] wrote: I'm using so: def Column0(*a, **k): return Column(nullable=False, default='', *a, **k) groups_table = Table('foo', metadata, Column0('id', Integer, primary_key=True), Column0('bar', Unicode(16)), ) Note: I'm using SA 0.5 beta. But well, that solved it. Thank you very much. On Jul 6, 9:17 pm, [EMAIL PROTECTED] wrote: how are u using it? probably u have another default=? or there is a non-keyword/positional argument matching that... try something like: def Column0(*a, **k): k.setdefault('default', '') k.setdefault('nullable', False) return Column(*a, **k) thus if u dont specify it explicit, it'll be implicit there. And, why can I add 'default' -- def Column0(*a, **k): return Column(nullable=False, default='', *a, **k) -- it shows the next error: TypeError: _FigureVisitName object got multiple values for keyword argument 'default' On Jul 6, 7:53 pm, [EMAIL PROTECTED] wrote: u can workaround by def Column0( *a,**k): return Column( nullable=False, *a,**k) and use Column0(...) instead of Column... On Sunday 06 July 2008 21:53:52 Kless wrote: Is possible create the fields with nullable=False by default? It's heavy to have to add this to each field. --~--~-~--~~~---~--~~ 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] _is_oid
Does anybody could say anything more? Defaults to False: used internally to indicate that this column is used as the quasi-hidden oid column --~--~-~--~~~---~--~~ 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: _is_oid
the underscore here means, doesn't do what you'd want it to. explicit oid support is likely to be removed soon. On Jul 6, 2008, at 7:35 PM, Kless wrote: Does anybody could say anything more? Defaults to False: used internally to indicate that this column is used as the quasi-hidden oid column --~--~-~--~~~---~--~~ 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: nullable=False by default
On Jul 6, 2008, at 7:06 PM, Kless wrote: I think that there is a bug. autoincrement doesn't works with *default=''* Any solution to this problem? the docs for autoincrement need to be read closely. This flag only applies to Integer columns with the primary key flag set. I'm not sure what interaction it would be expected to have with default in any case (default is all you need if you have a custom default generation function). --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---