Python Forum

Full Version: looking for code that substitues parts of python code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am looking for code, preferably organized as a function, that given a big string containing valid Python code, can substitute one string for another, and not make the change inside comments, or inside string literals of any type, anywhere. this could be used to change variable names and function/method calls in the given code string. it must support byte literals in the substituted string of code, but supporting bytes in the call arguments is not required although is otherwise a plus.

a nice alternative is code that can take a string of valid Python code and generate parts representing pieces of the code as it would be parsed, each yielded as a 2-tuple or 2-list with a type code and the string part. all these strings, joined together, will be the same as the original code given to it. then i would be trivial to do things like substitute all variable names not in literals or comments.