0

Would the most efficient way-and I know it's not very efficient, but I honestly can't find any better way-to manipulate a Python (.py) file, to add/subtract/append code, be to use the basic file I/O module included in Python?

For an example:

obj = open('Codemanipulationtest.py', 'w+')
obj.write("print 'This shows you can do basic I/O?'")
obj.close()

Will manipulate a file I have, named "codemanipulationtest.py", and add to it a print statement. Is this something that can be worked upon or are there any easier or more safe/efficient methods for manipulating/creating new python code?

I've read over this: Parse a .py file, read the AST, modify it, then write back the modified source code

And honestly it seems like the I/O method is easier. I am kind of newbish to Python so I may just be acting stupid.....thanks in advance for any responses.

Edit

The point of it all was simply to play around with the effects playing around with the code. I was thinking of hooking up whatever I end up using to some sort of learning algorithm and seeing how well it could generate little bits of code at a time, and seeing where it could go from there....

Community
  • 1
  • 1
Aaron Tp
  • 353
  • 1
  • 3
  • 12
  • 1
    It depends entirely what you want to do. Tell us exactly what you want to use it for. Either approach may be suitable for a given task. – Joe Jul 05 '12 at 18:47
  • What are you trying to do? You want to write programs that can write programs, toward what end? – steveha Jul 05 '12 at 18:48
  • Just a note, the `'w+'` truncates the existing file. You may want to use the `'a'` (i.e. append) mode. – pepr Jul 05 '12 at 18:51
  • Yeah I noticed that w+ truncates it shortly after. Thanks – Aaron Tp Jul 05 '12 at 18:52

2 Answers2

1

It depends on what you'll be doing with the code you're generating. You have a few options, each more advanced than the last.

  • Create a file and import it
  • Create a string and exec it
  • Write code to create classes (or modules) on the fly directly rather than as text, inserting whatever functions you need into them
  • Generate Python bytecode directly and execute that!

If you are writing code that will be used and modified by other programmers, then the first approach is probably best. Otherwise I recommend the third for most use cases. The last is only to masochists and former assembly language programmers.

If you want to modify existing Python source code, you can sometimes get away with doing simple modifications with basic search-and-replace, especially if you know something about the source file you're working with, but a better approach is the ast module. This gives you an abstract representation of the Python source that you can modify and then compile directly into Python objects.

kindall
  • 178,883
  • 35
  • 278
  • 309
1

To go about with generating the code I would break it out into various classes, IF class, FOR class, and so on. Then you can use the output wherein each class has a to_str() method that you can call in turn.

statements = [ ... ]

obj = open( "some.py", "w+" )

for s in statements:
    obj.write( s.to_str() )

obj.close()

This way you can extend your project easily and it will be more understandable and flexible. And, it keeps with the use of the simple write method that you wanted.

Depending on the learning algorithm this break out of the various classes can lead quite well into a sort of pseudo genetic algorithm for code. You can encode the genome as a sequence of statements and then you just have to find a way to go about passing parameters to each statement if they are required and such.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
sean
  • 3,955
  • 21
  • 28
  • So I'm assuming there *isn't* a library for this already, then? – Aaron Tp Jul 05 '12 at 19:15
  • For what you want to do I do not think there is a specific library for it. So, you would have to write the code generation portion yourself. The machine learning part there is PyBrain that you can look into that can prevent you from writing the learning portion. – sean Jul 05 '12 at 19:21