[web2py] Re: Lazy virtual fields - strange result!
After some thought, I'm really liking this design for virtual fields... what if lazy/virtual fields were declared directly in db.define_table()? Like so: db.define_table('item', Field('unit_price','double'), Field('quantity','integer'), VirtualField('total_price', lambda self: self.item.unit_price*self.item.quantity, lazy=True)) It's so simple. I still kinda feel like we might find better names than lazy/virtual though. So here's a design I like even more: db.define_table('item', Field('unit_price','double'), Field('quantity','integer'), Method('total_price', lambda self: self.item.unit_price*self.item.quantity, precompute=False)) `precompute' means "not lazy" and would default to false. On Aug 25, 6:14 am, Massimo Di Pierro wrote: > We are moving away from this because of many problems. Try this > instead. It is still experimental but may go into stable soon. > > def vfields(): > db.define_table('item', > Field('unit_price','double'), > Field('quantity','integer')) > db(db.item.id>0).delete() > > db.item.lazy_total_price=Field.lazy(lambda > self:self.item.unit_price*self.item.quantity) > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > {'unit_price':10.00, 'quantity': 99}, > {'unit_price':120.00, 'quantity': 2},]) > res = [] > for r in db(db.item.id>0).select(): > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > return dict(res=res) >
[web2py] Re: Lazy virtual fields - strange result!
I'm sorry, that was a doofus comment. Of course lambdas allow side- effects! I wish mailing lists supported "delete." On Aug 27, 1:08 pm, Michael Toomim wrote: > Interesting approach to use lambdas. Since lambdas don't do side- > effects, I checked out my virtualfields to see if my uses have side > effects. > > In my app I have: > 12 methods total across 3 database tables > 10 of those methods have no side-effects > 2 have side-effects > > The two methods with side-effects are: > 1. row.delete() ... deletes the relevant rows in related tables > 2. row.toggle_x() ... toggles the boolean field of x and updates > cached data on other tables and sends email notifications > > Perhaps this information is useful in designing the next lazy > virtualfields. > > On Aug 25, 9:14 pm, Massimo Di Pierro > wrote: > > > > > > > > > We are moving away from this because of many problems. Try this > > instead. It is still experimental but may go into stable soon. > > > def vfields(): > > db.define_table('item', > > Field('unit_price','double'), > > Field('quantity','integer')) > > db(db.item.id>0).delete() > > > db.item.lazy_total_price=Field.lazy(lambda > > self:self.item.unit_price*self.item.quantity) > > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > {'unit_price':10.00, 'quantity': 99}, > > {'unit_price':120.00, 'quantity': 2},]) > > res = [] > > for r in db(db.item.id>0).select(): > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > return dict(res=res) > > > On Aug 25, 7:50 am, Martin Weissenboeck wrote: > > > > I wanted to learn more about lazy virtual fields and therefore I have > > > repeated the example from the book: > > > > def vfields(): > > > db.define_table('item', > > > Field('unit_price','double'), > > > Field('quantity','integer')) > > > > db(db.item.id>0).delete() > > > > class MyVirtualFields: > > > def lazy_total_price(self): > > > return lambda self=self: self.item.unit_price*self.item.quantity > > > > db.item.virtualfields.append (MyVirtualFields()) > > > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > > {'unit_price':10.00, 'quantity': 99}, > > > {'unit_price':120.00, 'quantity': 2},]) > > > > res = [] > > > for r in db(db.item.id>0).select(): > > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > > return dict(res=res) > > > > The expected output is: > > > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > > > But I got > > > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > > > * > > > * > > > *Three times the same result. > > > * > > > I have read the book and my program over and over again - but I cannot see > > > any error.* > > > * > > > > Does somebody have an idea? > > > Martin
[web2py] Re: Lazy virtual fields - strange result!
Interesting approach to use lambdas. Since lambdas don't do side- effects, I checked out my virtualfields to see if my uses have side effects. In my app I have: 12 methods total across 3 database tables 10 of those methods have no side-effects 2 have side-effects The two methods with side-effects are: 1. row.delete() ... deletes the relevant rows in related tables 2. row.toggle_x() ... toggles the boolean field of x and updates cached data on other tables and sends email notifications Perhaps this information is useful in designing the next lazy virtualfields. On Aug 25, 9:14 pm, Massimo Di Pierro wrote: > We are moving away from this because of many problems. Try this > instead. It is still experimental but may go into stable soon. > > def vfields(): > db.define_table('item', > Field('unit_price','double'), > Field('quantity','integer')) > db(db.item.id>0).delete() > > db.item.lazy_total_price=Field.lazy(lambda > self:self.item.unit_price*self.item.quantity) > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > {'unit_price':10.00, 'quantity': 99}, > {'unit_price':120.00, 'quantity': 2},]) > res = [] > for r in db(db.item.id>0).select(): > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > return dict(res=res) > > On Aug 25, 7:50 am, Martin Weissenboeck wrote: > > > > > > > > > I wanted to learn more about lazy virtual fields and therefore I have > > repeated the example from the book: > > > def vfields(): > > db.define_table('item', > > Field('unit_price','double'), > > Field('quantity','integer')) > > > db(db.item.id>0).delete() > > > class MyVirtualFields: > > def lazy_total_price(self): > > return lambda self=self: self.item.unit_price*self.item.quantity > > > db.item.virtualfields.append (MyVirtualFields()) > > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > {'unit_price':10.00, 'quantity': 99}, > > {'unit_price':120.00, 'quantity': 2},]) > > > res = [] > > for r in db(db.item.id>0).select(): > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > return dict(res=res) > > > The expected output is: > > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > > But I got > > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > > * > > * > > *Three times the same result. > > * > > I have read the book and my program over and over again - but I cannot see > > any error.* > > * > > > Does somebody have an idea? > > Martin
Re: [web2py] Re: Lazy virtual fields - strange result!
I got: Traceback (most recent call last): ... db.item.lazy_total_price=Field.lazy(lambda self:self.item.unit_price*self.item.quantity) AttributeError: type object 'Field' has no attribute 'lazy' Version 1.98.2 (2011-08-25 19:11:31) Martin 2011/8/25 Massimo Di Pierro > The nightly build or trunk. > > On Aug 25, 10:24 am, Martin Weissenboeck wrote: > > Thank you. Which version of web2py? 1.98.2 does not know Field.lazy > > > > 2011/8/25 Massimo Di Pierro > > > > > > > > > > > > > > > > > We are moving away from this because of many problems. Try this > > > instead. It is still experimental but may go into stable soon. > > > > > def vfields(): > > > db.define_table('item', > > > Field('unit_price','double'), > > > Field('quantity','integer')) > > > db(db.item.id>0).delete() > > > > >db.item.lazy_total_price=Field.lazy(lambda > > > self:self.item.unit_price*self.item.quantity) > > > > >db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > > {'unit_price':10.00, 'quantity': 99}, > > > {'unit_price':120.00, 'quantity': 2},]) > > > res = [] > > > for r in db(db.item.id>0).select(): > > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > > return dict(res=res) > > > > > On Aug 25, 7:50 am, Martin Weissenboeck wrote: > > > > I wanted to learn more about lazy virtual fields and therefore I have > > > > repeated the example from the book: > > > > > > def vfields(): > > > >db.define_table('item', > > > > Field('unit_price','double'), > > > > Field('quantity','integer')) > > > > > >db(db.item.id>0).delete() > > > > > >class MyVirtualFields: > > > > def lazy_total_price(self): > > > >return lambda self=self: > self.item.unit_price*self.item.quantity > > > > > >db.item.virtualfields.append (MyVirtualFields()) > > > > > >db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > > > {'unit_price':10.00, 'quantity': 99}, > > > > {'unit_price':120.00, 'quantity': 2},]) > > > > > >res = [] > > > >for r in db(db.item.id>0).select(): > > > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > > >return dict(res=res) > > > > > > The expected output is: > > > > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > > > > > But I got > > > > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > > > > * > > > > * > > > > *Three times the same result. > > > > * > > > > I have read the book and my program over and over again - but I > cannot > > > see > > > > any error.* > > > > * > > > > > > Does somebody have an idea? > > > > Martin >
[web2py] Re: Lazy virtual fields - strange result!
The nightly build or trunk. On Aug 25, 10:24 am, Martin Weissenboeck wrote: > Thank you. Which version of web2py? 1.98.2 does not know Field.lazy > > 2011/8/25 Massimo Di Pierro > > > > > > > > > We are moving away from this because of many problems. Try this > > instead. It is still experimental but may go into stable soon. > > > def vfields(): > > db.define_table('item', > > Field('unit_price','double'), > > Field('quantity','integer')) > > db(db.item.id>0).delete() > > > db.item.lazy_total_price=Field.lazy(lambda > > self:self.item.unit_price*self.item.quantity) > > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > {'unit_price':10.00, 'quantity': 99}, > > {'unit_price':120.00, 'quantity': 2},]) > > res = [] > > for r in db(db.item.id>0).select(): > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > return dict(res=res) > > > On Aug 25, 7:50 am, Martin Weissenboeck wrote: > > > I wanted to learn more about lazy virtual fields and therefore I have > > > repeated the example from the book: > > > > def vfields(): > > > db.define_table('item', > > > Field('unit_price','double'), > > > Field('quantity','integer')) > > > > db(db.item.id>0).delete() > > > > class MyVirtualFields: > > > def lazy_total_price(self): > > > return lambda self=self: self.item.unit_price*self.item.quantity > > > > db.item.virtualfields.append (MyVirtualFields()) > > > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > > {'unit_price':10.00, 'quantity': 99}, > > > {'unit_price':120.00, 'quantity': 2},]) > > > > res = [] > > > for r in db(db.item.id>0).select(): > > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > > > return dict(res=res) > > > > The expected output is: > > > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > > > But I got > > > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > > > * > > > * > > > *Three times the same result. > > > * > > > I have read the book and my program over and over again - but I cannot > > see > > > any error.* > > > * > > > > Does somebody have an idea? > > > Martin
Re: [web2py] Re: Lazy virtual fields - strange result!
Thank you. Which version of web2py? 1.98.2 does not know Field.lazy 2011/8/25 Massimo Di Pierro > We are moving away from this because of many problems. Try this > instead. It is still experimental but may go into stable soon. > > def vfields(): > db.define_table('item', > Field('unit_price','double'), > Field('quantity','integer')) > db(db.item.id>0).delete() > >db.item.lazy_total_price=Field.lazy(lambda > self:self.item.unit_price*self.item.quantity) > >db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > {'unit_price':10.00, 'quantity': 99}, > {'unit_price':120.00, 'quantity': 2},]) > res = [] > for r in db(db.item.id>0).select(): > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > return dict(res=res) > > On Aug 25, 7:50 am, Martin Weissenboeck wrote: > > I wanted to learn more about lazy virtual fields and therefore I have > > repeated the example from the book: > > > > def vfields(): > >db.define_table('item', > > Field('unit_price','double'), > > Field('quantity','integer')) > > > >db(db.item.id>0).delete() > > > >class MyVirtualFields: > > def lazy_total_price(self): > >return lambda self=self: self.item.unit_price*self.item.quantity > > > >db.item.virtualfields.append (MyVirtualFields()) > > > >db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > > {'unit_price':10.00, 'quantity': 99}, > > {'unit_price':120.00, 'quantity': 2},]) > > > >res = [] > >for r in db(db.item.id>0).select(): > > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > >return dict(res=res) > > > > The expected output is: > > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > > > But I got > > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > > * > > * > > *Three times the same result. > > * > > I have read the book and my program over and over again - but I cannot > see > > any error.* > > * > > > > Does somebody have an idea? > > Martin >
[web2py] Re: Lazy virtual fields - strange result!
We are moving away from this because of many problems. Try this instead. It is still experimental but may go into stable soon. def vfields(): db.define_table('item', Field('unit_price','double'), Field('quantity','integer')) db(db.item.id>0).delete() db.item.lazy_total_price=Field.lazy(lambda self:self.item.unit_price*self.item.quantity) db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, {'unit_price':10.00, 'quantity': 99}, {'unit_price':120.00, 'quantity': 2},]) res = [] for r in db(db.item.id>0).select(): res.append([r.unit_price, r.quantity, r.lazy_total_price()]) return dict(res=res) On Aug 25, 7:50 am, Martin Weissenboeck wrote: > I wanted to learn more about lazy virtual fields and therefore I have > repeated the example from the book: > > def vfields(): > db.define_table('item', > Field('unit_price','double'), > Field('quantity','integer')) > > db(db.item.id>0).delete() > > class MyVirtualFields: > def lazy_total_price(self): > return lambda self=self: self.item.unit_price*self.item.quantity > > db.item.virtualfields.append (MyVirtualFields()) > > db.item.bulk_insert([{'unit_price':12.00, 'quantity': 15}, > {'unit_price':10.00, 'quantity': 99}, > {'unit_price':120.00, 'quantity': 2},]) > > res = [] > for r in db(db.item.id>0).select(): > res.append([r.unit_price, r.quantity, r.lazy_total_price()]) > return dict(res=res) > > The expected output is: > [[12.0, 15, 180.0], [10.0, 99, 990.0], [120.0, 2, 240.0]] > > But I got > * [[12.0, 15, *240.0]*, [10.0, 99, *240.0*], [120.0, 2, 240.0]]* > * > * > *Three times the same result. > * > I have read the book and my program over and over again - but I cannot see > any error.* > * > > Does somebody have an idea? > Martin