On 20 Jul., 09:52, John Ladasky <[EMAIL PROTECTED]> wrote: > Why not do genetic programming directly on Python code? Maybe my code > tree data structure is redundant to something which already exists? > But apparently Python's "_ast" module offers only one-way access -- it > will generate an AST from a piece of code, but you can't modify an > AST, and turn it back into executable code?
Why not? You can compile ASTs. Another option is to use EasyExtend http://www.fiber-space.de/EasyExtend/doc/EE.html which is a bit heavyweight though without prior knowledge of the framework. EE provides some generic functions over languages like parse/unparse. Python is just a special case. So you can do the following from EasyExtend.langlets.zero.langlet import parse, unparse src = """ if disc(a) < c: b = f1(a) else: b = f2(a) """ parse(src) # yields a parse tree unparse(parse(src)) # yields the source code of the parse tree Here `zero` means Python which is just the trivial/featureless langlet of the system or some kind of embedding. For meshing fragments together on random one can use cst.py. For each rule in Pythons grammar cst.py implements a corresponding function that produces the parse tree accordingly. So if there is a rule test: or_test ['if' or_test 'else' test] | lambdef a corresponding function test(*args) exists that produces a parse tree from components that were built using or_test(), test() or lambdef(). chaining these functions is just like building s-expr. > I would definitely need > this latter feature. > > ALTERNATELY -- and I don't mention this to start a flame war -- in > pondering this problem I've noticed the frightening fact that Lisp > seems set up to handle genetic programming by design. Definitely. But this is nothing new. Lisp was the original language used by John Koza to implement GP. > The s- > expression syntax IS essentially a parse tree. Now, I've spent a few > hours with Lisp so far, and I can't make it do much of anything -- but > I DO see how Lisp could be helpful. Still, I'm reluctant to pursue a > language I don't know, and which I'm finding much harder to grasp than > any language I've tried before. You can write a primitive s-expr evaluator/manipulator using Pythons overloading capabilities. But this way you will just produce s-expr and not Python functions. Kay -- http://mail.python.org/mailman/listinfo/python-list