Skip to content

Commit 3a259d5

Browse files
committed
Update custom Quantity constructors to use __new__
This is so that there are no remaining dependencies on the default quantity constructor, before it is disabled for specialized quantites. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 8d82973 commit 3a259d5

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ def __add__(self, other: Self) -> Self:
190190
"""
191191
if not type(other) is type(self):
192192
return NotImplemented
193-
return type(self)(self._base_value + other._base_value)
193+
summe = type(self).__new__(type(self))
194+
summe._base_value = self._base_value + other._base_value
195+
return summe
194196

195197
def __sub__(self, other: Self) -> Self:
196198
"""Return the difference of this quantity and another.
@@ -203,7 +205,9 @@ def __sub__(self, other: Self) -> Self:
203205
"""
204206
if not type(other) is type(self):
205207
return NotImplemented
206-
return type(self)(self._base_value - other._base_value)
208+
difference = type(self).__new__(type(self))
209+
difference._base_value = self._base_value - other._base_value
210+
return difference
207211

208212
def __gt__(self, other: Self) -> bool:
209213
"""Return whether this quantity is greater than another.
@@ -296,7 +300,9 @@ def from_watts(cls, watts: float) -> Self:
296300
Returns:
297301
A new power quantity.
298302
"""
299-
return cls(value=watts, exponent=0)
303+
power = cls.__new__(cls)
304+
power._base_value = watts
305+
return power
300306

301307
@classmethod
302308
def from_milliwatts(cls, milliwatts: float) -> Self:
@@ -308,7 +314,9 @@ def from_milliwatts(cls, milliwatts: float) -> Self:
308314
Returns:
309315
A new power quantity.
310316
"""
311-
return cls(value=milliwatts, exponent=-3)
317+
power = cls.__new__(cls)
318+
power._base_value = milliwatts * 10**-3
319+
return power
312320

313321
@classmethod
314322
def from_kilowatts(cls, kilowatts: float) -> Self:
@@ -320,7 +328,9 @@ def from_kilowatts(cls, kilowatts: float) -> Self:
320328
Returns:
321329
A new power quantity.
322330
"""
323-
return cls(value=kilowatts, exponent=3)
331+
power = cls.__new__(cls)
332+
power._base_value = kilowatts * 10**3
333+
return power
324334

325335
@classmethod
326336
def from_megawatts(cls, megawatts: float) -> Self:
@@ -332,7 +342,9 @@ def from_megawatts(cls, megawatts: float) -> Self:
332342
Returns:
333343
A new power quantity.
334344
"""
335-
return cls(value=megawatts, exponent=6)
345+
power = cls.__new__(cls)
346+
power._base_value = megawatts * 10**6
347+
return power
336348

337349
def as_watts(self) -> float:
338350
"""Return the power in watts.
@@ -427,7 +439,9 @@ def from_amperes(cls, amperes: float) -> Self:
427439
Returns:
428440
A new current quantity.
429441
"""
430-
return cls(value=amperes, exponent=0)
442+
current = cls.__new__(cls)
443+
current._base_value = amperes
444+
return current
431445

432446
@classmethod
433447
def from_milliamperes(cls, milliamperes: float) -> Self:
@@ -439,7 +453,9 @@ def from_milliamperes(cls, milliamperes: float) -> Self:
439453
Returns:
440454
A new current quantity.
441455
"""
442-
return cls(value=milliamperes, exponent=-3)
456+
current = cls.__new__(cls)
457+
current._base_value = milliamperes * 10**-3
458+
return current
443459

444460
def as_amperes(self) -> float:
445461
"""Return the current in amperes.
@@ -482,7 +498,9 @@ def from_volts(cls, volts: float) -> Self:
482498
Returns:
483499
A new voltage quantity.
484500
"""
485-
return cls(value=volts, exponent=0)
501+
voltage = cls.__new__(cls)
502+
voltage._base_value = volts
503+
return voltage
486504

487505
@classmethod
488506
def from_millivolts(cls, millivolts: float) -> Self:
@@ -494,7 +512,9 @@ def from_millivolts(cls, millivolts: float) -> Self:
494512
Returns:
495513
A new voltage quantity.
496514
"""
497-
return cls(value=millivolts, exponent=-3)
515+
voltage = cls.__new__(cls)
516+
voltage._base_value = millivolts * 10**-3
517+
return voltage
498518

499519
@classmethod
500520
def from_kilovolts(cls, kilovolts: float) -> Self:
@@ -506,7 +526,9 @@ def from_kilovolts(cls, kilovolts: float) -> Self:
506526
Returns:
507527
A new voltage quantity.
508528
"""
509-
return cls(value=kilovolts, exponent=3)
529+
voltage = cls.__new__(cls)
530+
voltage._base_value = kilovolts * 10**3
531+
return voltage
510532

511533
def as_volts(self) -> float:
512534
"""Return the voltage in volts.
@@ -564,7 +586,9 @@ def from_watt_hours(cls, watt_hours: float) -> Self:
564586
Returns:
565587
A new energy quantity.
566588
"""
567-
return cls(value=watt_hours, exponent=0)
589+
energy = cls.__new__(cls)
590+
energy._base_value = watt_hours
591+
return energy
568592

569593
@classmethod
570594
def from_kilowatt_hours(cls, kilowatt_hours: float) -> Self:
@@ -576,7 +600,9 @@ def from_kilowatt_hours(cls, kilowatt_hours: float) -> Self:
576600
Returns:
577601
A new energy quantity.
578602
"""
579-
return cls(value=kilowatt_hours, exponent=3)
603+
energy = cls.__new__(cls)
604+
energy._base_value = kilowatt_hours * 10**3
605+
return energy
580606

581607
@classmethod
582608
def from_megawatt_hours(cls, megawatt_hours: float) -> Self:
@@ -588,7 +614,9 @@ def from_megawatt_hours(cls, megawatt_hours: float) -> Self:
588614
Returns:
589615
A new energy quantity.
590616
"""
591-
return cls(value=megawatt_hours, exponent=6)
617+
energy = cls.__new__(cls)
618+
energy._base_value = megawatt_hours * 10**6
619+
return energy
592620

593621
def as_watt_hours(self) -> float:
594622
"""Return the energy in watt hours.

0 commit comments

Comments
 (0)