Quickstart: Unparse and dump source code
mainThe following example demonstrates how to use inspect to get the source of a module, parse it into an AST, and then use astunparse to both reconstruct the source and print a pretty-printed version of the tree.
import inspect
import ast
import astunparse
# get back the source code
astunparse.unparse(ast.parse(inspect.getsource(ast)))
# get a pretty-printed dump of the AST
astunparse.dump(ast.parse(inspect.getsource(ast)))