3 from typing import Optional
9 multiplier: Optional[float] = None,
11 percentage: Optional[float] = None,
12 percent_change: Optional[float] = None,
15 if multiplier is not None:
16 if isinstance(multiplier, str):
17 multiplier = multiplier.replace('%', '')
20 self.multiplier: float = m
22 self.multiplier = multiplier
24 if percentage is not None:
25 self.multiplier = percentage / 100
27 if percent_change is not None:
28 self.multiplier = 1.0 + percent_change / 100
32 'Exactly one of percentage, percent_change or multiplier is required.'
35 def apply_to(self, other):
36 return self.__mul__(other)
39 return self.__mul__(other)
42 return self.multiplier
44 def __mul__(self, other):
45 return self.multiplier * float(other)
49 def __truediv__(self, other):
50 return self.multiplier / float(other)
52 def __add__(self, other):
53 return self.multiplier + float(other)
57 def __sub__(self, other):
58 return self.multiplier - float(other)
60 def __eq__(self, other):
61 return self.multiplier == float(other)
63 def __ne__(self, other):
64 return not self.__eq__(other)
66 def __lt__(self, other):
67 return self.multiplier < float(other)
69 def __gt__(self, other):
70 return self.multiplier > float(other)
72 def __le__(self, other):
73 return self < other or self == other
75 def __ge__(self, other):
76 return self > other or self == other
79 return self.multiplier
81 def __repr__(self, *, relative=False, places=3):
83 percentage = (self.multiplier - 1.0) * 100.0
85 percentage = self.multiplier * 100.0
86 return f'{percentage:+.{places}f}%'