Skip to content

Commit bc1f343

Browse files
committed
Remove timedelta-based custom init method from DeliveryPeriod
Now the dataclass's automatic `__init__` method gets used, so a `DeliveryDuration` instance must be provided. If not available, it can be produced from a `timedelta` with `DeliveryDuration.from_timedelta()`. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 0e7c87c commit bc1f343

File tree

2 files changed

+5
-69
lines changed

2 files changed

+5
-69
lines changed

src/frequenz/client/electricity_trading/_types.py

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -386,54 +386,6 @@ class DeliveryPeriod:
386386
duration: DeliveryDuration
387387
"""The length of the delivery period."""
388388

389-
def __init__(
390-
self,
391-
start: datetime,
392-
duration: timedelta,
393-
) -> None:
394-
"""
395-
Initialize the DeliveryPeriod object.
396-
397-
Args:
398-
start: Start UTC timestamp represents the beginning of the delivery period.
399-
duration: The length of the delivery period.
400-
401-
Raises:
402-
ValueError: If the start timestamp does not have a timezone.
403-
or if the duration is not 5, 15, 30, or 60 minutes.
404-
"""
405-
if start.tzinfo is None:
406-
raise ValueError("Start timestamp must have a timezone.")
407-
if start.tzinfo != timezone.utc:
408-
_logger.warning(
409-
"Start timestamp is not in UTC timezone. Converting to UTC."
410-
)
411-
start = start.astimezone(timezone.utc)
412-
self.start = start
413-
414-
minutes = duration.total_seconds() / 60
415-
match minutes:
416-
case 5:
417-
self.duration = DeliveryDuration.MINUTES_5
418-
case 15:
419-
self.duration = DeliveryDuration.MINUTES_15
420-
case 30:
421-
self.duration = DeliveryDuration.MINUTES_30
422-
case 60:
423-
self.duration = DeliveryDuration.MINUTES_60
424-
case _:
425-
raise ValueError(
426-
"Invalid duration value. Duration must be 5, 15, 30, or 60 minutes."
427-
)
428-
429-
def __hash__(self) -> int:
430-
"""
431-
Create hash of the DeliveryPeriod object.
432-
433-
Returns:
434-
Hash of the DeliveryPeriod object.
435-
"""
436-
return hash((self.start, self.duration))
437389

438390
def __eq__(
439391
self,
@@ -481,27 +433,10 @@ def from_pb(cls, delivery_period: delivery_duration_pb2.DeliveryPeriod) -> Self:
481433
482434
Returns:
483435
DeliveryPeriod object corresponding to the protobuf message.
484-
485-
Raises:
486-
ValueError: If the duration is not 5, 15, 30, or 60 minutes.
487436
"""
488437
start = delivery_period.start.ToDatetime(tzinfo=timezone.utc)
489438
delivery_duration_enum = DeliveryDuration.from_pb(delivery_period.duration)
490-
491-
match delivery_duration_enum:
492-
case DeliveryDuration.MINUTES_5:
493-
duration = timedelta(minutes=5)
494-
case DeliveryDuration.MINUTES_15:
495-
duration = timedelta(minutes=15)
496-
case DeliveryDuration.MINUTES_30:
497-
duration = timedelta(minutes=30)
498-
case DeliveryDuration.MINUTES_60:
499-
duration = timedelta(minutes=60)
500-
case _:
501-
raise ValueError(
502-
"Invalid duration value. Duration must be 5, 15, 30, or 60 minutes."
503-
)
504-
return cls(start=start, duration=duration)
439+
return cls(start=start, duration=delivery_duration_enum)
505440

506441
def to_pb(self) -> delivery_duration_pb2.DeliveryPeriod:
507442
"""Convert a DeliveryPeriod object to protobuf DeliveryPeriod.

src/frequenz/client/electricity_trading/cli/etrading.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Client,
1414
Currency,
1515
DeliveryArea,
16+
DeliveryDuration,
1617
DeliveryPeriod,
1718
EnergyMarketCodeType,
1819
MarketSide,
@@ -72,7 +73,7 @@ async def receive_public_trades( # pylint: disable=too-many-arguments
7273
check_delivery_start(delivery_start)
7374
delivery_period = DeliveryPeriod(
7475
start=delivery_start,
75-
duration=timedelta(minutes=15),
76+
duration=DeliveryDuration.MINUTES_15,
7677
)
7778
stream = client.receive_public_trades(
7879
delivery_period=delivery_period,
@@ -112,7 +113,7 @@ async def receive_public_orders( # pylint: disable=too-many-arguments
112113
check_delivery_start(delivery_start)
113114
delivery_period = DeliveryPeriod(
114115
start=delivery_start,
115-
duration=timedelta(minutes=15),
116+
duration=DeliveryDuration.MINUTES_15,
116117
)
117118
stream = client.receive_public_order_book(
118119
delivery_period=delivery_period,
@@ -274,7 +275,7 @@ async def create_order(
274275
),
275276
delivery_period=DeliveryPeriod(
276277
start=delivery_start,
277-
duration=duration,
278+
duration=DeliveryDuration.from_timedelta(duration),
278279
),
279280
order_type=OrderType.LIMIT,
280281
side=side,

0 commit comments

Comments
 (0)