Manipulating Python Source

Discussion in 'Python' started by Wilson, Aug 12, 2008.

  1. Wilson

    Wilson Guest

    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
    Wilson, Aug 12, 2008
    #1
    1. Advertising

  2. Wilson

    Kay Schluehr Guest

    Kay Schluehr, Aug 12, 2008
    #2
    1. Advertising

  3. Bruno Desthuilliers, Aug 12, 2008
    #3
  4. Wilson

    Wilson Guest

    On 12 Aug, 15:53, Bruno Desthuilliers <bruno.
    > wrote:
    > Kay Schluehr a écrit :> On 12 Aug., 16:35,Wilson<> wrote:
    > >> 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.

    >
    > >http://docs.python.org/lib/module-parser.html
    > >http://docs.python.org/lib/compiler.html

    >
    > And eventually:http://docs.python.org/lib/module-inspect.html


    Very interesting! Thanks for the hints.
    Wilson, Aug 13, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Wilson

    manipulating PowerPoint with Python?

    Greg Wilson, Jun 27, 2003, in forum: Python
    Replies:
    1
    Views:
    1,683
    Aurélien Géron
    Jun 27, 2003
  2. Josh
    Replies:
    1
    Views:
    297
    Terry Reedy
    Dec 6, 2003
  3. Kenneth McDonald
    Replies:
    1
    Views:
    604
    Jeff Epler
    Jun 2, 2004
  4. Peng Yu
    Replies:
    5
    Views:
    1,057
    Peng Yu
    Oct 19, 2009
  5. Replies:
    59
    Views:
    666
    Jorgen Grahn
    Jan 7, 2014
Loading...

Share This Page