safe eval of moderately simple math expressions

Paul McGuire ptmcg at austin.rr.com
Thu Apr 9 14:28:35 EDT 2009


On Apr 9, 10:56 am, Joel Hedlund <joel.hedl... at gmail.com> wrote:
> Hi all!
>
> I'm writing a program that presents a lot of numbers to the user, and I
> want to let the user apply moderately simple arithmentics to these
> numbers.

Joel -

Take a look at the examples page on the pyparsing wiki (http://
pyparsing.wikispaces.com/Examples).  Look at the examples fourFn.py
and simpleArith.py for some expression parsers that you could extend
to support whatever math builtins you wish.  Since you would be doing
your own parsing and eval code, you could be sure that no dangerous
code was being run, just simple arithmetic.

-- Paul



More information about the Python-list mailing list