You asked for it so here they are: built-in lazy virtual fields.

Q. How do they work?

A. Like normal virtual fields but no need to mess up with lambdas,
etc. Just add a @staticmethod decorator.  Here is an example:

db=DAL()
db.define_table('x',Field('number','integer'))
if db(db.x).isempty(): [db.x.insert(number=i) for i in range(10)]

class MyVirtualFields(object):

    # normal virtual
field
    def normal_shift(self): return self.x.number+1

    # lazy virtual field (because of
@staticmethod)
    @staticmethod
    def lazy_shift(self,delta=4): return self.x.number+delta

db.x.virtualfields.append(MyVirtualFields())

for row in db(db.x).select():
    print row.number, row.normal_shift, row.lazy_shift(delta=7)

normal_shift is a normal virtual field
lazy_shift is a lazy virtual field (the name is accidental, you can
use any names of course).

There is a conceptual difference. In the former case self.x is
<instance>.<attribute> while in the latter self.x is <row>.<attribute>
as there is no instance needed.

This should be faster that some of the hacks we have attempted in the
past and should not consume more memory than needed. There are no
copies of objects when using only virtual fields.

Enjoy!

Reply via email to