From c60301719a1e0cb3f509bee7f385afa33833283d Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 10 Mar 2025 20:31:13 -0500 Subject: [PATCH] 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... --- scripts/csv.py | 4 ++-- scripts/plot.py | 48 +++++++++++++++++++++++--------------------- scripts/plotmpl.py | 48 +++++++++++++++++++++++--------------------- scripts/treemap.py | 48 +++++++++++++++++++++++--------------------- scripts/treemapd3.py | 48 +++++++++++++++++++++++--------------------- 5 files changed, 102 insertions(+), 94 deletions(-) diff --git a/scripts/csv.py b/scripts/csv.py index 58e1b5d6..ddf4d3b0 100755 --- a/scripts/csv.py +++ b/scripts/csv.py @@ -1296,11 +1296,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('-', '') diff --git a/scripts/plot.py b/scripts/plot.py index ef65fd80..67d81f96 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -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('-', '') diff --git a/scripts/plotmpl.py b/scripts/plotmpl.py index dbf47453..b468c4b1 100755 --- a/scripts/plotmpl.py +++ b/scripts/plotmpl.py @@ -169,31 +169,33 @@ def openio(path, mode='r', buffering=-1): return open(path, mode, buffering) # 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 @@ -419,11 +421,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('-', '') diff --git a/scripts/treemap.py b/scripts/treemap.py index f718ec75..cd9d67c7 100755 --- a/scripts/treemap.py +++ b/scripts/treemap.py @@ -46,31 +46,33 @@ def openio(path, mode='r', buffering=-1): return open(path, mode, buffering) # 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 @@ -279,11 +281,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('-', '') diff --git a/scripts/treemapd3.py b/scripts/treemapd3.py index 1cd7a755..d54a2270 100755 --- a/scripts/treemapd3.py +++ b/scripts/treemapd3.py @@ -62,31 +62,33 @@ def openio(path, mode='r', buffering=-1): return open(path, mode, buffering) # 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 @@ -295,11 +297,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('-', '')