29 lines
654 B
Python
29 lines
654 B
Python
def is_numeric(lit):
|
|
'Return value of numeric literal string or ValueError exception'
|
|
|
|
# Handle '0'
|
|
if lit == '0': return 0
|
|
# Hex/Binary
|
|
litneg = lit[1:] if lit[0] == '-' else lit
|
|
if litneg[0] == '0':
|
|
if litneg[1] in 'xX':
|
|
return int(lit,16)
|
|
elif litneg[1] in 'bB':
|
|
return int(lit,2)
|
|
else:
|
|
try:
|
|
return int(lit,8)
|
|
except ValueError:
|
|
pass
|
|
|
|
# Int/Float/Complex
|
|
try:
|
|
return int(lit)
|
|
except ValueError:
|
|
pass
|
|
try:
|
|
return float(lit)
|
|
except ValueError:
|
|
pass
|
|
return complex(lit)
|