Skip to content

Commit 4755a86

Browse files
Add __str__ method for DeliveryPeriod (#67)
Currently in the logs the delivery period is always printed as an object (e.g. `delivery_period=<frequenz.client.electricity_trading._types.DeliveryPeriod object at 0x7fdfcef729d0>`). In order to increase clarity, this PR adds a __str__ function so that the delivery period can be clearly printed, (e.g.: `DeliveryPeriod(start=2024-11-11 16:00:00 +00:00, duration=15min)`).
2 parents 0c6cef0 + ba63831 commit 4755a86

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Replace the local `PaginationParams` type with the `frequenz-client-common` one
1717
* Remove dependency to `googleapis-common-protos`
1818
* Replace `Energy` with `Power` for the `quantity` representation
19+
* Add str function for `DeliveryPeriod` object
1920

2021
## Bug Fixes
2122

src/frequenz/client/electricity_trading/_types.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,32 @@ def __eq__(
365365

366366
return self.start == other.start and self.duration == other.duration
367367

368+
def __str__(self) -> str:
369+
"""
370+
Return string representation of the DeliveryPeriod object.
371+
372+
Returns:
373+
String representation of the DeliveryPeriod object.
374+
"""
375+
duration_map = {
376+
DeliveryDuration.MINUTES_5: "5min",
377+
DeliveryDuration.MINUTES_15: "15min",
378+
DeliveryDuration.MINUTES_30: "30min",
379+
DeliveryDuration.MINUTES_60: "60min",
380+
}
381+
duration_str = duration_map.get(self.duration, "Unknown duration")
382+
start_str = self.start.strftime("%Y-%m-%d %H:%M:%S %Z")
383+
return f"DeliveryPeriod(start={start_str}, duration={duration_str})"
384+
385+
def __repr__(self) -> str:
386+
"""
387+
Developer-friendly representation of the DeliveryPeriod object.
388+
389+
Returns:
390+
Developer-friendly representation of the DeliveryPeriod object.
391+
"""
392+
return self.__str__()
393+
368394
@classmethod
369395
def from_pb(cls, delivery_period: delivery_duration_pb2.DeliveryPeriod) -> Self:
370396
"""Convert a protobuf DeliveryPeriod to DeliveryPeriod object.

0 commit comments

Comments
 (0)