[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 12:32:00 PM

2008/2/9, Alex <noname9968@gmail.com>:
> Guilherme Polo wrote:
> > 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.
>
> Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
> 3rd party library. Simply such wasn't mentioned in library review and
> tutorials, so I didn't know of it. What's the module's name?

inspect is a module, inspect is the name. It is not a module for
editing Python code per se, but it will help with the other part.

>
> >> 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.
>
> No, the code by itself is the goal.
>
> --
>
> http://mail.python.org/mailman/listinfo/p...
>


--
-- Guilherme H. Polo Goncalves
7 Answers

Arnaud Delobelle

2/9/2008 1:10:00 PM

0

On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
> 2008/2/9, Alex <noname9...@gmail.com>:
>
> > Guilherme Polo 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.
>
> > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
> >  3rd party library. Simply such wasn't mentioned in library review and
> >  tutorials, so I didn't know of it. What's the module's name?
>
> inspect is a module, inspect is the name. It is not a module for
> editing Python code per se, but it will help with the other part.

I don't think the OP wants to edit python code *objects*, rather he
wants to edit python *source* code programmatically. Inspect is not
the tool for this.

--
Arnaud

Guilherme Polo

2/9/2008 1:17:00 PM

0

2008/2/9, Arnaud Delobelle <arnodel@googlemail.com>:
> On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
> > 2008/2/9, Alex <noname9...@gmail.com>:
> >
> > > Guilherme Polo 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.
> >
> > > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
> > > 3rd party library. Simply such wasn't mentioned in library review and
> > > tutorials, so I didn't know of it. What's the module's name?
> >
>
> > inspect is a module, inspect is the name. It is not a module for
> > editing Python code per se, but it will help with the other part.
>
>
> I don't think the OP wants to edit python code *objects*, rather he
> wants to edit python *source* code programmatically. Inspect is not
> the tool for this.

I didn't tell him to use inspect to edit python code, I said it was
useful for the other part. The other part, as he mentioned on his
email is: "find specific
function definition, add another function call inside, find existing
call".

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


--
-- Guilherme H. Polo Goncalves

Steve Holden

2/9/2008 1:35:00 PM

0

Guilherme Polo wrote:
> 2008/2/9, Arnaud Delobelle <arnodel@googlemail.com>:
>> On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
>> > 2008/2/9, Alex <noname9...@gmail.com>:
>> >
>> > > Guilherme Polo 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.
>> >
>> > > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
>> > > 3rd party library. Simply such wasn't mentioned in library review and
>> > > tutorials, so I didn't know of it. What's the module's name?
>> >
>>
>>> inspect is a module, inspect is the name. It is not a module for
>> > editing Python code per se, but it will help with the other part.
>>
>>
>> I don't think the OP wants to edit python code *objects*, rather he
>> wants to edit python *source* code programmatically. Inspect is not
>> the tool for this.
>
> I didn't tell him to use inspect to edit python code, I said it was
> useful for the other part. The other part, as he mentioned on his
> email is: "find specific
> function definition, add another function call inside, find existing
> call".
>
Yes, the OP was a little ambiguous. So I ought to mention [sigh, these
name] bicycle repair man

http://bicyclerepair.source...

It provides refactoring functionality for Python, so may have some bits
in it that will help (though I don't know how up to date it is).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Unnamed One

2/9/2008 1:46:00 PM

0

Guilherme Polo wrote:
> 2008/2/9, Arnaud Delobelle <arnodel@googlemail.com>:
>
>> On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
>> > 2008/2/9, Alex <noname9...@gmail.com>:
>> >
>> > > Guilherme Polo 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.
>> >
>> > > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
>> > > 3rd party library. Simply such wasn't mentioned in library review and
>> > > tutorials, so I didn't know of it. What's the module's name?
>> >
>>
>>
>>> inspect is a module, inspect is the name. It is not a module for
>>>
>> > editing Python code per se, but it will help with the other part.
>>
>>
>> I don't think the OP wants to edit python code *objects*, rather he
>> wants to edit python *source* code programmatically. Inspect is not
>> the tool for this.
>>
>
> I didn't tell him to use inspect to edit python code, I said it was
> useful for the other part. The other part, as he mentioned on his
> email is: "find specific
> function definition, add another function call inside, find existing
> call".
Sorry but I said "in *.py file", meaning that file isn't executed to
edit objects in memory. It's instead saved in modified form, possibly to
be edited by user. Guess it's a common task for visual GUI editors and
any visual programming tools.

Guilherme Polo

2/9/2008 2:22:00 PM

0

2008/2/9, Alex <noname9968@gmail.com>:
> Guilherme Polo wrote:
> > 2008/2/9, Arnaud Delobelle <arnodel@googlemail.com>:
> >
> >> On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
> >> > 2008/2/9, Alex <noname9...@gmail.com>:
> >> >
> >> > > Guilherme Polo 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.
> >> >
> >> > > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
> >> > > 3rd party library. Simply such wasn't mentioned in library review and
> >> > > tutorials, so I didn't know of it. What's the module's name?
> >> >
> >>
> >>
> >>> inspect is a module, inspect is the name. It is not a module for
> >>>
> >> > editing Python code per se, but it will help with the other part.
> >>
> >>
> >> I don't think the OP wants to edit python code *objects*, rather he
> >> wants to edit python *source* code programmatically. Inspect is not
> >> the tool for this.
> >>
> >
> > I didn't tell him to use inspect to edit python code, I said it was
> > useful for the other part. The other part, as he mentioned on his
> > email is: "find specific
> > function definition, add another function call inside, find existing
> > call".
>
> Sorry but I said "in *.py file", meaning that file isn't executed to
> edit objects in memory. It's instead saved in modified form, possibly to
> be edited by user. Guess it's a common task for visual GUI editors and
> any visual programming tools.
>

By visual GUI editors I will assume GUI designer tools. These tend to
not generate direct python code, glade-2 used to but glade-3 doesn't
anymore. Other tools like XRCed generates xrc, wxGlade has an option
to generate .xrc too, Qt Designer generates .ui and .qrc, Glade-3
generates .glade file, Gazpacho generates .glade, or a gazpacho format
or gtkbuilder format. In all these, it is recommended to use something
to work with the generated code, like libglade, wx.xrc and PyQt has
tools to convert .ui and .qrc to python modules but they don't affect
your custom code (it is also possible to load .ui using uic module).

With this we come back to my first email, where I told you it is not
recommended to generate direct python code, especially if you are
doing the kind of things you just mentioned. If you still want to
generate python code, from some other source, inspect can be helpful.

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


--
-- Guilherme H. Polo Goncalves

Unnamed One

2/9/2008 3:55:00 PM

0

Guilherme Polo wrote:
> 2008/2/9, Alex <noname9968@gmail.com>:
>
>> Guilherme Polo wrote:
>> > 2008/2/9, Arnaud Delobelle <arnodel@googlemail.com>:
>> >
>> >> On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
>> >> > 2008/2/9, Alex <noname9...@gmail.com>:
>> >> >
>> >> > > Guilherme Polo 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.
>> >> >
>> >> > > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
>> >> > > 3rd party library. Simply such wasn't mentioned in library review and
>> >> > > tutorials, so I didn't know of it. What's the module's name?
>> >> >
>> >>
>> >>
>> >>> inspect is a module, inspect is the name. It is not a module for
>> >>>
>> >> > editing Python code per se, but it will help with the other part.
>> >>
>> >>
>> >> I don't think the OP wants to edit python code *objects*, rather he
>> >> wants to edit python *source* code programmatically. Inspect is not
>> >> the tool for this.
>> >>
>> >
>> > I didn't tell him to use inspect to edit python code, I said it was
>> > useful for the other part. The other part, as he mentioned on his
>> > email is: "find specific
>> > function definition, add another function call inside, find existing
>> > call".
>>
>> Sorry but I said "in *.py file", meaning that file isn't executed to
>> edit objects in memory. It's instead saved in modified form, possibly to
>> be edited by user. Guess it's a common task for visual GUI editors and
>> any visual programming tools.
>>
>>
>
> By visual GUI editors I will assume GUI designer tools. These tend to
> not generate direct python code, glade-2 used to but glade-3 doesn't
> anymore. Other tools like XRCed generates xrc, wxGlade has an option
> to generate .xrc too, Qt Designer generates .ui and .qrc, Glade-3
> generates .glade file, Gazpacho generates .glade, or a gazpacho format
> or gtkbuilder format. In all these, it is recommended to use something
> to work with the generated code, like libglade, wx.xrc and PyQt has
> tools to convert .ui and .qrc to python modules but they don't affect
> your custom code (it is also possible to load .ui using uic module).
>
> With this we come back to my first email, where I told you it is not
> recommended to generate direct python code, especially if you are
> doing the kind of things you just mentioned. If you still want to
> generate python code, from some other source, inspect can be helpful.
Thank you for detailed reply... but I still want to generate python
code. By the way, Python Package Index references code generators able
to generate Python code
(http://pypi.python.org/pypi?:action=browse&show=all...), which I'll
inspect later (especially their ability to *edit* code). Inspect might
be useful too.

Guilherme Polo

2/9/2008 4:10:00 PM

0

2008/2/9, Alex <noname9968@gmail.com>:
> Guilherme Polo wrote:
> > 2008/2/9, Alex <noname9968@gmail.com>:
> >
> >> Guilherme Polo wrote:
> >> > 2008/2/9, Arnaud Delobelle <arnodel@googlemail.com>:
> >> >
> >> >> On Feb 9, 12:32 pm, "Guilherme Polo" <ggp...@gmail.com> wrote:
> >> >> > 2008/2/9, Alex <noname9...@gmail.com>:
> >> >> >
> >> >> > > Guilherme Polo 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.
> >> >> >
> >> >> > > Yes, I forgot to mention - I'm new to Python. I didn't necessary mention
> >> >> > > 3rd party library. Simply such wasn't mentioned in library review and
> >> >> > > tutorials, so I didn't know of it. What's the module's name?
> >> >> >
> >> >>
> >> >>
> >> >>> inspect is a module, inspect is the name. It is not a module for
> >> >>>
> >> >> > editing Python code per se, but it will help with the other part.
> >> >>
> >> >>
> >> >> I don't think the OP wants to edit python code *objects*, rather he
> >> >> wants to edit python *source* code programmatically. Inspect is not
> >> >> the tool for this.
> >> >>
> >> >
> >> > I didn't tell him to use inspect to edit python code, I said it was
> >> > useful for the other part. The other part, as he mentioned on his
> >> > email is: "find specific
> >> > function definition, add another function call inside, find existing
> >> > call".
> >>
> >> Sorry but I said "in *.py file", meaning that file isn't executed to
> >> edit objects in memory. It's instead saved in modified form, possibly to
> >> be edited by user. Guess it's a common task for visual GUI editors and
> >> any visual programming tools.
> >>
> >>
> >
> > By visual GUI editors I will assume GUI designer tools. These tend to
> > not generate direct python code, glade-2 used to but glade-3 doesn't
> > anymore. Other tools like XRCed generates xrc, wxGlade has an option
> > to generate .xrc too, Qt Designer generates .ui and .qrc, Glade-3
> > generates .glade file, Gazpacho generates .glade, or a gazpacho format
> > or gtkbuilder format. In all these, it is recommended to use something
> > to work with the generated code, like libglade, wx.xrc and PyQt has
> > tools to convert .ui and .qrc to python modules but they don't affect
> > your custom code (it is also possible to load .ui using uic module).
> >
> > With this we come back to my first email, where I told you it is not
> > recommended to generate direct python code, especially if you are
> > doing the kind of things you just mentioned. If you still want to
> > generate python code, from some other source, inspect can be helpful.
>
> Thank you for detailed reply... but I still want to generate python
> code. By the way, Python Package Index references code generators able
> to generate Python code
> (http://pypi.python.org/pypi?:action=browse&show=all...), which I'll
> inspect later (especially their ability to *edit* code). Inspect might
> be useful too.
>

Depending on what you want to generate, it may be good to generate
python code I won't try to change your mind on this. One of those code
generators there is mine, but it doesn't have an option to edit the
code neither it is in active development at all (got bored), and it
generates direct python code and other language to complement the
generated python code. But if I was going to add a possible code
editing/updating I would probably use inspect for the reasons
mentioned before.

My only intention with this email was to tell that it is pretty hard
to know what you are thinking, so it gets hard to point tools to use.
If you advance in your code generation tool, you may have a better
idea of what you want then you will get better answers too.

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


--
-- Guilherme H. Polo Goncalves