maxerickson at gmail.com
Sat Aug 13 01:03:00 CEST 2005
Scott David Daniels <Scott.Daniels at Acm.Org> wrote in
news:42fcea4a$1 at nntp0.pdx.net:
> max wrote:
>> Larry Bates <lbates at syscononline.com> wrote in
>> news:42FCDCA7.7030107 at syscononline.com:
>>>Python has built in eval function and doesn't require a library.
>> Are you kidding? Read the original post a little more closely.
>> The o.p. is looking for a library that evaluates mathematical
>> expressions and is callable from python code.
> He is absolutely correct.
> From the web page referenced:
> ucDefineFunction("area(length,width) = length*width");
> ucDefineFunction("test() = 5");
> ucDefineFunction("abc(x, y=10) = x + y");
> ucDefineFunction("shl[x, y] = x * 2^y");
> cout << ucEval("frac(150/17) * area(20,30)") << endl;
> cout << ucEval("abc(5)-abc(3,4)*(#b01101 shl 1)")
> << endl;
> The python equivalent:
> exec "def area(length,width): return length*width"
> exec "def frac(x): return abs(abs(x) - int(abs(x)))"
> exec "def test(): return 5"
> exec "def abc(x, y=10): return x + y"
> exec "def shl(x, y): return x * 2^y"
> print eval("frac(150/17) * area(20,30)")
> print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")
> --Scott David Daniels
> Scott.Daniels at Acm.Org
Ouch, I sure was wrong. You did such a good job making me look
foolish that it was mentioned in Python-URL!. At least Larry Bates
had the grace not to call me an idiot.
More information about the Python-list