[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Edit Python code programmatically

Unnamed One

2/9/2008 11:38:00 AM

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

3 Answers

Steven D'Aprano

2/9/2008 12:02:00 PM

0

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

Ben Finney

2/9/2008 12:08:00 PM

0

Alex <noname9968@gmail.com> writes:

> 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.sourceforg..., 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

Unnamed One

2/9/2008 12:18:00 PM

0

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.