3 # © Copyright 2021-2022, Scott Gasch
5 """A class to represent a rate of change."""
7 from typing import Optional
11 """A class to represent a rate of change."""
15 multiplier: Optional[float] = None,
17 percentage: Optional[float] = None,
18 percent_change: Optional[float] = None,
21 if multiplier is not None:
22 if isinstance(multiplier, str):
23 multiplier = multiplier.replace('%', '')
26 self.multiplier: float = m
28 self.multiplier = multiplier
30 if percentage is not None:
31 self.multiplier = percentage / 100
33 if percent_change is not None:
34 self.multiplier = 1.0 + percent_change / 100
37 raise Exception('Exactly one of percentage, percent_change or multiplier is required.')
39 def apply_to(self, other):
40 return self.__mul__(other)
43 return self.__mul__(other)
46 return self.multiplier
48 def __mul__(self, other):
49 return self.multiplier * float(other)
53 def __truediv__(self, other):
54 return self.multiplier / float(other)
56 def __add__(self, other):
57 return self.multiplier + float(other)
61 def __sub__(self, other):
62 return self.multiplier - float(other)
64 def __eq__(self, other):
65 return self.multiplier == float(other)
67 def __ne__(self, other):
68 return not self.__eq__(other)
70 def __lt__(self, other):
71 return self.multiplier < float(other)
73 def __gt__(self, other):
74 return self.multiplier > float(other)
76 def __le__(self, other):
77 return self < other or self == other
79 def __ge__(self, other):
80 return self > other or self == other
83 return self.multiplier
85 def __repr__(self, *, relative=False, places=3):
87 percentage = (self.multiplier - 1.0) * 100.0
89 percentage = self.multiplier * 100.0
90 return f'{percentage:+.{places}f}%'