summaryrefslogtreecommitdiff
path: root/type/rate.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
committerScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
commit4faa994d32223c8d560d9dad0ca90a3f7eb10d6a (patch)
tree1200e5615d94247a120b98a4ddceb2cd6674e0af /type/rate.py
parentc79ecbf708a63a54a9c3e8d189b65d4794930082 (diff)
Money, Rate, CentCount and a bunch of bugfixes.
Diffstat (limited to 'type/rate.py')
-rw-r--r--type/rate.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/type/rate.py b/type/rate.py
new file mode 100644
index 0000000..3161131
--- /dev/null
+++ b/type/rate.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+
+from typing import Optional
+
+
+class Rate(object):
+ def __init__(
+ self,
+ multiplier: Optional[float] = None,
+ *,
+ percentage: Optional[float] = None,
+ percent_change: Optional[float] = None,
+ ):
+ count = 0
+ if multiplier is not None:
+ if isinstance(multiplier, str):
+ multiplier = multiplier.replace('%', '')
+ m = float(multiplier)
+ m /= 100
+ self.multiplier = m
+ else:
+ self.multiplier = multiplier
+ count += 1
+ if percentage is not None:
+ self.multiplier = percentage / 100
+ count += 1
+ if percent_change is not None:
+ self.multiplier = 1.0 + percent_change / 100
+ count += 1
+ if count != 1:
+ raise Exception(
+ 'Exactly one of percentage, percent_change or multiplier is required.'
+ )
+
+ def apply_to(self, other):
+ return self.__mul__(other)
+
+ def of(self, other):
+ return self.__mul__(other)
+
+ def __float__(self):
+ return self.multiplier
+
+ def __mul__(self, other):
+ return self.multiplier * float(other)
+
+ __rmul__ = __mul__
+
+ def __truediv__(self, other):
+ return self.multiplier / float(other)
+
+ def __add__(self, other):
+ return self.multiplier + float(other)
+
+ __radd__ = __add__
+
+ def __sub__(self, other):
+ return self.multiplier - float(other)
+
+ def __eq__(self, other):
+ return self.multiplier == float(other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __lt__(self, other):
+ return self.multiplier < float(other)
+
+ def __gt__(self, other):
+ return self.multiplier > float(other)
+
+ def __le__(self, other):
+ return self < other or self == other
+
+ def __ge__(self, other):
+ return self > other or self == other
+
+ def __hash__(self):
+ return self.multiplier
+
+ def __repr__(self,
+ *,
+ relative=False,
+ places=3):
+ if relative:
+ percentage = (self.multiplier - 1.0) * 100.0
+ else:
+ percentage = self.multiplier * 100.0
+ return f'{percentage:+.{places}f}%'