3 """A class to represent a rate of change."""
5 from typing import Optional
9 """A class to represent a rate of change."""
13 multiplier: Optional[float] = None,
15 percentage: Optional[float] = None,
16 percent_change: Optional[float] = None,
19 if multiplier is not None:
20 if isinstance(multiplier, str):
21 multiplier = multiplier.replace('%', '')
24 self.multiplier: float = m
26 self.multiplier = multiplier
28 if percentage is not None:
29 self.multiplier = percentage / 100
31 if percent_change is not None:
32 self.multiplier = 1.0 + percent_change / 100
35 raise Exception('Exactly one of percentage, percent_change or multiplier is required.')
37 def apply_to(self, other):
38 return self.__mul__(other)
41 return self.__mul__(other)
44 return self.multiplier
46 def __mul__(self, other):
47 return self.multiplier * float(other)
51 def __truediv__(self, other):
52 return self.multiplier / float(other)
54 def __add__(self, other):
55 return self.multiplier + float(other)
59 def __sub__(self, other):
60 return self.multiplier - float(other)
62 def __eq__(self, other):
63 return self.multiplier == float(other)
65 def __ne__(self, other):
66 return not self.__eq__(other)
68 def __lt__(self, other):
69 return self.multiplier < float(other)
71 def __gt__(self, other):
72 return self.multiplier > float(other)
74 def __le__(self, other):
75 return self < other or self == other
77 def __ge__(self, other):
78 return self > other or self == other
81 return self.multiplier
83 def __repr__(self, *, relative=False, places=3):
85 percentage = (self.multiplier - 1.0) * 100.0
87 percentage = self.multiplier * 100.0
88 return f'{percentage:+.{places}f}%'