summaryrefslogtreecommitdiff
path: root/type/rate.py
blob: 58489061570ab3a1a6f96728c04361ad760e21d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3

# © Copyright 2021-2022, Scott Gasch

"""A class to represent a rate of change."""

from typing import Optional


class Rate(object):
    """A class to represent a rate of change."""

    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: float = 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}%'