Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Best Practices: Abstract Syntax Trees

Modifying Abstract Syntax Trees

I would like to be able to build and modify an ast and then optionally write it out as python byte code for execution later without overhead.

I have been hacking around with the ast docs for python3.0 and python2.6, but I can't seem to find any good sources on best practices for this type of code.

Question

What are some best practices and guidelines for modifying abstract syntax trees in python?

[edit]

Unknown states that byteplay is a good example of such a library.

Also, benford cites GeniuSQL which uses abstract syntax trees to transform python code to SQL.

like image 944
brad Avatar asked May 26 '09 18:05

brad


2 Answers

Other than the manual and the source code, you are on your own. This subject and python bytecode are very undocumented.

Alternatively you could try using this python bytecode library which I have heard good thing about but haven't tried it yet:

http://code.google.com/p/byteplay/

like image 142
Unknown Avatar answered Nov 07 '22 02:11

Unknown


I think geniusql is doing something along those lines to translate an ast into sql... There was a talk on it but I can't find it - and I'm not allowed to link anyway :-(

like image 26
Ben Ford Avatar answered Nov 07 '22 03:11

Ben Ford