From 249b745ce8a7d5910077dbee2fb2eae7010b8ea5 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Mon, 16 Sep 2024 11:07:51 +1200 Subject: [PATCH] Don't use Expr.__str__ for general use #139 --- lcapy/expr.py | 17 ++++++++++------- lcapy/mnacpts.py | 7 +++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lcapy/expr.py b/lcapy/expr.py index b7c95ec9..d3b1597e 100644 --- a/lcapy/expr.py +++ b/lcapy/expr.py @@ -59,13 +59,16 @@ def _pexpr(self): if not hasattr(self, 'expr'): return self - if state.show_units: - if state.canonical_units: - return self.expr_with_canonical_units - else: - return self.expr_with_units - else: - return self.expr + try: + if state.show_units: + if state.canonical_units: + return self.expr_with_canonical_units + else: + return self.expr_with_units + except AttributeError: + pass + + return self.expr def __repr__(self): """This is called by repr(expr). It is used, e.g., when printing diff --git a/lcapy/mnacpts.py b/lcapy/mnacpts.py index b65f3f54..e9a94b4f 100644 --- a/lcapy/mnacpts.py +++ b/lcapy/mnacpts.py @@ -185,6 +185,13 @@ def _expand(self): def _arg_format(self, value): """Place value string inside curly braces if it contains a delimiter.""" + # Don't use Expr.__str__ since this will add units + # if show_units enabled. + try: + value = value.sympy + except AttributeError: + pass + string = str(value) if string.startswith('{'):