Edit Python code programmatically

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

    Edit Python code programmatically

    Which library could you recommend to perform simple editing of Python
    code (from Python program)? For example, open *.py file, find specific
    function definition, add another function call inside, find existing
    call and change parameter value, etc.

    What I'm trying to implement isn't a real visual programming tool, but
    some code-generation is necessary. For now I think I can generate Python
    syntax manually (like any text file), but it can become more complicated
    in future (like partially implementing code-generation library), plus
    there'll always be possibility of corrupting files and losing data (or
    having to recover valid Python syntax manually) due to coding mistake.

    Thanks

  • Steven D'Aprano

    #2
    Re: Edit Python code programmaticall y

    On Sat, 09 Feb 2008 14:38:29 +0300, Alex wrote:
    Which library could you recommend to perform simple editing of Python
    code (from Python program)?
    I'm not even sure such a library exists.

    For example, open *.py file, find specific
    function definition, add another function call inside, find existing
    call and change parameter value, etc.
    Why do you want to do that? I'm not sure what you're trying to
    accomplish. Code refactoring? I imagine that's probably best done with
    your text editor: at best, your editor will have dedicated refactoring
    tools, and at worst, it will have global search and replace.



    --
    Steven

    Comment

    • Ben Finney

      #3
      Re: Edit Python code programmaticall y

      Alex <noname9968@gma il.comwrites:
      Which library could you recommend to perform simple editing of
      Python code (from Python program)? For example, open *.py file, find
      specific function definition, add another function call inside, find
      existing call and change parameter value, etc.
      You might want to look into the Python refactoring tool "Bicycle
      Repair Man" <URL:http://bicyclerepair.s ourceforge.net/>, which
      presumably needs to do some of this.

      --
      \ "I washed a sock. Then I put it in the dryer. When I took it |
      `\ out, it was gone." -- Steven Wright |
      _o__) |
      Ben Finney

      Comment

      • Alex

        #4
        Re: Edit Python code programmaticall y

        Steven D'Aprano wrote:
        On Sat, 09 Feb 2008 14:38:29 +0300, Alex wrote:
        >
        >
        >Which library could you recommend to perform simple editing of Python
        >code (from Python program)?
        >>
        >
        I'm not even sure such a library exists.
        >
        Yes they exist, that field is called "code-generation", "generative
        programming" etc.
        >
        >
        >For example, open *.py file, find specific
        >function definition, add another function call inside, find existing
        >call and change parameter value, etc.
        >>
        >
        Why do you want to do that? I'm not sure what you're trying to
        accomplish. Code refactoring? I imagine that's probably best done with
        your text editor: at best, your editor will have dedicated refactoring
        tools, and at worst, it will have global search and replace.
        I don't feel like describing all ideas - it's nothing really intersting
        anyway, just a learning project (to get to know language features), but
        obviously it's not for regular programming - I know text editors can do
        that just fine. Simply in some situation I think instead of generating
        data I'd better generate some code.

        Comment

        Working...