Skip to content

Commit

Permalink
don't omit 1 in str (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Feb 12, 2025
2 parents 8d84e26 + bfc76ba commit 27f1bed
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
6 changes: 3 additions & 3 deletions test/cython/test_with_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ def test_hash() -> None:
def test_str() -> None:
assert str(val(2, conv(3, 4, 5, 6), s, m)) == '2 m'
assert str(val(2j, conv(3, 4, 5, 6), s)) == '2j s'
assert str(val(1, units=m, display_units=s)) == 's'
assert str(val(1, units=m)) == 'm'
assert str(val(1, units=h)) == 's^3600'
assert str(val(1, units=m, display_units=s)) == '1 s'
assert str(val(1, units=m)) == '1 m'
assert str(val(1, units=h)) == '1 s^3600'
assert str(val([2, 3, 5], units=h, display_units=m)) == '[2 3 5] m'
assert str(val([2, 3], units=h, display_units=mps)) == '[2 3] m/s'

Expand Down
6 changes: 3 additions & 3 deletions test/test_unit_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_get_unit_with_auto_create_override() -> None:
db_manual = UnitDatabase(auto_create_units=False)

u = db_auto.get_unit('missing')
assert str(u) == 'missing'
assert str(u) == '1 missing'
with pytest.raises(KeyError):
db_manual.get_unit('missing')

Expand All @@ -61,9 +61,9 @@ def test_get_unit_with_auto_create_override() -> None:
db_manual.get_unit('gone', auto_create=False)

u = db_auto.get_unit('empty', auto_create=True)
assert str(u) == 'empty'
assert str(u) == '1 empty'
u = db_manual.get_unit('empty', auto_create=True)
assert str(u) == 'empty'
assert str(u) == '1 empty'


def test_add_root_unit() -> None:
Expand Down
4 changes: 2 additions & 2 deletions test/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def test_repr() -> None:
def test_str() -> None:
from tunits.units import mm, meter, kilometer, rad, cyc

assert str(Value(1, mm)) == 'mm'
assert str(Value(1, mm)) == '1 mm'
assert str(Value(4, mm)) == '4 mm'
assert str(2 * meter * kilometer) == '2 km*m'
assert str(cyc) == 'cyc'
assert str(cyc) == '1 cyc'
assert str(3.25 * cyc**2) == '3.25 cyc^2'
assert str(3.25 * cyc * rad) == '3.25 cyc*rad'
assert str((4 * kilometer) ** 0.5) == '2.0 km^(1/2)'
Expand Down
3 changes: 0 additions & 3 deletions tunits/core/cython/with_unit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,6 @@ cdef class WithUnit:

def __str__(self):
unit_str = str(self.display_units)
if not isinstance(self.value, np.ndarray) \
and self.value == 1 and unit_str != '':
return unit_str
val_str = (repr if isinstance(self.value, float) else str)(self.value)
return (val_str + " " + unit_str).strip()

Expand Down

0 comments on commit 27f1bed

Please sign in to comment.