Saturday, January 30, 2010

Python AST Pretty Printer

Description

This is a python module that provides a dump function, identical to ast.dump, except the returned string has newlines and indentation.

Download: astpp.py

Example

import ast
import astpp

tree = ast.parse(
"""
print "Hello World!"
s = "I'm a string!"
print s
""")
print astpp.dump(tree)
Which prints
Module(body=[
    Print(dest=None, values=[
        Str(s='Hello World!'),
      ], nl=True),
    Assign(targets=[
        Name(id='s', ctx=Store()),
      ], value=Str(s="I'm a string!")),
    Print(dest=None, values=[
        Name(id='s', ctx=Load()),
      ], nl=True),
  ])

1 comment:

  1. Thank you! It is exactly what is missing in module AST.

    ReplyDelete