awesome-python-ast icon indicating copy to clipboard operation
awesome-python-ast copied to clipboard

AbstractTree

Open lverweijen opened this issue 1 year ago • 0 comments

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()

mpl_pythagorian

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)

lverweijen avatar Feb 06 '24 22:02 lverweijen