Skip to content

Commit 1163cc5

Browse files
committed
Add parentheses around denominator of UnknownUnits
1 parent 0b5d0fa commit 1163cc5

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

sasdata/quantities/_units_base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,14 @@ def _name(self):
399399
return ""
400400
case (_, []):
401401
return " ".join(num)
402+
case ([], [d]):
403+
return f"1 / {d}"
402404
case ([], _):
403-
return "1 / " + " ".join(den)
405+
return "1 / (" + " ".join(den) + ")"
406+
case (_, [d]):
407+
return f"{" ".join(num)} / {d}"
404408
case _:
405-
return " ".join(num) + " / " + " ".join(den)
409+
return f"{" ".join(num)} / ({" ".join(den)})"
406410

407411
def __eq__(self, other):
408412
match other:

sasdata/quantities/units.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,14 @@ def _name(self):
484484
return ""
485485
case (_, []):
486486
return " ".join(num)
487+
case ([], [d]):
488+
return f"1 / {d}"
487489
case ([], _):
488-
return "1 / " + " ".join(den)
490+
return "1 / (" + " ".join(den) + ")"
491+
case (_, [d]):
492+
return f"{" ".join(num)} / {d}"
489493
case _:
490-
return " ".join(num) + " / " + " ".join(den)
494+
return f"{" ".join(num)} / ({" ".join(den)})"
491495

492496
def __eq__(self, other):
493497
match other:

test/quantities/utest_units.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def dissimilar_term(request):
9393
def test_unit_dissimilar(dissimilar_term):
9494
units = dissimilar_term
9595
for i, unit_1 in enumerate(units):
96-
for unit_2 in units[i + 1 :]:
96+
for unit_2 in units[i + 1:]:
9797
assert not unit_1.equivalent(unit_2), "Units should not be equivalent"
9898

9999

@@ -124,7 +124,9 @@ def test_unit_names():
124124
assert str(pizza * pizza) == "Pizza^2"
125125

126126
assert str(1 / pizza) == "1 / Pizza"
127+
assert str(1 / pizza / pineapple) == "1 / (Pineapple Pizza)"
127128
assert str(slice / pizza) == "Slice / Pizza"
129+
assert str(slice / pizza / pineapple) == "Slice / (Pineapple Pizza)"
128130
assert str((slice / pizza) ** 2) == "Slice^2 / Pizza^2"
129131

130132
assert str(pie**0.5) == "Pie^0.5" # A valid unit, because pie are square

0 commit comments

Comments
 (0)