Skip to content

Commit 8fbaa3a

Browse files
committed
Add __str__ and __repr__ to LatestValueCache
The `str` just returns the value, and `repr` gets all the info, including the new `unique_id`. We also implement `__str__` for `_Sentinel` to return a more meaningful string, since it will be shown in the `LatestValueCache` string representation when no value has been received yet. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 8b2e546 commit 8fbaa3a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/frequenz/sdk/_internal/_channels.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def new_receiver(self, *, limit: int = 50) -> Receiver[T_co]:
3232
class _Sentinel:
3333
"""A sentinel to denote that no value has been received yet."""
3434

35+
def __str__(self) -> str:
36+
"""Return a string representation of this sentinel."""
37+
return "<no value received yet>"
38+
3539

3640
class LatestValueCache(typing.Generic[T_co]):
3741
"""A cache that stores the latest value in a receiver."""
@@ -91,3 +95,14 @@ async def _run(self) -> None:
9195
async def stop(self) -> None:
9296
"""Stop the cache."""
9397
await cancel_and_await(self._task)
98+
99+
def __repr__(self) -> str:
100+
"""Return a string representation of this cache."""
101+
return (
102+
f"<LatestValueCache latest_value={self._latest_value!r}, "
103+
f"receiver={self._receiver!r}, unique_id={self._unique_id!r}>"
104+
)
105+
106+
def __str__(self) -> str:
107+
"""Return the last value seen by this cache."""
108+
return str(self._latest_value)

0 commit comments

Comments
 (0)