Python code generator

mvanveen picture mvanveen · Oct 25, 2011 · Viewed 10.3k times · Source

I want to be able to perform code generation of python given an AST description.

I've done static analysis of C and built AST visitors in python, so I feel relatively comfortable manipulating a syntax tree, but I've never attempted code generation before and am trying to determine the best practice for generating python code.

Specifically, I'd love pointers on how automatic code generation is typically done, or any pointers to libraries targetting python which could make this task simpler.

My end goal is to attempt something similar to csmith or a tool to make python code compliant with PEP8.

Answer

Eli Bendersky picture Eli Bendersky · Oct 25, 2011

You may want to take a look at the 2to3 tool, developed by the Python code devs to automatically convert Python 2 code to Python 3 code. The tool first parses the code to a tree, and then spits out "fixed" Python 3 code from that tree.

This may be a good place to start because this is an "official" Python tool endorsed by the core developers, and part of the recommended Python 2 to 3 migration path.

Alternatively, check out the codegen.py module, which generates Python code back from Python's ast.

See also this SO question, which may be relevant to yours (I'm not marking it a duplicate because I'm not sure the scopes of the questions overlap 100%)