[storm] Calculated attributes
Gerdus van Zyl
gerdusvanzyl at gmail.com
Tue Jan 15 13:15:16 GMT 2008
Thanks for the reply. The function/property code you provided was very
usefull, another trick in the python toolbox. Maybe it can be added to
a cookbook page on the website. I am all for keeping storm light and
uncomplicated, it's a refreshing change.
On Jan 15, 2008 1:23 PM, Gustavo Niemeyer <gustavo at niemeyer.net> wrote:
> 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