Changing an AST

2005-10-10 Thread 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

Re: Changing an AST

2005-10-10 Thread Fredrik Lundh
beza1e1 [EMAIL PROTECTED] wrote: 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

Re: Changing an AST

2005-10-10 Thread Leif K-Brooks
beza1e1 wrote: Is it possible compiler.parse a statement, then change and then execute/resolve it? This should work: from compiler.pycodegen import ModuleCodeGenerator from compiler.misc import set_filename from compiler import parse tree = parse('foo = 42') set_filename('foo',

Re: Changing an AST

2005-10-10 Thread 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. -- http://mail.python.org/mailman/listinfo/python-list

Re: Changing an AST

2005-10-10 Thread Fredrik Lundh
beza1e1 wrote: 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