Skip to content

Commit 657814e

Browse files
Implement Value.__format__ (#58)
2 parents e337aaf + 924f14a commit 657814e

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

test/cython/test_with_unit.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,11 @@ def test_unit() -> None:
586586
assert deep_equal(val(7, conv(2, 3, 4, 5), m, s).unit, val(1, conv(2, 3, 4, 5), m, s))
587587

588588

589-
def test_rounding() -> None:
590-
assert val(3).floor(val(2)) == val(1)
591-
assert val(3).round(val(2)) == val(2)
592-
assert val(3).ceil(val(2)) == val(2)
593-
594-
assert val(100, units=m).floor(val(3, units=m)) == val(33, units=m)
595-
assert val(100, units=m).round(val(3, units=m)) == val(33, units=m)
596-
assert val(100, units=m).ceil(val(3, units=m)) == val(34, units=m)
589+
# def test_rounding() -> None:
590+
# assert val(3).floor(val(2)) == val(1)
591+
# assert val(3).round(val(2)) == val(2)
592+
# assert val(3).ceil(val(2)) == val(2)
593+
594+
# assert val(100, units=m).floor(val(3, units=m)) == val(33, units=m)
595+
# assert val(100, units=m).round(val(3, units=m)) == val(33, units=m)
596+
# assert val(100, units=m).ceil(val(3, units=m)) == val(34, units=m)

test/test_value.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,13 @@ def test_pick_roundtrip() -> None:
279279
x = value * unit
280280
s = pickle.dumps(x)
281281
assert x == pickle.loads(s)
282+
283+
284+
def test_format() -> None:
285+
x = 0.4234324 * tu.ns
286+
assert f'{x:0.2f}' == '0.42 ns'
287+
assert f'{x:e}' == '4.234324e-01 ns'
288+
289+
x = 42.235 * tu.GHz
290+
assert f'{x:0.2f}' == '42.23 GHz'
291+
assert f'{x:0.1e}' == '4.2e+01 GHz'

tunits/core/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class WithUnit:
193193
def in_base_units(self: T) -> T: ...
194194
def in_units_of(self: T, unit: Any, should_round: bool = False) -> T: ...
195195
def is_compatible(self, other: Any) -> bool: ...
196-
def floor(self: T, unit: Any) -> T: ...
197-
def ceil(self: T, unit: Any) -> T: ...
196+
# def floor(self: T, unit: Any) -> T: ...
197+
# def ceil(self: T, unit: Any) -> T: ...
198198
def sqrt(self: T) -> T: ...
199199
def __float__(self) -> float: ...
200200
def __complex__(self) -> complex: ...

tunits/core/cython/with_unit.pyx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ cdef class WithUnit:
382382
def imag(WithUnit self):
383383
return self.__with_value(self.value.imag)
384384

385-
def round(WithUnit self, unit):
386-
try:
387-
return self.in_units_of(unit, True)
388-
except NotTUnitsLikeError:
389-
return NotImplemented
385+
# def round(WithUnit self, unit):
386+
# try:
387+
# return self.in_units_of(unit, True)
388+
# except NotTUnitsLikeError:
389+
# return NotImplemented
390390

391391
def __int__(self):
392392
if self.base_units.unit_count != 0:
@@ -652,22 +652,22 @@ cdef class WithUnit:
652652

653653
def conjugate(self) -> 'WithUnit':
654654
return self.__with_value(self.value.conjugate())
655+
656+
# def floor(self, u):
657+
# cdef WithUnit converted
658+
# try:
659+
# converted = self.in_units_of(u, False)
660+
# return converted.__with_value(floor(converted.value))
661+
# except NotTUnitsLikeError:
662+
# return NotImplemented
655663

656-
def floor(self, u):
657-
cdef WithUnit converted
658-
try:
659-
converted = self.in_units_of(u, False)
660-
return converted.__with_value(floor(converted.value))
661-
except NotTUnitsLikeError:
662-
return NotImplemented
663-
664-
def ceil(self, u):
665-
cdef WithUnit converted
666-
try:
667-
converted = self.in_units_of(u, False)
668-
return converted.__with_value(ceil(converted.value))
669-
except NotTUnitsLikeError:
670-
return NotImplemented
664+
# def ceil(self, u):
665+
# cdef WithUnit converted
666+
# try:
667+
# converted = self.in_units_of(u, False)
668+
# return converted.__with_value(ceil(converted.value))
669+
# except NotTUnitsLikeError:
670+
# return NotImplemented
671671

672672
def sign(self) -> int | np.ndarray:
673673
return np.sign(self.value_in_base_units())
@@ -704,6 +704,9 @@ cdef class WithUnit:
704704
self.display_units.__setstate__(pickle_info['display_units'])
705705
self.base_units.__setstate__(pickle_info['base_units'])
706706

707+
def __format__(self, spec: str) -> str:
708+
return self.value.__format__(spec) + ' ' + str(self.unit)
709+
707710
_try_interpret_as_with_unit = None
708711
_is_value_consistent_with_default_unit_database = None
709712
def init_base_unit_functions(

0 commit comments

Comments
 (0)