Skip to content

Commit 925b579

Browse files
add more properties
1 parent af7fa09 commit 925b579

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

test/test_value.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,28 @@ def test_divison_with_dimensionless_preserves_ratios() -> None:
229229
assert B / A == Value(1.2, 'GHz^-2')
230230

231231
assert A / B == Value(10 / 12, 'GHz^2')
232+
233+
234+
def test_units() -> None:
235+
A, B = Value(1, 'GHz^2'), Value(1200, 'MHz/GHz')
236+
assert A.units == 'GHz^2'
237+
assert B.units == 'MHz/GHz'
238+
239+
240+
def test_base_unit() -> None:
241+
A, B = Value(1, 'GHz^2'), Value(1200, 'MHz/GHz')
242+
assert A.base_unit == Value(1, 'Hz^2')
243+
assert B.base_unit == Value(1, '')
244+
245+
246+
def test_name() -> None:
247+
A, B = Value(1, 'GHz^2'), Value(1200, 'MHz/GHz')
248+
assert A.name == 'GHz^2'
249+
with pytest.raises(AssertionError):
250+
_ = B.name
251+
252+
253+
def test_sign() -> None:
254+
for x in np.linspace(-10, 10, 20):
255+
v = Value(x, 'ns')
256+
assert v.sign() == np.sign(x)

test/test_value_array.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,21 @@ def test_divison_with_dimensionless_preserves_ratios() -> None:
203203
assert B / A == ValueArray([1.2], 'GHz^-2')
204204

205205
assert A / B == ValueArray([10 / 12], 'GHz^2')
206+
207+
208+
def test_units() -> None:
209+
A, B = Value(np.random.random(10), 'GHz^2'), Value(np.random.random(10), 'MHz/GHz')
210+
assert A.units == 'GHz^2'
211+
assert B.units == 'MHz/GHz'
212+
213+
214+
def test_base_unit() -> None:
215+
A, B = Value(np.random.random(10), 'GHz^2'), Value(np.random.random(10), 'MHz/GHz')
216+
assert A.base_unit == Value(1, 'Hz^2')
217+
assert B.base_unit == Value(1, '')
218+
219+
220+
def test_sign() -> None:
221+
for x in np.random.random((10, 3, 4)):
222+
v = Value(x, 'ns')
223+
np.testing.assert_equal(v.sign(), np.sign(x))

tunits/core/__init__.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ class WithUnit:
183183
real: 'Value' | 'ValueArray'
184184
imag: 'Value' | 'ValueArray'
185185
is_dimensionless: bool
186+
units: str
187+
base_unit: 'Value'
188+
name: str
186189

187190
def __init__(
188191
self, value: Any, unit: 'WithUnit' | UnitTerm | UnitArray | str | None = None
@@ -313,6 +316,7 @@ class Value(Generic[NumericalT], WithUnit, np.generic, SupportsIndex):
313316
def value_in_base_units(self) -> NumericalT: ...
314317
def __index__(self) -> int: ...
315318
def __pow__(self, other: Any, modulus: Any = None) -> Value: ...
319+
def sign(self) -> int: ...
316320

317321
class ValueArray(Generic[ValueType2], WithUnit):
318322
value: NDArray[Any]
@@ -373,6 +377,7 @@ class ValueArray(Generic[ValueType2], WithUnit):
373377
self, ufunc: Callable[..., Any], method: str, *inputs: Any, **kwargs: Any
374378
) -> ValueArray: ...
375379
def __pow__(self, other: Any, modulus: Any = None) -> ValueArray: ...
380+
def sign(self) -> NDArray[np.integer]: ...
376381

377382
def init_base_unit_functions(
378383
try_interpret_as_with_unit: Callable[[Any, bool], WithUnit | None],

tunits/core/cython/with_unit.pyx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ cdef class WithUnit:
149149
property exp10:
150150
def __get__(self):
151151
return self.conv.exp10
152+
property units:
153+
def __get__(self):
154+
return str(self.display_units)
155+
property base_unit:
156+
def __get__(self):
157+
return Value(1, str(self.base_units))
158+
159+
property name:
160+
def __get__(self):
161+
assert self.value == 1
162+
return str(self.display_units)
152163

153164
def __init__(WithUnit self, value, unit=None):
154165
"""
@@ -655,6 +666,9 @@ cdef class WithUnit:
655666
except NotTUnitsLikeError:
656667
return NotImplemented
657668

669+
def sign(self) -> int | np.ndarray:
670+
return np.sign(self.value_in_base_units())
671+
658672
_try_interpret_as_with_unit = None
659673
_is_value_consistent_with_default_unit_database = None
660674
def init_base_unit_functions(

0 commit comments

Comments
 (0)