Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/frequenz/quantities/_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ def base_value(self) -> float:
"""
return self._base_value

def __round__(self, ndigits: int | None = None) -> Self:
"""Round this quantity to the given number of digits.

Args:
ndigits: The number of digits to round to.

Returns:
The rounded quantity.
"""
return self._new(round(self._base_value, ndigits))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blankline after function?


def __pos__(self) -> Self:
"""Return this quantity.

Returns:
This quantity.
"""
return self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blankline after function?

@property
def base_unit(self) -> str | None:
"""Return the base unit of this quantity.
Expand Down
32 changes: 32 additions & 0 deletions tests/test_quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ def test_isclose() -> None:
assert not Fz1(1.024445).isclose(Fz1(1.0))


@hypothesis.given(value=st.floats(allow_nan=False, allow_infinity=False))
@pytest.mark.parametrize("ndigits", [0, 1, 2, 3])
def test_round(value: float, ndigits: int) -> None:
"""Test the rounding of the quantities."""
assert round(Quantity(value), ndigits) == Quantity(round(value, ndigits))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a blnak line here?

def test_addition_subtraction() -> None:
"""Test the addition and subtraction of the quantities."""
assert Quantity(1) + Quantity(1, exponent=0) == Quantity(2, exponent=0)
Expand Down Expand Up @@ -486,6 +491,33 @@ def test_neg() -> None:
assert -(-pct) == pct


def test_pos() -> None:
"""Test the positive sign of quantities."""
power = Power.from_watts(1000.0)
assert +power == power
assert +(+power) == power

voltage = Voltage.from_volts(230.0)
assert +voltage == voltage
assert +(+voltage) == voltage

current = Current.from_amperes(2)
assert +current == current
assert +(+current) == current

energy = Energy.from_kilowatt_hours(6.2)
assert +energy == energy
assert +(+energy) == energy

freq = Frequency.from_hertz(50)
assert +freq == freq
assert +(+freq) == freq

pct = Percentage.from_fraction(30)
assert +pct == pct
assert +(+pct) == pct


def test_inf() -> None:
"""Test proper formating when using inf in quantities."""
assert f"{Power.from_watts(float('inf'))}" == "inf W"
Expand Down