Edit Python code programmatically

A

Alex

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
 
S

Steven D'Aprano

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

Ben Finney

Alex said:
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.sourceforge.net/>, which
presumably needs to do some of this.
 
A

Alex

Steven said:
I'm not even sure such a library exists.
Yes they exist, that field is called "code-generation", "generative
programming" 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top