scripts: csv.py: Made RFloats independent from RInts
The only reason RFloats reused RInt's operator definitions was to save a few keystrokes. But this dependency is unnecessary and will get in the way if we ever add a script that only uses RFloats.
This commit is contained in:
+38
-10
@@ -157,8 +157,9 @@ class RFloat(co.namedtuple('RFloat', 'x')):
|
|||||||
def __float__(self):
|
def __float__(self):
|
||||||
return float(self.x)
|
return float(self.x)
|
||||||
|
|
||||||
none = RInt.none
|
none = '%7s' % '-'
|
||||||
table = RInt.table
|
def table(self):
|
||||||
|
return '%7s' % (self,)
|
||||||
|
|
||||||
def diff(self, other):
|
def diff(self, other):
|
||||||
new = self.x if self else 0
|
new = self.x if self else 0
|
||||||
@@ -171,18 +172,45 @@ class RFloat(co.namedtuple('RFloat', 'x')):
|
|||||||
else:
|
else:
|
||||||
return '%+7.1f' % diff
|
return '%+7.1f' % diff
|
||||||
|
|
||||||
ratio = RInt.ratio
|
def ratio(self, other):
|
||||||
__pos__ = RInt.__pos__
|
new = self.x if self else 0
|
||||||
__neg__ = RInt.__neg__
|
old = other.x if other else 0
|
||||||
__abs__ = RInt.__abs__
|
if mt.isinf(new) and mt.isinf(old):
|
||||||
__add__ = RInt.__add__
|
return 0.0
|
||||||
__sub__ = RInt.__sub__
|
elif mt.isinf(new):
|
||||||
__mul__ = RInt.__mul__
|
return +mt.inf
|
||||||
|
elif mt.isinf(old):
|
||||||
|
return -mt.inf
|
||||||
|
elif not old and not new:
|
||||||
|
return 0.0
|
||||||
|
elif not old:
|
||||||
|
return +mt.inf
|
||||||
|
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)
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
return self.__class__(self.x - other.x)
|
||||||
|
|
||||||
|
def __mul__(self, other):
|
||||||
|
return self.__class__(self.x * other.x)
|
||||||
|
|
||||||
def __div__(self, other):
|
def __div__(self, other):
|
||||||
return self.__class__(self.x / other.x)
|
return self.__class__(self.x / other.x)
|
||||||
|
|
||||||
__mod__ = RInt.__mod__
|
def __mod__(self, other):
|
||||||
|
return self.__class__(self.x % other.x)
|
||||||
|
|
||||||
# fractional fields, a/b
|
# fractional fields, a/b
|
||||||
class RFrac(co.namedtuple('RFrac', 'a,b')):
|
class RFrac(co.namedtuple('RFrac', 'a,b')):
|
||||||
|
|||||||
Reference in New Issue
Block a user