mirror of
https://github.com/littlefs-project/littlefs.git
synced 2025-12-08 00:22:44 +00:00
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:
@@ -44,54 +44,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' % '+∞'
|
||||
@@ -101,8 +101,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):
|
||||
@@ -117,22 +117,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:
|
||||
@@ -140,10 +140,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 PerfResult(co.namedtuple('PerfResult', [
|
||||
@@ -821,17 +821,17 @@ def collect_perf(perf_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,
|
||||
|
||||
Reference in New Issue
Block a user