Re: [sqlalchemy] Comparable properties
Oliver Beattie wrote: @property def is_visible(self): return (self.enabled and not self.is_deleted) This can clearly be mapped quite easily to SQL expression `Klass.enabled == True Klass.is_deleted == False` You could always add a class-level attribute that stored this... @property def is_visible(self): return (self.enabled and not self.is_deleted) visible = enabled==True is_deleted==False You may need to wrap that into a method with the classproperty decorator... But, it'd be nice to have one attribute of the object fulfil both roles, and I don't know how to do that :-S Chris -- Simplistix - Content Management, Batch Processing Python Consulting - http://www.simplistix.co.uk -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@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.
RE: [sqlalchemy] Comparable properties
-Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com] On Behalf Of Chris Withers Sent: 08 July 2010 09:28 To: sqlalchemy@googlegroups.com Subject: Re: [sqlalchemy] Comparable properties Oliver Beattie wrote: @property def is_visible(self): return (self.enabled and not self.is_deleted) This can clearly be mapped quite easily to SQL expression `Klass.enabled == True Klass.is_deleted == False` You could always add a class-level attribute that stored this... @property def is_visible(self): return (self.enabled and not self.is_deleted) visible = enabled==True is_deleted==False You may need to wrap that into a method with the classproperty decorator... But, it'd be nice to have one attribute of the object fulfil both roles, and I don't know how to do that :-S Chris I think the 'Derived Attributes' example does what you want: http://www.sqlalchemy.org/docs/examples.html#module-derived_attributes http://www.sqlalchemy.org/trac/browser/examples/derived_attributes/attri butes.py As far as I can tell, it uses some Python descriptor magic to allow your property to work both at the instance and at the class level (so 'self' will either be the instance or the class). Accessing Klass.is_visible returns the SQL expression construct, but instance.is_visible works as normal. You'd be more restricted in what you can write inside your property definition though. For example, you can't use plain Python 'and', or assume that 'self.enabled' evaluates to True or False. I think something like this would work though: @hybrid def is_visible(self): return (self.enabled == True) (self.is_deleted == False) Hope that helps, Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@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.
Re: [sqlalchemy] Comparable properties
King Simon-NFHD78 wrote: @hybrid def is_visible(self): return (self.enabled == True) (self.is_deleted == False) Yeah, having to write something that works as both plain python and a sql layer construct seems a little brittle. I wonder if a decorator could be knocked up which would let you do: def _python_is_visible(self): return (self.enabled and not self.is_deleted) def _sql_is_visible(self): return (self.enabled == True) (self.is_deleted == False) is_visible = some_magic(_python_is_visible,_sql_is_visible) cheers, Chris -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@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.