Skip to content

Commit 04c077f

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

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,53 @@ def as_megawatts(self) -> float:
352352
The power in megawatts.
353353
"""
354354
return self._base_value / 1e6
355+
356+
357+
class Current(
358+
Quantity,
359+
exponent_unit_map={
360+
-3: "mA",
361+
0: "A",
362+
},
363+
):
364+
"""A current quantity."""
365+
366+
@classmethod
367+
def from_amperes(cls, amperes: float) -> Self:
368+
"""Initialize a new current quantity.
369+
370+
Args:
371+
amperes: The current in amperes.
372+
373+
Returns:
374+
A new current quantity.
375+
"""
376+
return cls(value=amperes, exponent=0)
377+
378+
@classmethod
379+
def from_milliamperes(cls, milliamperes: float) -> Self:
380+
"""Initialize a new current quantity.
381+
382+
Args:
383+
milliamperes: The current in milliamperes.
384+
385+
Returns:
386+
A new current quantity.
387+
"""
388+
return cls(value=milliamperes, exponent=-3)
389+
390+
def as_amperes(self) -> float:
391+
"""Return the current in amperes.
392+
393+
Returns:
394+
The current in amperes.
395+
"""
396+
return self._base_value
397+
398+
def as_milliamperes(self) -> float:
399+
"""Return the current in milliamperes.
400+
401+
Returns:
402+
The current in milliamperes.
403+
"""
404+
return self._base_value * 1e3

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 Power, Quantity
8+
from frequenz.sdk.timeseries._quantities import Current, Power, Quantity
99

1010

1111
class Fz1(
@@ -161,3 +161,18 @@ def test_power() -> None:
161161
assert power == Power.from_milliwatts(1200000.0)
162162
assert power == Power.from_megawatts(0.0012)
163163
assert power != Power.from_watts(1000.0)
164+
165+
166+
def test_current() -> None:
167+
"""Test the current class."""
168+
current = Current.from_milliamperes(0.0000002)
169+
assert f"{current:.9}" == "0.0000002 mA"
170+
current = Current.from_amperes(600000.0)
171+
assert f"{current}" == "600000 A"
172+
173+
current = Current.from_amperes(6.0)
174+
assert current.as_amperes() == 6.0
175+
assert current.as_milliamperes() == 6000.0
176+
assert current == Current.from_milliamperes(6000.0)
177+
assert current == Current.from_amperes(6.0)
178+
assert current != Current.from_amperes(5.0)

0 commit comments

Comments
 (0)