Manipulating Python Source

Patrol Sun sunp1028 at gmail.com
Tue Aug 12 21:42:55 EDT 2008


This idea recalls Spring's Bean binding to me. Good idea. Is there similar
thing?

2008/8/12 Wilson <PaulAlexWilson at gmail.com>

> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080813/b65096f7/attachment-0001.html>


More information about the Python-list mailing list