awesome-python-ast
awesome-python-ast copied to clipboard
AbstractTree
I recently created a package abstracttree that has some support for ast. It can visualise ast objects in different ways. For example:
import ast
from abstracttree import plot_tree
import matplotlib.pyplot as plt
p = ast.parse("y = x**2 + y**2")
plot_tree(p)
plt.show()
Or if you prefer text it can create:
print_tree(p)
---
Module(body=[↓], type_ignores=[])
└─ Assign(targets=[↓], value=↓, type_comment=None)
├─ Name(id='y', ctx=Store())
└─ BinOp(left=↓, op=Add(), right=↓)
├─ BinOp(left=↓, op=Pow(), right=↓)
│ ├─ Name(id='x', ctx=Load())
│ └─ Constant(value=2)
└─ BinOp(left=↓, op=Pow(), right=↓)
├─ Name(id='y', ctx=Load())
└─ Constant(value=2)