X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=type%2Fcentcount.py;h=e78d068910e8281cbf28ee8674e7b6cff44f89fd;hb=532df2c5b57c7517dfb3dddd8c1358fbadf8baf3;hp=13f14b7f835d2e15679ea3bddd9499398056448e;hpb=6ba90a1f30f1c0cf4df12fcd0c62181f29bc3668;p=python_utils.git diff --git a/type/centcount.py b/type/centcount.py index 13f14b7..e78d068 100644 --- a/type/centcount.py +++ b/type/centcount.py @@ -1,7 +1,12 @@ #!/usr/bin/env python3 +# © Copyright 2021-2022, Scott Gasch + +"""An amount of money (USD) represented as an integral count of +cents.""" + import re -from typing import Optional, Tuple, TypeVar +from typing import Optional, Tuple import math_utils @@ -36,9 +41,9 @@ class CentCount(object): a = round(a, 2) s = f'{a:,.2f}' if self.currency is not None: - return '%s %s' % (s, self.currency) + return f'{s} {self.currency}' else: - return '$%s' % s + return f'${s}' def __pos__(self): return CentCount(centcount=self.centcount, currency=self.currency) @@ -50,7 +55,8 @@ class CentCount(object): if isinstance(other, CentCount): if self.currency == other.currency: return CentCount( - centcount=self.centcount + other.centcount, currency=self.currency + centcount=self.centcount + other.centcount, + currency=self.currency, ) else: raise TypeError('Incompatible currencies in add expression') @@ -64,7 +70,8 @@ class CentCount(object): if isinstance(other, CentCount): if self.currency == other.currency: return CentCount( - centcount=self.centcount - other.centcount, currency=self.currency + centcount=self.centcount - other.centcount, + currency=self.currency, ) else: raise TypeError('Incompatible currencies in add expression') @@ -79,7 +86,8 @@ class CentCount(object): raise TypeError('can not multiply monetary quantities') else: return CentCount( - centcount=int(self.centcount * float(other)), currency=self.currency + centcount=int(self.centcount * float(other)), + currency=self.currency, ) def __truediv__(self, other): @@ -113,7 +121,8 @@ class CentCount(object): if isinstance(other, CentCount): if self.currency == other.currency: return CentCount( - centcount=other.centcount - self.centcount, currency=self.currency + centcount=other.centcount - self.centcount, + currency=self.currency, ) else: raise TypeError('Incompatible currencies in sub expression') @@ -122,7 +131,8 @@ class CentCount(object): raise TypeError('In strict_mode only two moneys can be added') else: return CentCount( - centcount=int(other) - self.centcount, currency=self.currency + centcount=int(other) - self.centcount, + currency=self.currency, ) __rmul__ = __mul__ @@ -176,8 +186,8 @@ class CentCount(object): def __ge__(self, other): return self > other or self == other - def __hash__(self): - return self.__repr__ + def __hash__(self) -> int: + return hash(self.__repr__) CENTCOUNT_RE = re.compile(r"^([+|-]?)(\d+)(\.\d+)$") CURRENCY_RE = re.compile(r"^[A-Z][A-Z][A-Z]$")