Skip to content

Commit

Permalink
Remove unnecessary casting of int to float
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Apr 16, 2024
1 parent 9e72905 commit cc18c62
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/wireviz/wv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,25 @@ def parse_number_and_unit(
elif isinstance(inp, NumberAndUnit):
return inp
elif isinstance(inp, float) or isinstance(inp, int):
return NumberAndUnit(float(inp), default_unit)
return NumberAndUnit(inp, default_unit)
elif isinstance(inp, str):
if " " in inp:
number, unit = inp.split(" ", 1)
num_str, unit = inp.split(" ", 1)
else:
number, unit = inp, default_unit
num_str, unit = inp, default_unit

try:
number = float(number)
except ValueError:
raise Exception(
f"{inp} is not a valid number and unit.\n"
"It must be a number, or a number and unit separated by a space."
)
else:
return NumberAndUnit(number, unit)
number = int(num_str)
except ValueError: # maybe it is a float?
try:
number = float(num_str)
except ValueError: # neither float nor int
raise Exception(
f"{inp} is not a valid number and unit.\n"
"It must be a number, or a number and unit separated by a space."
)

return NumberAndUnit(number, unit)


def int2tuple(inp):
Expand Down

0 comments on commit cc18c62

Please sign in to comment.