Parsing and Editing Source

Paul Wilson paulalexwilson at gmail.com
Fri Aug 15 10:21:49 EDT 2008


Hi all,

I'd like to be able to do the following to a python source file
programmatically:
 * Read in a source file
 * Add/Remove/Edit Classes, methods, functions
 * Add/Remove/Edit Decorators
 * List the Classes
 * List the imported modules
 * List the functions
 * List methods of classes

And then save out the result back to the original file (or elsewhere).

I've begun by using the tokenize module to generate a token-tuple list
and am building datastructures around it that enable the above
methods. I'm find that I'm getting a little caught up in the details
and thought I'd step back and ask if there's a more elegant way to
approach this, or if anyone knows a library that could assist.

So far, I've got code that generates a line number to token-tuple list
dictionary, and am working on a datastructure describing where the
classes begin and end, indexed by their name, such that they can be
later modified.

Any thoughts?
Thanks,
Paul



More information about the Python-list mailing list