Perhaps I'm not using the proper term, but I'm looking to take a block of Python code (in Python), get the token tree for the code, make some kind of modification, and re-assemble it to Python.
For instance, consider this block of code:
def x(y):
b = 2
if y == b:
foo(y)
I would like to be able to take this and programmatically do this:
def x(y):
b = 2
if y == b:
bar(y)
I can't imagine that there's not a library that does something like this. Thanks in advance.
EDIT
Perhaps I wasn't entirely clear. I'm looking for a tool to read and manipulate arbitrary code, not code that I'm writing. I'd like to be able to modify code on-the-fly. The project I'm working on is a test app: it uses the Netflix philosophy to try to randomly break the functionality of an app in as many ways as it can, running the test suite each time. When the tests don't fail, there's an indication that there's either a gap in code coverage and/or the code is dead.