Re: Simple and safe evaluator
- From: George Sakkis <george.sakkis@xxxxxxxxx>
- Date: Mon, 16 Jun 2008 14:30:37 -0700 (PDT)
On Jun 16, 4:47 pm, bvdp <b...@xxxxxxxxxxxx> wrote:
2. I thought I'd be happy with * / + -, etc. Of course now I want to add
a few more funcs like int() and sin(). How would I do that?
For the builtin eval, just populate the globals dict with the names
you want to make available:
import math
globs = {'__builtins__' : None}
# expose selected builtins
for name in 'True False int float round abs divmod'.split():
globs[name] = eval(name)
# expose selected math constants and functions
for name in 'e pi sqrt exp log ceil floor sin cos tan'.split():
globs[name] = getattr(math,name)
return eval(s, globs, {})
The change to the _ast version is left as an exercise to the reader ;)
George
.
- Follow-Ups:
- Re: Simple and safe evaluator
- From: bvdp
- Re: Simple and safe evaluator
- References:
- Re: Simple and safe evaluator
- From: bvdp
- Re: Simple and safe evaluator
- Prev by Date: Re: How to request data from a lazily-created tree structure ?
- Next by Date: Re: sorted or .sort() ?
- Previous by thread: Re: Simple and safe evaluator
- Next by thread: Re: Simple and safe evaluator
- Index(es):
Relevant Pages
|