Manipulating Python Source

Wilson PaulAlexWilson at gmail.com
Tue Aug 12 10:35:08 EDT 2008


Hi,

I'm wondering if there are any tools available or simple methods for
taking a python source file and parsing into some hierarchical format,
like the ConfigParser. I'd like to be able to do something like the
following:

test.py:
-------------------------------------------------------

""" This is an example """

class MyClass(ParentA, ParentB):
    def some_func(self, foo, bar):
         self.baz = "batman"

class MyClass2(object):
    """ This is an example class """
    def __init__(self):
         self.a = "Blablabla"

And from the interpreter:

>>> import magicalParser
>>> parsed = magicalParser.parse(test.py)
>>> parsed.getClasses()
["MyClass", "MyClass2"]
>>> parsed.docString
" This is an example "
>>> parsed.removeClass("MyClass2")
>>> parsed.getClasses()
["MyClass"]
>>> parsed.MyClass.getFuncs()
["some_func"]
>>> parsed.MyClass.some_func.addParam("baz")
>>> parsed.printSource()
""" This is an example """

class MyClass(ParentA, ParentB):
    def some_func(self, foo, bar, baz):
         self.baz = "batman"
>>> exit()

Or something that would yield the above effect. Any ideas?

Thanks,
Paul



More information about the Python-list mailing list