diff options
author | Nao Pross <np@0hm.ch> | 2024-03-07 12:42:49 +0100 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-03-07 12:42:49 +0100 |
commit | 1d5f3e7520ff212c3b56f5bad4c3e3535bc5c60b (patch) | |
tree | 42a2de203affef5dd6460fa7d2c5e7fc3f9433c4 | |
parent | Relax dependencies (diff) | |
download | mdpoly-1d5f3e7520ff212c3b56f5bad4c3e3535bc5c60b.tar.gz mdpoly-1d5f3e7520ff212c3b56f5bad4c3e3535bc5c60b.zip |
Comments & minor changes
-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 |