[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Re: Edit Python code programmatically

Guilherme Polo

2/9/2008 11:47:00 AM

2008/2/9, Alex <noname9968@gmail.com>:
> 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 are after inspect, it is included with python.

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

Generating code like this is always dangerous. Maybe you could
generate some other kind of file, then use some library or build one,
to operator over this file.

> Thanks
>
>
> --
> http://mail.python.org/mailman/listinfo/p...
>


--
-- Guilherme H. Polo Goncalves
2 Answers

Benjamin

2/9/2008 5:55:00 PM

0

On Feb 9, 5:47 am, "Guilherme Polo" <ggp...@gmail.com> wrote:
> 2008/2/9, Alex <noname9...@gmail.com>:
>
> > 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 are after inspect, it is included with python.
>
> > 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.
>
> Generating code like this is always dangerous. Maybe you could
> generate some other kind of file, then use some library or build one,
> to operator over this file.
It can be quite useful, though, say when you're writing interfaces for
several languages and you want to follow DRY.
>
> > Thanks
>
> > --
> > http://mail.python.org/mailman/listinfo/p...
>
> --
> -- Guilherme H. Polo Goncalves

greg

2/11/2008 5:15:00 AM

0

Guilherme Polo wrote:

> Generating code like this is always dangerous.

Generating code isn't dangerous in itself. What's dangerous
is mixing hand-edited and generated code in the same file.
There's too much opportunity for hand edits to get wiped
out by subsequent automatic edits or vice versa.

If you really want to generate code, find a way of
separating out the generated code into another .py
file that never needs to be edited by hand.

--
Greg