Migration from old pyutilz package name (which, in turn, came from
[pyutils.git] / src / pyutils / typez / rate.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """A class to represent a rate of change."""
6
7 from typing import Optional
8
9
10 class Rate(object):
11     """A class to represent a rate of change."""
12
13     def __init__(
14         self,
15         multiplier: Optional[float] = None,
16         *,
17         percentage: Optional[float] = None,
18         percent_change: Optional[float] = None,
19     ):
20         count = 0
21         if multiplier is not None:
22             if isinstance(multiplier, str):
23                 multiplier = multiplier.replace('%', '')
24                 m = float(multiplier)
25                 m /= 100
26                 self.multiplier: float = m
27             else:
28                 self.multiplier = multiplier
29             count += 1
30         if percentage is not None:
31             self.multiplier = percentage / 100
32             count += 1
33         if percent_change is not None:
34             self.multiplier = 1.0 + percent_change / 100
35             count += 1
36         if count != 1:
37             raise Exception(
38                 'Exactly one of percentage, percent_change or multiplier is required.'
39             )
40
41     def apply_to(self, other):
42         return self.__mul__(other)
43
44     def of(self, other):
45         return self.__mul__(other)
46
47     def __float__(self):
48         return self.multiplier
49
50     def __mul__(self, other):
51         return self.multiplier * float(other)
52
53     __rmul__ = __mul__
54
55     def __truediv__(self, other):
56         return self.multiplier / float(other)
57
58     def __add__(self, other):
59         return self.multiplier + float(other)
60
61     __radd__ = __add__
62
63     def __sub__(self, other):
64         return self.multiplier - float(other)
65
66     def __eq__(self, other):
67         return self.multiplier == float(other)
68
69     def __ne__(self, other):
70         return not self.__eq__(other)
71
72     def __lt__(self, other):
73         return self.multiplier < float(other)
74
75     def __gt__(self, other):
76         return self.multiplier > float(other)
77
78     def __le__(self, other):
79         return self < other or self == other
80
81     def __ge__(self, other):
82         return self > other or self == other
83
84     def __hash__(self):
85         return self.multiplier
86
87     def __repr__(self, *, relative=False, places=3):
88         if relative:
89             percentage = (self.multiplier - 1.0) * 100.0
90         else:
91             percentage = self.multiplier * 100.0
92         return f'{percentage:+.{places}f}%'