so, I read the docs and.....

I think either there's a bug or I'm doing something wrong....

in db.py

---------------
.....

db.define_table('test',
                Field('name', length=5)
                )

class TestLazy:
    def lazy_test_field(self):
        def lazy(self=self):
            return self.test.name
        return lazy

class TestNotLazy:
    def not_lazy_test_field(self):
        return self.test.name

db.test.virtualfields.append(TestLazy())
db.test.virtualfields.append(TestNotLazy())

----------------

Testing in the shell

web2py Shell Version 1.83.2 (2010-08-15 08:16:30)
In[0]:  set = db(db.test.id>0).select()

Out[0]:
In[1]:  set.as_dict()

Out[1]: {1: {'name': 'miao', 'id': 1, 'not_lazy_test_field': 'miao'},
2: {'name': 'bau', 'id': 2, 'not_lazy_test_field': 'bau'}}

In[2]:for row in set:
    print row.id, row.name, row.not_lazy_test_field

Out[2]:
1 miao miao
2 bau bau

In[3]:
for row in set:
    print row.id, row.name, row.not_lazy_test_field,
row.lazy_test_field

Out[3]:
1 miao miao <function lazy at 0x2e5c2a8>
2 bau bau <function lazy at 0x308ec80>

In[4]:
for row in set:
    print row.id, row.name, row.not_lazy_test_field,
row.lazy_test_field()

Out[4]:
1 miao miao bau
2 bau bau bau


sorry for the unmeaning data in the set, but...seems that lazy
virtualfield is evaluated every line (different hash function when you
try to print row.lazy_test_field without parenthesis, In[3]) but it
returns data computed for the last line in the set...in all the rows
of the set itself (In[4])

Can somebody point me in the right direction ?
Thanks

Reply via email to