newbie: generate a function based on an expression

Ezequiel, Justin j.ezequiel at spitech.com
Tue Dec 13 01:20:13 EST 2005


>>> def genFun(expr, locs):
... 	return lambda x: eval('min(Max,max(Min,%s))' % expr, locs, {'x':
x})
... 
>>> fun = genFun("A*x+off", {'A': 3.0, 'off': -0.5, 'Max': 2.0,  'Min':
-2.0} )
>>> fun
<function <lambda> at 0x011B1470>
>>> fun(0)
-0.5
>>> fun(-10)
-2.0
>>> fun(10)
2.0
>>> 



More information about the Python-list mailing list