Skip to content

Commit 81f9c61

Browse files
committed
Add class for representing Power quantities
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 1519cb3 commit 81f9c61

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,87 @@ def __eq__(self, other: object) -> bool:
268268
# doesn't help mypy identify the type of other, so the below line is necessary.
269269
assert isinstance(other, self.__class__)
270270
return self._base_value == other._base_value
271+
272+
273+
class Power(
274+
Quantity,
275+
exponent_unit_map={
276+
-3: "mW",
277+
0: "W",
278+
3: "kW",
279+
6: "MW",
280+
},
281+
):
282+
"""A power quantity."""
283+
284+
@classmethod
285+
def from_watts(cls, watts: float) -> Self:
286+
"""Initialize a new power quantity.
287+
288+
Args:
289+
watts: The power in watts.
290+
291+
Returns:
292+
A new power quantity.
293+
"""
294+
return cls(value=watts, exponent=0)
295+
296+
@classmethod
297+
def from_milliwatts(cls, milliwatts: float) -> Self:
298+
"""Initialize a new power quantity.
299+
300+
Args:
301+
milliwatts: The power in milliwatts.
302+
303+
Returns:
304+
A new power quantity.
305+
"""
306+
return cls(value=milliwatts, exponent=-3)
307+
308+
@classmethod
309+
def from_kilowatts(cls, kilowatts: float) -> Self:
310+
"""Initialize a new power quantity.
311+
312+
Args:
313+
kilowatts: The power in kilowatts.
314+
315+
Returns:
316+
A new power quantity.
317+
"""
318+
return cls(value=kilowatts, exponent=3)
319+
320+
@classmethod
321+
def from_megawatts(cls, megawatts: float) -> Self:
322+
"""Initialize a new power quantity.
323+
324+
Args:
325+
megawatts: The power in megawatts.
326+
327+
Returns:
328+
A new power quantity.
329+
"""
330+
return cls(value=megawatts, exponent=6)
331+
332+
def as_watts(self) -> float:
333+
"""Return the power in watts.
334+
335+
Returns:
336+
The power in watts.
337+
"""
338+
return self._base_value
339+
340+
def as_kilowatts(self) -> float:
341+
"""Return the power in kilowatts.
342+
343+
Returns:
344+
The power in kilowatts.
345+
"""
346+
return self._base_value / 1e3
347+
348+
def as_megawatts(self) -> float:
349+
"""Return the power in megawatts.
350+
351+
Returns:
352+
The power in megawatts.
353+
"""
354+
return self._base_value / 1e6

tests/timeseries/test_quantities.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from frequenz.sdk.timeseries._quantities import Quantity
8+
from frequenz.sdk.timeseries._quantities import Power, Quantity
99

1010

1111
class Fz1(
@@ -146,3 +146,18 @@ def test_comparison() -> None:
146146
excinfo.value.args[0]
147147
== "'>=' not supported between instances of 'Fz1' and 'Fz2'"
148148
)
149+
150+
151+
def test_power() -> None:
152+
"""Test the power class."""
153+
power = Power.from_milliwatts(0.0000002)
154+
assert f"{power:.9}" == "0.0000002 mW"
155+
power = Power.from_kilowatts(10000000.2)
156+
assert f"{power}" == "10000 MW"
157+
158+
power = Power.from_kilowatts(1.2)
159+
assert power.as_watts() == 1200.0
160+
assert power.as_megawatts() == 0.0012
161+
assert power == Power.from_milliwatts(1200000.0)
162+
assert power == Power.from_megawatts(0.0012)
163+
assert power != Power.from_watts(1000.0)

0 commit comments

Comments
 (0)