Skip to content

Commit 76de40d

Browse files
authored
Support float.inf and float.nan in quantities (#514)
This might be controversial, but my use case is the following: ``` boundaries: list[tuple[Percentage, SoC]] = [ (config.battery_bounds.lower, SoC.DISCHARGED), (reserve_bounds.lower, SoC.RESERVE), (config.target_soc_bounds_pct.lower, SoC.NORMAL), (config.target_soc_bounds_pct.upper, SoC.TARGET), (reserve_bounds.upper, SoC.NORMAL), (config.battery_bounds.upper, SoC.RESERVE), (Percentage.from_percent(float("inf")), SoC.CHARGED), ] for boundary, state in boundaries: if bat_soc_pct < boundary: return state ``` I am using inf here as a stop-marker that will always be larger than the last boundary. This way I can avoid having to add a special case for the last boundary.
2 parents 60516db + 8f9570d commit 76de40d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66

77
## Upgrading
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
1010

1111
- `Channels` has been upgraded to version 0.16.0, for information on how to upgrade visit https://github.com/frequenz-floss/frequenz-channels-python/releases/tag/v0.16.0
1212

1313
## New Features
1414

15-
* Add quantity class `Frequency` for frequency values.
15+
- Add quantity class `Frequency` for frequency values.
1616

1717
## Bug Fixes
1818

1919
- Fix formatting issue for `Quantity` objects with zero values.
20+
- Fix formatting isuse for `Quantity` when the base value is float.inf or float.nan.
2021

2122
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ def __format__(self, __format_spec: str) -> str:
170170
if not self._exponent_unit_map:
171171
return f"{self._base_value:.{precision}f}"
172172

173+
if math.isinf(self._base_value) or math.isnan(self._base_value):
174+
return f"{self._base_value} {self._exponent_unit_map[0]}"
175+
173176
abs_value = abs(self._base_value)
174177
exponent = math.floor(math.log10(abs_value)) if abs_value else 0
175178
unit_place = exponent - exponent % 3

tests/timeseries/test_quantities.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,34 @@ def test_neg() -> None:
319319
pct = Percentage.from_fraction(30)
320320
assert -pct == Percentage.from_fraction(-30)
321321
assert -(-pct) == pct
322+
323+
324+
def test_inf() -> None:
325+
"""Test proper formating when using inf in quantities"""
326+
assert f"{Power.from_watts(float('inf'))}" == "inf W"
327+
assert f"{Power.from_watts(float('-inf'))}" == "-inf W"
328+
329+
assert f"{Voltage.from_volts(float('inf'))}" == "inf V"
330+
assert f"{Voltage.from_volts(float('-inf'))}" == "-inf V"
331+
332+
assert f"{Current.from_amperes(float('inf'))}" == "inf A"
333+
assert f"{Current.from_amperes(float('-inf'))}" == "-inf A"
334+
335+
assert f"{Energy.from_watt_hours(float('inf'))}" == "inf Wh"
336+
assert f"{Energy.from_watt_hours(float('-inf'))}" == "-inf Wh"
337+
338+
assert f"{Frequency.from_hertz(float('inf'))}" == "inf Hz"
339+
assert f"{Frequency.from_hertz(float('-inf'))}" == "-inf Hz"
340+
341+
assert f"{Percentage.from_fraction(float('inf'))}" == "inf %"
342+
assert f"{Percentage.from_fraction(float('-inf'))}" == "-inf %"
343+
344+
345+
def test_nan() -> None:
346+
"""Test proper formating when using nan in quantities"""
347+
assert f"{Power.from_watts(float('nan'))}" == "nan W"
348+
assert f"{Voltage.from_volts(float('nan'))}" == "nan V"
349+
assert f"{Current.from_amperes(float('nan'))}" == "nan A"
350+
assert f"{Energy.from_watt_hours(float('nan'))}" == "nan Wh"
351+
assert f"{Frequency.from_hertz(float('nan'))}" == "nan Hz"
352+
assert f"{Percentage.from_fraction(float('nan'))}" == "nan %"

0 commit comments

Comments
 (0)