scripts: Adopted dat tweak in other scripts

This just makes dat behave similarly to Python's getattr, etc:

- dat("bogus")       -> raises ValueError
- dat("bogus", 1234) -> returns 1234

This replaces try_dat, which is easy to forget about when copy-pasting
between scripts.

Though all of this wouldn't be necessary if only we could catch
exceptions in expressions...
This commit is contained in:
Christopher Haster
2025-03-10 20:31:13 -05:00
parent e780fd40f7
commit c60301719a
5 changed files with 102 additions and 94 deletions

View File

@@ -1296,11 +1296,11 @@ def punescape(s, attrs=None):
f = m.group('format') f = m.group('format')
if f[-1] in 'dboxX': if f[-1] in 'dboxX':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = int(v) v = int(v)
elif f[-1] in 'fFeEgG': elif f[-1] in 'fFeEgG':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = float(v) v = float(v)
else: else:
f = ('<' if '-' in f else '>') + f.replace('-', '') f = ('<' if '-' in f else '>') + f.replace('-', '')

View File

@@ -245,31 +245,33 @@ class RingIO:
# parse different data representations # parse different data representations
def dat(x): def dat(x, *args):
# allow the first part of an a/b fraction
if '/' in x:
x, _ = x.split('/', 1)
# first try as int
try: try:
return int(x, 0) # allow the first part of an a/b fraction
except ValueError: if '/' in x:
pass x, _ = x.split('/', 1)
# then try as float # first try as int
try: try:
return float(x) return int(x, 0)
except ValueError: except ValueError:
pass pass
# else give up # then try as float
raise ValueError("invalid dat %r" % x) try:
return float(x)
except ValueError:
pass
def try_dat(x): # else give up
try: raise ValueError("invalid dat %r" % x)
return dat(x)
except ValueError: # default on error?
return None except ValueError as e:
if args:
return args[0]
else:
raise
def collect(csv_paths, defines=[]): def collect(csv_paths, defines=[]):
# collect results from CSV files # collect results from CSV files
@@ -495,11 +497,11 @@ def punescape(s, attrs=None):
f = m.group('format') f = m.group('format')
if f[-1] in 'dboxX': if f[-1] in 'dboxX':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = int(v) v = int(v)
elif f[-1] in 'fFeEgG': elif f[-1] in 'fFeEgG':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = float(v) v = float(v)
else: else:
f = ('<' if '-' in f else '>') + f.replace('-', '') f = ('<' if '-' in f else '>') + f.replace('-', '')

View File

@@ -169,31 +169,33 @@ def openio(path, mode='r', buffering=-1):
return open(path, mode, buffering) return open(path, mode, buffering)
# parse different data representations # parse different data representations
def dat(x): def dat(x, *args):
# allow the first part of an a/b fraction
if '/' in x:
x, _ = x.split('/', 1)
# first try as int
try: try:
return int(x, 0) # allow the first part of an a/b fraction
except ValueError: if '/' in x:
pass x, _ = x.split('/', 1)
# then try as float # first try as int
try: try:
return float(x) return int(x, 0)
except ValueError: except ValueError:
pass pass
# else give up # then try as float
raise ValueError("invalid dat %r" % x) try:
return float(x)
except ValueError:
pass
def try_dat(x): # else give up
try: raise ValueError("invalid dat %r" % x)
return dat(x)
except ValueError: # default on error?
return None except ValueError as e:
if args:
return args[0]
else:
raise
def collect(csv_paths, defines=[]): def collect(csv_paths, defines=[]):
# collect results from CSV files # collect results from CSV files
@@ -419,11 +421,11 @@ def punescape(s, attrs=None):
f = m.group('format') f = m.group('format')
if f[-1] in 'dboxX': if f[-1] in 'dboxX':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = int(v) v = int(v)
elif f[-1] in 'fFeEgG': elif f[-1] in 'fFeEgG':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = float(v) v = float(v)
else: else:
f = ('<' if '-' in f else '>') + f.replace('-', '') f = ('<' if '-' in f else '>') + f.replace('-', '')

View File

@@ -46,31 +46,33 @@ def openio(path, mode='r', buffering=-1):
return open(path, mode, buffering) return open(path, mode, buffering)
# parse different data representations # parse different data representations
def dat(x): def dat(x, *args):
# allow the first part of an a/b fraction
if '/' in x:
x, _ = x.split('/', 1)
# first try as int
try: try:
return int(x, 0) # allow the first part of an a/b fraction
except ValueError: if '/' in x:
pass x, _ = x.split('/', 1)
# then try as float # first try as int
try: try:
return float(x) return int(x, 0)
except ValueError: except ValueError:
pass pass
# else give up # then try as float
raise ValueError("invalid dat %r" % x) try:
return float(x)
except ValueError:
pass
def try_dat(x): # else give up
try: raise ValueError("invalid dat %r" % x)
return dat(x)
except ValueError: # default on error?
return None except ValueError as e:
if args:
return args[0]
else:
raise
def collect(csv_paths, defines=[]): def collect(csv_paths, defines=[]):
# collect results from CSV files # collect results from CSV files
@@ -279,11 +281,11 @@ def punescape(s, attrs=None):
f = m.group('format') f = m.group('format')
if f[-1] in 'dboxX': if f[-1] in 'dboxX':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = int(v) v = int(v)
elif f[-1] in 'fFeEgG': elif f[-1] in 'fFeEgG':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = float(v) v = float(v)
else: else:
f = ('<' if '-' in f else '>') + f.replace('-', '') f = ('<' if '-' in f else '>') + f.replace('-', '')

View File

@@ -62,31 +62,33 @@ def openio(path, mode='r', buffering=-1):
return open(path, mode, buffering) return open(path, mode, buffering)
# parse different data representations # parse different data representations
def dat(x): def dat(x, *args):
# allow the first part of an a/b fraction
if '/' in x:
x, _ = x.split('/', 1)
# first try as int
try: try:
return int(x, 0) # allow the first part of an a/b fraction
except ValueError: if '/' in x:
pass x, _ = x.split('/', 1)
# then try as float # first try as int
try: try:
return float(x) return int(x, 0)
except ValueError: except ValueError:
pass pass
# else give up # then try as float
raise ValueError("invalid dat %r" % x) try:
return float(x)
except ValueError:
pass
def try_dat(x): # else give up
try: raise ValueError("invalid dat %r" % x)
return dat(x)
except ValueError: # default on error?
return None except ValueError as e:
if args:
return args[0]
else:
raise
def collect(csv_paths, defines=[]): def collect(csv_paths, defines=[]):
# collect results from CSV files # collect results from CSV files
@@ -295,11 +297,11 @@ def punescape(s, attrs=None):
f = m.group('format') f = m.group('format')
if f[-1] in 'dboxX': if f[-1] in 'dboxX':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = int(v) v = int(v)
elif f[-1] in 'fFeEgG': elif f[-1] in 'fFeEgG':
if isinstance(v, str): if isinstance(v, str):
v = try_dat(v) or 0 v = dat(v, 0)
v = float(v) v = float(v)
else: else:
f = ('<' if '-' in f else '>') + f.replace('-', '') f = ('<' if '-' in f else '>') + f.replace('-', '')