From f67ca9f3737d4729be84cb4d6357502b9a73e7eb Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 15 Mar 2024 18:22:38 +0100 Subject: Add util.showtree to print internals of expressions --- mdpoly/util.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mdpoly/util.py b/mdpoly/util.py index c7f3b51..ae726ef 100644 --- a/mdpoly/util.py +++ b/mdpoly/util.py @@ -36,3 +36,16 @@ def iszero(x: float, eps: float =NUMERICS_EPS) -> bool: def filtertype(t: type, seq: Iterable): """ Filter based on type. """ yield from filter(lambda x: isinstance(x, t), seq) + + +def showtree(expr): + """ Pretty print expression tree. """ + from PrettyPrint import PrettyPrintTree + def getval(x): + if x.is_leaf: + return str(x) + return x.__class__.__qualname__ + + PrettyPrintTree(lambda x: x.children(), getval)(expr) + + -- cgit v1.2.1