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 compute it
|
||||||
try:
|
try:
|
||||||
new_fields[name] = (ofield,ofield.compute(row))
|
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):
|
except (KeyError, AttributeError):
|
||||||
# error sinlently unless field is required!
|
# error silently unless field is required!
|
||||||
if ofield.required:
|
if ofield.required:
|
||||||
raise SyntaxError('unable to comput field: %s' % name)
|
raise SyntaxError('unable to compute field: %s' % name)
|
||||||
return new_fields.values()
|
return new_fields.values()
|
||||||
|
|
||||||
def _attempt_upload(self, fields):
|
def _attempt_upload(self, fields):
|
||||||
|
|||||||
@@ -600,6 +600,19 @@ class TestComputedFields(unittest.TestCase):
|
|||||||
db.tt.drop()
|
db.tt.drop()
|
||||||
db.commit()
|
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):
|
class TestCommonFilters(unittest.TestCase):
|
||||||
|
|
||||||
def testRun(self):
|
def testRun(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user