Skip to content

Commit 160780f

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

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,67 @@ def as_milliamperes(self) -> float:
402402
The current in milliamperes.
403403
"""
404404
return self._base_value * 1e3
405+
406+
407+
class Voltage(Quantity, exponent_unit_map={0: "V", -3: "mV", 3: "kV"}):
408+
"""A voltage quantity."""
409+
410+
@classmethod
411+
def from_volts(cls, volts: float) -> Self:
412+
"""Initialize a new voltage quantity.
413+
414+
Args:
415+
volts: The voltage in volts.
416+
417+
Returns:
418+
A new voltage quantity.
419+
"""
420+
return cls(value=volts, exponent=0)
421+
422+
@classmethod
423+
def from_millivolts(cls, millivolts: float) -> Self:
424+
"""Initialize a new voltage quantity.
425+
426+
Args:
427+
millivolts: The voltage in millivolts.
428+
429+
Returns:
430+
A new voltage quantity.
431+
"""
432+
return cls(value=millivolts, exponent=-3)
433+
434+
@classmethod
435+
def from_kilovolts(cls, kilovolts: float) -> Self:
436+
"""Initialize a new voltage quantity.
437+
438+
Args:
439+
kilovolts: The voltage in kilovolts.
440+
441+
Returns:
442+
A new voltage quantity.
443+
"""
444+
return cls(value=kilovolts, exponent=3)
445+
446+
def as_volts(self) -> float:
447+
"""Return the voltage in volts.
448+
449+
Returns:
450+
The voltage in volts.
451+
"""
452+
return self._base_value
453+
454+
def as_millivolts(self) -> float:
455+
"""Return the voltage in millivolts.
456+
457+
Returns:
458+
The voltage in millivolts.
459+
"""
460+
return self._base_value * 1e3
461+
462+
def as_kilovolts(self) -> float:
463+
"""Return the voltage in kilovolts.
464+
465+
Returns:
466+
The voltage in kilovolts.
467+
"""
468+
return self._base_value / 1e3

tests/timeseries/test_quantities.py

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

1010

1111
class Fz1(
@@ -176,3 +176,20 @@ def test_current() -> None:
176176
assert current == Current.from_milliamperes(6000.0)
177177
assert current == Current.from_amperes(6.0)
178178
assert current != Current.from_amperes(5.0)
179+
180+
181+
def test_voltage() -> None:
182+
"""Test the voltage class."""
183+
voltage = Voltage.from_millivolts(0.0000002)
184+
assert f"{voltage:.9}" == "0.0000002 mV"
185+
voltage = Voltage.from_kilovolts(600000.0)
186+
assert f"{voltage}" == "600000 kV"
187+
188+
voltage = Voltage.from_volts(6.0)
189+
assert voltage.as_volts() == 6.0
190+
assert voltage.as_millivolts() == 6000.0
191+
assert voltage.as_kilovolts() == 0.006
192+
assert voltage == Voltage.from_millivolts(6000.0)
193+
assert voltage == Voltage.from_kilovolts(0.006)
194+
assert voltage == Voltage.from_volts(6.0)
195+
assert voltage != Voltage.from_volts(5.0)

0 commit comments

Comments
 (0)