Skip to content

Commit a464978

Browse files
committed
Add a special string representation for Interval instances
In particular the `__str__` method returns a much more compact representation. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent e9f0249 commit a464978

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/frequenz/core/math.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __lt__(self, other: Self, /) -> bool:
4040
"""Type variable for a value that a `LessThanComparable` or `None`."""
4141

4242

43-
@dataclass(frozen=True)
43+
@dataclass(frozen=True, repr=False)
4444
class Interval(Generic[LessThanComparableOrNoneT]):
4545
"""An interval to test if a value is within its limits.
4646
@@ -104,3 +104,13 @@ def __contains__(self, item: LessThanComparableOrNoneT) -> bool:
104104
casted_item < cast(LessThanComparable, self.start)
105105
or casted_item > cast(LessThanComparable, self.end)
106106
)
107+
108+
def __repr__(self) -> str:
109+
"""Return a string representation of this instance."""
110+
return f"Interval({self.start!r}, {self.end!r})"
111+
112+
def __str__(self) -> str:
113+
"""Return a string representation of this instance."""
114+
start = "∞" if self.start is None else str(self.start)
115+
end = "∞" if self.end is None else str(self.end)
116+
return f"[{start}, {end}]"

0 commit comments

Comments
 (0)