scripts: Adopted csv.py-related result-type tweaks in all scripts

- RInt/RFloat now accepts implicitly castable types (mainly
  RInt(RFloat(x)) and RFloat(RInt(x))).

- RInt/RFloat/RFrac are now "truthy", implements __bool__.

- More operator support for RInt/RFloat/RFrac:

  - __pos__ => +a
  - __neg__ => -a
  - __abs__ => abs(a)
  - __div__ => a/b
  - __mod__ => a%b

  These work in Python, but are mainly used to implement expr eval in
  csv.py.
This commit is contained in:
Christopher Haster
2024-11-12 01:09:26 -06:00
parent 7f7420d13f
commit 434479f101
7 changed files with 162 additions and 8 deletions

View File

@@ -48,7 +48,8 @@ class RInt(co.namedtuple('RInt', 'x')):
x = -mt.inf
else:
raise
assert isinstance(x, int) or mt.isinf(x), x
if not (isinstance(x, int) or mt.isinf(x)):
x = int(x)
return super().__new__(cls, x)
def __str__(self):
@@ -59,6 +60,9 @@ class RInt(co.namedtuple('RInt', 'x')):
else:
return str(self.x)
def __bool__(self):
return bool(self.x)
def __int__(self):
assert not mt.isinf(self.x)
return self.x
@@ -97,6 +101,15 @@ class RInt(co.namedtuple('RInt', 'x')):
else:
return (new-old) / old
def __pos__(self):
return self.__class__(+self.x)
def __neg__(self):
return self.__class__(-self.x)
def __abs__(self):
return self.__class__(abs(self.x))
def __add__(self, other):
return self.__class__(self.x + other.x)
@@ -106,6 +119,12 @@ class RInt(co.namedtuple('RInt', 'x')):
def __mul__(self, other):
return self.__class__(self.x * other.x)
def __div__(self, other):
return self.__class__(self.x // other.x)
def __mod__(self, other):
return self.__class__(self.x % other.x)
# code size results
class CodeResult(co.namedtuple('CodeResult', [
'file', 'function',