Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,8 @@
def is_numeric(s):
try:
float(s)
return True
except (ValueError, TypeError):
return False
is_numeric('123.0')

View file

@ -0,0 +1 @@
'123'.isdigit()

View file

@ -0,0 +1,13 @@
def is_numeric(literal):
"""Return whether a literal can be parsed as a numeric value"""
castings = [int, float, complex,
lambda s: int(s,2), #binary
lambda s: int(s,8), #octal
lambda s: int(s,16)] #hex
for cast in castings:
try:
cast(literal)
return True
except ValueError:
pass
return False

View file

@ -0,0 +1,23 @@
def numeric(literal):
"""Return value of numeric literal or None if can't parse a value"""
castings = [int, float, complex,
lambda s: int(s,2), #binary
lambda s: int(s,8), #octal
lambda s: int(s,16)] #hex
for cast in castings:
try:
return cast(literal)
except ValueError:
pass
return None
tests = [
'0', '0.', '00', '123', '0123', '+123', '-123', '-123.', '-123e-4', '-.8E-04',
'0.123', '(5)', '-123+4.5j', '0b0101', ' +0B101 ', '0o123', '-0xABC', '0x1a1',
'12.5%', '1/2', '½', '', 'π', '', '1,000,000', '1 000', '- 001.20e+02',
'NaN', 'inf', '-Infinity']
for s in tests:
print("%14s -> %-14s %-20s is_numeric: %-5s str.isnumeric: %s" % (
'"'+s+'"', numeric(s), type(numeric(s)), is_numeric(s), s.isnumeric() ))

View file

@ -0,0 +1,5 @@
import re
numeric = re.compile('[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?')
is_numeric = lambda x: numeric.fullmatch(x) != None
is_numeric('123.0')