Bag's formula

It's possible to define as a bag's item an algebric formula that calculates a value using other bag's item. The method formula() takes as first parameter a formula that uses the char '$' to define all the variables, and receives some kwargs that specify for the path of each variable.

b=Bag({'rect': Bag(), 'trian': Bag()})
b['rect.params.base']=20
b['rect.params.height']=10
b['rect.area']= b.formula('$base*$height', base ='rect.params.base', height='rect.params.height')

>>>print b.rect.area
200

You can define some formulas and symbol if you plan to use them in several situations, in fact Bag has a register of all defined formula and symbols. With the method defineSymbol() it's possible define a variable and link it to a value at the specified path. With the method defineFormula() you can define a formula that uses defined symbols.

b.defineFormula(calculate_perimeter='2*($base + $height)' )
b.defineSymbol(base ='rect.params.base',  height='rect.params.height')
b['rect.perimeter']= b.formula('calculate_perimeter')

>>>print b.rect.perimeter
60