aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-03-15 18:22:38 +0100
committerNao Pross <np@0hm.ch>2024-03-15 18:22:38 +0100
commitf67ca9f3737d4729be84cb4d6357502b9a73e7eb (patch)
tree960211b9eb9e67e3bb1f3dc4aac1ed8eeff7bdc6
parentMake expression objects dataclasses (diff)
downloadmdpoly-f67ca9f3737d4729be84cb4d6357502b9a73e7eb.tar.gz
mdpoly-f67ca9f3737d4729be84cb4d6357502b9a73e7eb.zip
Add util.showtree to print internals of expressions
-rw-r--r--mdpoly/util.py13
1 files changed, 13 insertions, 0 deletions
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)
+
+