Skip to content

Commit dcd8988

Browse files
authored
feat: added value_in method to SIObject (#103)
* feat: added value_in method to SIObject Allows for better readability compare to single division back to dimensionless quantity. Example: - obj / METER <- unclear if it is per meter or dimless - obj.value_in(METER) <- clear dimless numeric * docs: added value_in method under SIObject in api docs
1 parent 30983fe commit dcd8988

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

si-units/docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- cbrt
77
- sqrt
88
- has_unit
9+
- value_in
910
summary:
1011
attributes: false
1112
functions: true

si-units/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ impl PySIObject {
135135
self.unit.eq(&other.unit)
136136
}
137137

138+
pub fn value_in<'py>(&self, py: Python<'py>, unit: &Self) -> PyResult<Bound<'py, PyAny>> {
139+
self.check_units(unit)?;
140+
self.value
141+
.bind(py)
142+
.call_method1("__truediv__", (&unit.value,))
143+
}
144+
138145
#[classattr]
139146
fn __array_priority__() -> u64 {
140147
1000

si-units/src/si_units/_core.pyi

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Self, Any
1+
from typing import Any, Self
22

33
class SIObject:
44
"""Combination of value and unit.
@@ -14,7 +14,7 @@ class SIObject:
1414
def __init__(self, value: float | Any, unit: list[int]) -> None:
1515
"""Constructs a new quantity.
1616
17-
Warning: Don't use the default constructor
17+
Warning: Don't use the default constructor
1818
This constructor should not be used to construct a quantity.
1919
Instead, multiply the value (float or array of floats)
2020
by the appropriate unit. See example below.
@@ -64,7 +64,7 @@ class SIObject:
6464
Raises:
6565
RuntimeError: When exponents of units are not multiples of three.
6666
AttributeError: When the inner data type has no 'cbrt' method.
67-
67+
6868
Examples:
6969
>>> from si_units import METER
7070
>>> volume = METER**3
@@ -83,6 +83,29 @@ class SIObject:
8383
"""
8484
...
8585

86+
def value_in(self, unit: Self) -> float | Any:
87+
"""Return the numeric value expressed in specified unit.
88+
89+
The underlying value (float, numpy.ndarray, torch.tensor, ...)
90+
is divided by unit and returned without the unit wrapper.
91+
92+
Args:
93+
unit: A quantity describing target unit (e.g. KILO * WATT * HOUR).
94+
95+
Returns:
96+
The numeric value of self expressed in unit.
97+
98+
Raises:
99+
RuntimeError: When self and unit have incompatible units.
100+
101+
Examples:
102+
>>> from si_units import JOULE, KILO, WATT, HOUR
103+
>>> energy = 5.4e6 * JOULE
104+
>>> energy.value_in(KILO * WATT * HOUR)
105+
1.5
106+
"""
107+
...
108+
86109
def array(value: SIObject | list[SIObject]) -> SIObject:
87110
"""Build SIObject from scalar or list.
88111
@@ -92,7 +115,7 @@ def array(value: SIObject | list[SIObject]) -> SIObject:
92115
value: Values to store. Must all have the same unit.
93116
94117
Returns:
95-
The quantity with values stored within array,
118+
The quantity with values stored within array,
96119
even if value is given as a scalar.
97120
98121
Raises:

0 commit comments

Comments
 (0)