[sqlalchemy] Python 3.4 Enum type

2013-08-06 Thread Ian Kelly
Since Python 3.4 is adding support for enums to the standard library,
I wrote a TypeDecorator for it:

import sqlalchemy.types as types


class PythonEnum(types.TypeDecorator):

impl = types.Enum

def __init__(self, enum_class, **kw):
super().__init__(*(m.name for m in enum_class), **kw)
self._enum_class = enum_class

def process_bind_param(self, value, dialect):
return value.name

def process_result_value(self, value, dialect):
return self._enum_class[value]

@property
def python_type(self):
return self._enum_class


Comments welcome.  Is this something that should be added to sqlalchemy?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Python 3.4 Enum type

2013-08-06 Thread Michael Bayer
there's a lot of flavors of python enum and I recall the standard library one 
rubs me the wrong way a bit.   I'd prefer for sqlalchemy.Enum to have some easy 
way to accommodate any Python-side enum using some kind of convention, 
obviously one that's compat with the std lib enum.




On Aug 6, 2013, at 5:12 PM, Ian Kelly ian.g.ke...@gmail.com wrote:

 Since Python 3.4 is adding support for enums to the standard library,
 I wrote a TypeDecorator for it:
 
 import sqlalchemy.types as types
 
 
 class PythonEnum(types.TypeDecorator):
 
impl = types.Enum
 
def __init__(self, enum_class, **kw):
super().__init__(*(m.name for m in enum_class), **kw)
self._enum_class = enum_class
 
def process_bind_param(self, value, dialect):
return value.name
 
def process_result_value(self, value, dialect):
return self._enum_class[value]
 
@property
def python_type(self):
return self._enum_class
 
 
 Comments welcome.  Is this something that should be added to sqlalchemy?
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 



signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] Association Proxy question ( or other ORM feature )

2013-08-06 Thread Jonathan Vanasco
I have three tables that are structured somewhat like this:

Document
* id 
* all_images = sa.orm.relationship(Document2Image)

Image
* id
* all_documents = sa.orm.relationship(Document2Image)

Document2Image
* id
* document_id
* image_id
* image_type_id
* document = sa.orm.relationship(Document)
* image = sa.orm.relationship(Image)



is there a convenient way to set up a relationship on Document where it 
maps to Document2Image with a specific Document2Image.image_type_id ?

I believe I want a variant of the Association Proxy [ 
http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/associationproxy.html 
] that was listed in Relationships - Association Object [ 
http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#association-object 
]

I just can't wrap my head around this.

What I'd like to have is something like :

Document
* id 
* all_images = sa.orm.relationship(Document2Image)
* header_image = sa.orm.relationship( Image , filter( Image.image_type_id 
== 1 , Image.id == Document2Image.image_id , Document2Image.document_id == 
Document.id )

this way i can just write out :

   document.header_image

and either see 'None' or the 'Image' object.

if it matters, i'm only concerned with reads right now.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Association Proxy question ( or other ORM feature )

2013-08-06 Thread Michael Bayer

On Aug 6, 2013, at 6:23 PM, Jonathan Vanasco jonat...@findmeon.com wrote:

 I have three tables that are structured somewhat like this:
 
   Document
   * id 
   * all_images = sa.orm.relationship(Document2Image)
 
   Image
   * id
   * all_documents = sa.orm.relationship(Document2Image)
 
   Document2Image
   * id
   * document_id
   * image_id
   * image_type_id
   * document = sa.orm.relationship(Document)
   * image = sa.orm.relationship(Image)
 
 
 
 is there a convenient way to set up a relationship on Document where it maps 
 to Document2Image with a specific Document2Image.image_type_id ?
 
 I believe I want a variant of the Association Proxy [ 
 http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/associationproxy.html ] 
 that was listed in Relationships - Association Object [ 
 http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#association-object
  ]

you can build a relationship from Document to Document2Image using a custom 
primaryjoin, then relationship to Image, then association proxy.

Or, you can produce a select() of what rows you want from Document2Image, then 
make that as the secondary argument between a single relationship on 
Document-Image.

If OTOH you want a join condition that uses columns from Document2Image and 
Image at the same time, sometimes you can make a select() of those two things, 
then create a non primary mapper to Image using that select() as the base, then 
Document gets a relationship referring to that non-primary mapper.




signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] notin_ passed empty sequence with unexpected substitution

2013-08-06 Thread Victor Reichert
Hi,

I'm running:

selected_eventids = []

selected_event = 
self.session.query(Master_Simulation_Event).filter(Master_Simulation_Event.cumulative_probability
 
= selection_percentile, 
Master_Simulation_Event.event_id.notin_(selected_eventids) ).\
order_by(Master_Simulation_Event.cumulative_probability - 
selection_percentile).first()

And it generates:

2013-08-06 18:03:43,196 INFO sqlalchemy.engine.base.Engine SELECT TOP 1 
master_simulation_events.id AS master_simulation_events_id, 
master_simulation_events.event_id AS master_simulation_events_event_id, 
master_simulation_events.rate AS master_simulation_events_rate, 
master_simulation_events.sample_probability AS 
master_simulation_events_sample_probability, 
master_simulation_events.cumulative_probability AS 
master_simulation_events_cumulative_probability 
FROM master_simulation_events 
WHERE master_simulation_events.cumulative_probability = ? AND 
master_simulation_events.event_id != master_simulation_events.event_id 
ORDER BY master_simulation_events.cumulative_probability - ?
2013-08-06 18:03:43,196 INFO sqlalchemy.engine.base.Engine 
(0.19528160149168805, 0.19528160149168805)

The important part is the notin_([]) is translated to: 
 master_simulation_events.event_id != master_simulation_events.event_id

I recieve the following warning:

C:\Python27\lib\site-packages\sqlalchemy\sql\expression.py:1982: SAWarning: 
The IN-predicate on master_simulation_events.event_id was invoked with an 
empty sequence. This results in a contradiction, which nonetheless can be 
expensive to evaluate.  Consider alternative strategies for improved 
performance.
  return o[0](self, self.expr, op, *(other + o[1:]), **kwargs)

While it doesn't make a lot of sense to pass an empty list to an in_ clause 
I think passing an emtpy list list to notin_ clause is OK and it shouldn't 
be replaced.

Is this a bug?

~Victor

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.