views:

84

answers:

1

I'd like to write a code generation tool that will allow me to create sourcefiles for dynamically generated classes. I can create the class and use it in code, but it would be nice to have a sourcefile both for documentation and to allow something to import.

Does such a thing exist? I've seen sourcecodegen, but I'd rather avoid messing with the ast trees as they're not portable.

+2  A: 

I'm not aware of any off-the-shelf library, but have a look at the Python templating engines Mako and Jinja2. They can both generate Python source behind the scenes (they convert text templates to Python code and then to Python bytecode).

Vinay Sajip
+1: Just use a template engine. It works well. That's what we do.
S.Lott