diff options
-rw-r--r-- | mdpoly/abc.py | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/mdpoly/abc.py b/mdpoly/abc.py index 99d9484..8783262 100644 --- a/mdpoly/abc.py +++ b/mdpoly/abc.py @@ -23,6 +23,8 @@ class Algebra(Enum): class Expr(ABC): """ Binary tree to represent a mathematical expression. """ + # --- Properties --- + @property def is_leaf(self) -> bool: """ Whether an expression is a Leaf of the tree. """ @@ -73,10 +75,17 @@ class Expr(ABC): and *right*. If the shape cannot be computed because of an algebraic problem raises :py:class:`mdpoly.errors.AlgebraicError`. """ + # --- Magic Methods --- + def __repr__(self): name = self.__class__.__qualname__ return f"{name}(left={self.left}, right={self.right})" + def __iter__(self) -> Iterable[Expr]: + yield from self.children() + + # --- Methods --- + @abstractmethod def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]: """ Convert to a concrete representation @@ -147,11 +156,7 @@ class Expr(ABC): return replace_all(self) - - def __iter__(self) -> Iterable[Expr]: - yield from self.children() - - # --- Operator overloading --- + # --- Private methods --- @staticmethod def _assert_same_algebra(left: Expr, right: Expr) -> None: @@ -179,10 +184,13 @@ class Expr(ABC): return obj if not isinstance(obj, if_type): - raise TypeError(f"Cannot wrap {obj} with type {wrapper_type} because it is not of type {if_type}.") + raise TypeError(f"Cannot wrap {obj} with type {wrapper_type} because " + f"it is not of type {if_type}.") return wrapper_type(obj, *args, **kwargs) + # --- Operator overloading --- + def __add__(self, other: Any) -> Self: raise NotImplementedError |