X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=type%2Fcentcount.py;fp=type%2Fcentcount.py;h=13f14b7f835d2e15679ea3bddd9499398056448e;hb=6ba90a1f30f1c0cf4df12fcd0c62181f29bc3668;hp=4e5b8a6aa6b4b8ef61671b93b0284825cdf75cbc;hpb=31c81f6539969a5eba864d3305f9fb7bf716a367;p=python_utils.git diff --git a/type/centcount.py b/type/centcount.py index 4e5b8a6..13f14b7 100644 --- a/type/centcount.py +++ b/type/centcount.py @@ -1,27 +1,18 @@ #!/usr/bin/env python3 import re -from typing import Optional, TypeVar, Tuple +from typing import Optional, Tuple, TypeVar import math_utils -T = TypeVar('T', bound='CentCount') - - class CentCount(object): """A class for representing monetary amounts potentially with different currencies meant to avoid floating point rounding issues by treating amount as a simple integral count of cents. """ - def __init__ ( - self, - centcount, - currency: str = 'USD', - *, - strict_mode = False - ): + def __init__(self, centcount, currency: str = 'USD', *, strict_mode=False): self.strict_mode = strict_mode if isinstance(centcount, str): ret = CentCount._parse(centcount) @@ -37,7 +28,7 @@ class CentCount(object): if not currency: self.currency: Optional[str] = None else: - self.currency: Optional[str] = currency + self.currency = currency def __repr__(self): a = float(self.centcount) @@ -59,8 +50,7 @@ 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') @@ -74,8 +64,7 @@ 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') @@ -90,8 +79,7 @@ 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): @@ -99,8 +87,8 @@ class CentCount(object): raise TypeError('can not divide monetary quantities') else: return CentCount( - centcount = int(float(self.centcount) / float(other)), - currency = self.currency + centcount=int(float(self.centcount) / float(other)), + currency=self.currency, ) def __int__(self): @@ -125,8 +113,7 @@ 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') @@ -135,8 +122,7 @@ 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__ @@ -148,10 +134,7 @@ class CentCount(object): if other is None: return False if isinstance(other, CentCount): - return ( - self.centcount == other.centcount and - self.currency == other.currency - ) + return self.centcount == other.centcount and self.currency == other.currency if self.strict_mode: raise TypeError("In strict mode only two CentCounts can be compared") else: @@ -196,8 +179,8 @@ class CentCount(object): def __hash__(self): return self.__repr__ - CENTCOUNT_RE = re.compile("^([+|-]?)(\d+)(\.\d+)$") - CURRENCY_RE = re.compile("^[A-Z][A-Z][A-Z]$") + CENTCOUNT_RE = re.compile(r"^([+|-]?)(\d+)(\.\d+)$") + CURRENCY_RE = re.compile(r"^[A-Z][A-Z][A-Z]$") @classmethod def _parse(cls, s: str) -> Optional[Tuple[int, str]]: @@ -220,7 +203,7 @@ class CentCount(object): return None @classmethod - def parse(cls, s: str) -> T: + def parse(cls, s: str) -> 'CentCount': chunks = CentCount._parse(s) if chunks is not None: return CentCount(chunks[0], chunks[1])