[storm] Calculated attributes

Gustavo Niemeyer gustavo at niemeyer.net
Tue Jan 15 11:23:54 GMT 2008


Hello Gerdus,

> Is there an easy way to get SQL calculated attributes using storm?
(...)
> I want to turn getBalance into balance property.
> e.g: balance = SQLvalue("SELECT sum(amount) FROM entry")
> I know it can be done using balance = property(getBalance) etc, but a
> convenience function would be great.

Yes, using a property is the way to go in these cases.  If you use that
frequently, you can easily write a helper (untested code):

  def sqlattr(expr):
      def sql_property(self):
          return Store.of(self).execute(expr).get_one()[0]
      return property(sql_property)

At this point, this isn't something enough people have demonstrated
interest for that would be worth adopting in the core.

> Also is there a method that is called on an Entity when it is retrieve
> from the store?  __init__ does not seem to be called.

Yes, the __load__() method is called in that situation.

-- 
Gustavo Niemeyer
http://niemeyer.net



More information about the storm mailing list