Entry
Is there a pretty printer which takes AST objects ( or tuples ) and outputs source code?
Sep 12th, 2000 08:33
Steve Holden, Joe Hosteny,
Some explanation of what "AST objects" are would be helpful...
The pprint module is your friend here. It will produce readable
object representations, nicely indented if so requested. The
printable output will be usable Python code.
You should also note that in general the repr() function will try
to produce a Python representation of an object which is legal
input to the interpreter -- this alone may be enough for simple
objects.
>>> tuple = (1, 'banana', 'orange', 3, 4)
>>> repr(tuple)
"(1, 'banana', 'orange', 3, 4)"
>>> import pprint
>>> pp = pprint.PrettyPrinter()
>>> pp.pformat(tuple)
"(1, 'banana', 'orange', 3, 4)"
>>> pp = pprint.PrettyPrinter(width=2, indent=4)
>>> pp.pformat(tuple)
"( 1,\012 'banana',\012 'orange',\012 3,\012 4)"