Changing an AST

B

beza1e1

Is it possible compiler.parse a statement, then change and then
execute/resolve it?

Background:
I'm probably to lazy to write my own parser.

I have such a statement as string: "distance = x**2 + y**2"
x and y are undefined, so it is no executable Python code, but it is
parseable. Now i'd like traverse through the AST and change Name('x')
for the value i have elsewhere. And finally let Python resolve the
computation.

More Background:
I want to write a simulation game with many interdepended values. I
don't want to create a class with dozens of functions, but let Python
create/evaluate them.

I hope this can be understood ;)
 
F

Fredrik Lundh

beza1e1 said:
I have such a statement as string: "distance = x**2 + y**2"
x and y are undefined, so it is no executable Python code, but it is
parseable. Now i'd like traverse through the AST and change Name('x')
for the value i have elsewhere. And finally let Python resolve the
computation.

you can use the parser class for this purpose:

http://www.effbot.org/librarybook/parser.htm

instead of manipulating the parse tree, you can use the AST to identify
the variables, create a dictionary with the current values, and use exec
or eval to evaluate it.

or you can use a regular expression to dig out all variable names.

or you can compile the expression and analyze the code object to find
the variable names, and use the dictionary/exec approach:
>>> expr = compile("distance = x**2 + y**2", "", "exec")
>>> expr.co_varnames ('distance',)
>>> list(set(expr.co_names) - set(expr.co_varnames)) ['y', 'x']
>>> context = {"x": 10, "y": 20}
>>> exec expr in context
>>> context["distance"]
500

(the regular expression approach is probably the only one that's
guaranteed to work on all python implementations)

</F>
 
B

beza1e1

Thank you! this "compile/exec in context" is the thing i wanted.

It is not that performant i think. But it should ease the prototyping.
 
F

Fredrik Lundh

beza1e1 said:
Thank you! this "compile/exec in context" is the thing i wanted.

It is not that performant i think.

it's about as fast as it can get, as long as you only call compile when
the expressions change. (the example didn't show it, but the "expr"
code object can of course be reused with different contexts)

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top