Requirements:
- The input may be a string or a number
- If the input can be interpreted as int without loss of precision, discard it in int
- If the input can be thought of as a float, drop it into a float
Here is the code section where I use this.
def make_operand(symbol, left=None, right=None):
valid_symbols = ['*', '/', '+', '-']
if symbol in valid_symbols:
return Operand(symbol, left, right)
as_int = re.compile("^-?[0-9]+$").match(str(symbol))
as_float = re.compile("^[-+]?[0-9]*\.?[0-9]+$").match(str(symbol))
as_number = int(symbol) if as_int else float(symbol) if as_float else None
if as_number:
return NumericOperand(as_number)
raise ValueError("Invalid symbol or number")
It works, but it looks dirty and smells bad.
An implementation using try blocks also works, but seems less simple:
as_number = None
try:
as_float = float(symbol)
except ValueError:
as_float = None
if as_float:
as_int = int(as_float)
as_number = as_int if as_int == as_float else as_float
if as_number:
return NumericOperand(as_number)
raise ValueError("Invalid symbol or number")
Is there a better approach, or is one close to the Pythonic method for doing things?
source
share