Merge pull request #110 from timrichardson/computed_fields
allow compute fields to depend on compute fields defined earlier
This commit is contained in:
+4
-2
@@ -8542,10 +8542,12 @@ class Table(object):
|
||||
# try compute it
|
||||
try:
|
||||
new_fields[name] = (ofield,ofield.compute(row))
|
||||
__,fields[name] = new_fields[name] #the value is the second element of the tuple
|
||||
row = Row(fields) #allow later compute fields to refer to this value
|
||||
except (KeyError, AttributeError):
|
||||
# error sinlently unless field is required!
|
||||
# error silently unless field is required!
|
||||
if ofield.required:
|
||||
raise SyntaxError('unable to comput field: %s' % name)
|
||||
raise SyntaxError('unable to compute field: %s' % name)
|
||||
return new_fields.values()
|
||||
|
||||
def _attempt_upload(self, fields):
|
||||
|
||||
@@ -599,6 +599,19 @@ class TestComputedFields(unittest.TestCase):
|
||||
self.assertEqual(db.tt[id].cc,'zx')
|
||||
db.tt.drop()
|
||||
db.commit()
|
||||
|
||||
# test checking that a compute field can refer to earlier-defined computed fields
|
||||
db.define_table('tt',
|
||||
Field('aa'),
|
||||
Field('bb',default='x'),
|
||||
Field('cc',compute=lambda r: r.aa+r.bb),
|
||||
Field('dd',compute=lambda r: r.bb + r.cc))
|
||||
db.commit()
|
||||
id = db.tt.insert(aa="z")
|
||||
self.assertEqual(db.tt[id].dd,'xzx')
|
||||
db.tt.drop()
|
||||
db.commit()
|
||||
|
||||
|
||||
class TestCommonFilters(unittest.TestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user