mirror of
https://github.com/littlefs-project/littlefs.git
synced 2025-12-09 09:02:53 +00:00
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:
@@ -245,31 +245,33 @@ class RingIO:
|
||||
|
||||
|
||||
# parse different data representations
|
||||
def dat(x):
|
||||
# allow the first part of an a/b fraction
|
||||
if '/' in x:
|
||||
x, _ = x.split('/', 1)
|
||||
|
||||
# first try as int
|
||||
def dat(x, *args):
|
||||
try:
|
||||
return int(x, 0)
|
||||
except ValueError:
|
||||
pass
|
||||
# allow the first part of an a/b fraction
|
||||
if '/' in x:
|
||||
x, _ = x.split('/', 1)
|
||||
|
||||
# then try as float
|
||||
try:
|
||||
return float(x)
|
||||
except ValueError:
|
||||
pass
|
||||
# first try as int
|
||||
try:
|
||||
return int(x, 0)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# else give up
|
||||
raise ValueError("invalid dat %r" % x)
|
||||
# then try as float
|
||||
try:
|
||||
return float(x)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def try_dat(x):
|
||||
try:
|
||||
return dat(x)
|
||||
except ValueError:
|
||||
return None
|
||||
# else give up
|
||||
raise ValueError("invalid dat %r" % x)
|
||||
|
||||
# default on error?
|
||||
except ValueError as e:
|
||||
if args:
|
||||
return args[0]
|
||||
else:
|
||||
raise
|
||||
|
||||
def collect(csv_paths, defines=[]):
|
||||
# collect results from CSV files
|
||||
@@ -495,11 +497,11 @@ def punescape(s, attrs=None):
|
||||
f = m.group('format')
|
||||
if f[-1] in 'dboxX':
|
||||
if isinstance(v, str):
|
||||
v = try_dat(v) or 0
|
||||
v = dat(v, 0)
|
||||
v = int(v)
|
||||
elif f[-1] in 'fFeEgG':
|
||||
if isinstance(v, str):
|
||||
v = try_dat(v) or 0
|
||||
v = dat(v, 0)
|
||||
v = float(v)
|
||||
else:
|
||||
f = ('<' if '-' in f else '>') + f.replace('-', '')
|
||||
|
||||
Reference in New Issue
Block a user