-
Couldn't load subscription status.
- Fork 4
Add more common operations to Quantity
#10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
|
||
| def __pos__(self) -> Self: | ||
| """Return this quantity. | ||
|
|
||
| Returns: | ||
| This quantity. | ||
| """ | ||
| return self | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blankline after function?