Manipulating Python Source

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wilson

    Manipulating Python Source

    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.p arse(test.py)
    >>parsed.getCla sses()
    ["MyClass", "MyClass2"]
    >>parsed.docStr ing
    " This is an example "
    >>parsed.remove Class("MyClass2 ")
    >>parsed.getCla sses()
    ["MyClass"]
    >>parsed.MyClas s.getFuncs()
    ["some_func"]
    >>parsed.MyClas s.some_func.add Param("baz")
    >>parsed.printS ource()
    """ 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
  • Kay Schluehr

    #2
    Re: Manipulating Python Source

    On 12 Aug., 16:35, Wilson <PaulAlexWil... @gmail.comwrote :
    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.



    Comment

    • Bruno Desthuilliers

      #3
      Re: Manipulating Python Source

      Kay Schluehr a écrit :
      On 12 Aug., 16:35, Wilson <PaulAlexWil... @gmail.comwrote :
      >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.
      >


      >
      And eventually:
      Source code: Lib/inspect.py The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, ...

      Comment

      • Wilson

        #4
        Re: Manipulating Python Source

        On 12 Aug, 15:53, Bruno Desthuilliers <bruno.
        42.desthuilli.. .@websiteburo.i nvalidwrote:
        Kay Schluehr a écrit :On 12 Aug., 16:35,Wilson<Pa ulAlexWil...@gm ail.comwrote:
        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.
        >>
        And eventually:http://docs.python.org/lib/module-inspect.html
        Very interesting! Thanks for the hints.

        Comment

        Working...