From 1d5f3e7520ff212c3b56f5bad4c3e3535bc5c60b Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Thu, 7 Mar 2024 12:42:49 +0100 Subject: Comments & minor changes --- mdpoly/abc.py | 20 ++++++++++++++------ 1 file 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 -- cgit v1.2.1