Compiling snippets of python code

Basil Brush news.btinternet.com at erik.feegles.net
Fri May 8 12:58:49 CEST 2009


I need some advice on compiling snippets of python source at runtime. So
perhaps some python experts can point the way.

I am rewriting an old Java app in python. It tries to find solutions to
computational problems based on the idea of genetic evolution. The Java
version just used an array of bytes for the genomes. These byte
sequences were bytecode which told a rudimentary VM (virtual machine)
which maths functions to call. It used another array as a small chunk of
virtual memory for doing the calculations.

Having seen the docs on python's compile module, I'm thinking of doing
it differently in the new python version. My aim is to have the genomes
made up of a sequence of python source snippets, with each snippet to be
randomly generated, compiled, and executed at runtime. The advantage of
this is that it gives me greater flexibility and I can change what
actions the genomes can do without having to add new functions to my VM.

But there's some crucial points. When the snippet is executed I would
need to pass in a list or array, and I need the list back again with any
changes that were made; this is the virtual memory and how I would get
the results back, and is also how the snippet would get data from a
previous snippet in a sequence. How would I do this? Also, there is a
database of input data. Rather than pass all that data to the snippet
I'd rather have functions in my module which the snippet could call to
get data. So what's the scope of an executing piece of code? Can it
access my modules and functions?

My linux system has python 2.5 and I noticed that the docs say the
compile module has been removed from python 3.0. So I'm concerned about
whether my app would work in 3.0 if I upgraded. Does anyone know if they
moved the compiler to another module or simply removed it entirely?



More information about the Python-list mailing list