scripts: Prefer .a for single entry namedtuples

- CsvInt.x -> CsvInt.a
- CsvFloat.x -> CsvFloat.a
- Rev.x -> Rev.a

This matches CsvFrac.a (paired with CsvFrac.b), and avoids confusion
with x/y variables such as Tile.x and Tile.y.

The other contender was .v, since these are cs*v* related types, but
sticking with .a gets the point across that the name really doesn't have
any meaning.

There's also some irony that we're forcing namedtuples to have
meaningless names, but it is useful to have a quick accessor for the
internal value.
This commit is contained in:
Christopher Haster
2025-04-12 01:40:16 -05:00
parent 98b16a9013
commit c63ed79c5f
9 changed files with 396 additions and 396 deletions

View File

@@ -35,54 +35,54 @@ THRESHOLD = (0.5, 0.85)
# integer fields
class CsvInt(co.namedtuple('CsvInt', 'x')):
class CsvInt(co.namedtuple('CsvInt', 'a')):
__slots__ = ()
def __new__(cls, x=0):
if isinstance(x, CsvInt):
return x
if isinstance(x, str):
def __new__(cls, a=0):
if isinstance(a, CsvInt):
return a
if isinstance(a, str):
try:
x = int(x, 0)
a = int(a, 0)
except ValueError:
# also accept +-∞ and +-inf
if re.match('^\s*\+?\s*(?:∞|inf)\s*$', x):
x = mt.inf
elif re.match('^\s*-\s*(?:∞|inf)\s*$', x):
x = -mt.inf
if re.match('^\s*\+?\s*(?:∞|inf)\s*$', a):
a = mt.inf
elif re.match('^\s*-\s*(?:∞|inf)\s*$', a):
a = -mt.inf
else:
raise
if not (isinstance(x, int) or mt.isinf(x)):
x = int(x)
return super().__new__(cls, x)
if not (isinstance(a, int) or mt.isinf(a)):
a = int(a)
return super().__new__(cls, a)
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.x)
return '%s(%r)' % (self.__class__.__name__, self.a)
def __str__(self):
if self.x == mt.inf:
if self.a == mt.inf:
return ''
elif self.x == -mt.inf:
elif self.a == -mt.inf:
return '-∞'
else:
return str(self.x)
return str(self.a)
def __bool__(self):
return bool(self.x)
return bool(self.a)
def __int__(self):
assert not mt.isinf(self.x)
return self.x
assert not mt.isinf(self.a)
return self.a
def __float__(self):
return float(self.x)
return float(self.a)
none = '%7s' % '-'
def table(self):
return '%7s' % (self,)
def diff(self, other):
new = self.x if self else 0
old = other.x if other else 0
new = self.a if self else 0
old = other.a if other else 0
diff = new - old
if diff == +mt.inf:
return '%7s' % '+∞'
@@ -92,8 +92,8 @@ class CsvInt(co.namedtuple('CsvInt', 'x')):
return '%+7d' % diff
def ratio(self, other):
new = self.x if self else 0
old = other.x if other else 0
new = self.a if self else 0
old = other.a if other else 0
if mt.isinf(new) and mt.isinf(old):
return 0.0
elif mt.isinf(new):
@@ -108,22 +108,22 @@ class CsvInt(co.namedtuple('CsvInt', 'x')):
return (new-old) / old
def __pos__(self):
return self.__class__(+self.x)
return self.__class__(+self.a)
def __neg__(self):
return self.__class__(-self.x)
return self.__class__(-self.a)
def __abs__(self):
return self.__class__(abs(self.x))
return self.__class__(abs(self.a))
def __add__(self, other):
return self.__class__(self.x + other.x)
return self.__class__(self.a + other.a)
def __sub__(self, other):
return self.__class__(self.x - other.x)
return self.__class__(self.a - other.a)
def __mul__(self, other):
return self.__class__(self.x * other.x)
return self.__class__(self.a * other.a)
def __truediv__(self, other):
if not other:
@@ -131,10 +131,10 @@ class CsvInt(co.namedtuple('CsvInt', 'x')):
return self.__class__(+mt.inf)
else:
return self.__class__(-mt.inf)
return self.__class__(self.x // other.x)
return self.__class__(self.a // other.a)
def __mod__(self, other):
return self.__class__(self.x % other.x)
return self.__class__(self.a % other.a)
# perf results
class PerfBdResult(co.namedtuple('PerfBdResult', [
@@ -795,17 +795,17 @@ def collect_perfbd(elf_path, trace_paths, *,
# common folding/tabling/read/write code
class Rev(co.namedtuple('Rev', 'x')):
class Rev(co.namedtuple('Rev', 'a')):
__slots__ = ()
# yes we need all of these because we're a namedtuple
def __lt__(self, other):
return self.x > other.x
return self.a > other.a
def __gt__(self, other):
return self.x < other.x
return self.a < other.a
def __le__(self, other):
return self.x >= other.x
return self.a >= other.a
def __ge__(self, other):
return self.x <= other.x
return self.a <= other.a
def fold(Result, results, *,
by=None,