Skip to content

Exploring the Python AST

simchuck edited this page Mar 12, 2019 · 8 revisions

wiki home | installation | contributing | documentation

Simple Tools for Exploring the Python AST During Development of pandas-vet

Option 1: Using the standard library

The ast module has a .dump() function which returns a formatted string dump of the AST tree node. Default is to show annotations, which helps in identifying the attribute chain.

ast.dump(node, annotate_fields=True, include_attributes=False)

Example:

import ast

code = """
print("Hello World!")
s = "I'm a string!"
print(s)
"""
tree = ast.parse(code)
print(ast.dump(tree))

Output:

'Module(body=[Expr(value=Call(func=Name(id=\'print\', ctx=Load()), args=[Str(s=\'Hello World!\')], keywords=[])), Assign(targets=[Name(id=\'s\', ctx=Store())], value=Str(s="I\'m a string!")), Expr(value=Call(func=Name(id=\'print\', ctx=Load()), args=[Name(id=\'s\', ctx=Load())], keywords=[]))])'

Option 2: Using the astpp module from greentreesnakes

The pretty-print module mentioned in the above post (available at https://bitbucket.org/takluyver/greentreesnakes/src/default/astpp.py) includes a astpp.dump() function which produces essentially the same output, but presented in a tree format that provides more visual clarity.

astpp.dump(node, annotate_fields=True, include_attributes=False, indent=' ')

Example:

import ast
import astpp
 
code = """
print("Hello World!")
s = "I'm a string!"
print(s)
"""
tree = ast.parse(code)
print(astpp.dump(tree))

Output:

Module(body=[
    Expr(value=Call(func=Name(id='print', ctx=Load()), args=[
        Str(s='Hello World!'),
      ], keywords=[])),
    Assign(targets=[
        Name(id='s', ctx=Store()),
      ], value=Str(s="I'm a string!")),
    Expr(value=Call(func=Name(id='print', ctx=Load()), args=[
        Name(id='s', ctx=Load()),
      ], keywords=[])),
  ])

The astpp module also includes a parseprint() function to produce this formatted dump directly from the code string (no need to manually parse), along with an alias pdp() which references the same function:

astpp.parseprint(code, filename='<string>', mode='exec', **kwargs) astpp.pdp(code)

Option 3: Graphviz visualization using the astvisualizer from 0bserver07

Sometimes it is easier to understand the hierarchy with a picture. 0bserver07 provides a simple python command line script to generate a PDF representation of the AST tree structure.

Download the code from github.

git clone https://github.com/0bserver07/python-ast-visualizer.git

You need to install the graphviz package.

pip install graphviz

The application will accept either inline code in the command line, or in a specified source file.

Example:

python astvisualizer.py "df = pd.read_csv('raw_data.csv', 'r')"

Output: Creates the files Digraph.gv and Digraph.gv.pdf.

df = pd.read_csv('raw_data', 'r')

To process code from a separate file,

python astvisualizer.pdf -f code.py