X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=type%2Fmoney.py;fp=type%2Fmoney.py;h=d7e6ffa2197c629949ecae30c855df5870cc3a3a;hb=6ba90a1f30f1c0cf4df12fcd0c62181f29bc3668;hp=290c2c86f91a7a6da652b21e670d2579c51ba6c0;hpb=31c81f6539969a5eba864d3305f9fb7bf716a367;p=python_utils.git diff --git a/type/money.py b/type/money.py index 290c2c8..d7e6ffa 100644 --- a/type/money.py +++ b/type/money.py @@ -1,26 +1,23 @@ #!/usr/bin/env python3 -from decimal import Decimal import re -from typing import Optional, TypeVar, Tuple +from decimal import Decimal +from typing import Optional, Tuple, TypeVar import math_utils -T = TypeVar('T', bound='Money') - - class Money(object): """A class for representing monetary amounts potentially with different currencies. """ - def __init__ ( - self, - amount: Decimal = Decimal("0"), - currency: str = 'USD', - *, - strict_mode = False + def __init__( + self, + amount: Decimal = Decimal("0"), + currency: str = 'USD', + *, + strict_mode=False, ): self.strict_mode = strict_mode if isinstance(amount, str): @@ -35,7 +32,7 @@ class Money(object): if not currency: self.currency: Optional[str] = None else: - self.currency: Optional[str] = currency + self.currency = currency def __repr__(self): a = float(self.amount) @@ -55,10 +52,7 @@ class Money(object): def __add__(self, other): if isinstance(other, Money): if self.currency == other.currency: - return Money( - amount = self.amount + other.amount, - currency = self.currency - ) + return Money(amount=self.amount + other.amount, currency=self.currency) else: raise TypeError('Incompatible currencies in add expression') else: @@ -66,17 +60,13 @@ class Money(object): raise TypeError('In strict_mode only two moneys can be added') else: return Money( - amount = self.amount + Decimal(float(other)), - currency = self.currency + amount=self.amount + Decimal(float(other)), currency=self.currency ) def __sub__(self, other): if isinstance(other, Money): if self.currency == other.currency: - return Money( - amount = self.amount - other.amount, - currency = self.currency - ) + return Money(amount=self.amount - other.amount, currency=self.currency) else: raise TypeError('Incompatible currencies in add expression') else: @@ -84,8 +74,7 @@ class Money(object): raise TypeError('In strict_mode only two moneys can be added') else: return Money( - amount = self.amount - Decimal(float(other)), - currency = self.currency + amount=self.amount - Decimal(float(other)), currency=self.currency ) def __mul__(self, other): @@ -93,8 +82,7 @@ class Money(object): raise TypeError('can not multiply monetary quantities') else: return Money( - amount = self.amount * Decimal(float(other)), - currency = self.currency + amount=self.amount * Decimal(float(other)), currency=self.currency ) def __truediv__(self, other): @@ -102,8 +90,7 @@ class Money(object): raise TypeError('can not divide monetary quantities') else: return Money( - amount = self.amount / Decimal(float(other)), - currency = self.currency + amount=self.amount / Decimal(float(other)), currency=self.currency ) def __float__(self): @@ -124,10 +111,7 @@ class Money(object): def __rsub__(self, other): if isinstance(other, Money): if self.currency == other.currency: - return Money( - amount = other.amount - self.amount, - currency = self.currency - ) + return Money(amount=other.amount - self.amount, currency=self.currency) else: raise TypeError('Incompatible currencies in sub expression') else: @@ -135,8 +119,7 @@ class Money(object): raise TypeError('In strict_mode only two moneys can be added') else: return Money( - amount = Decimal(float(other)) - self.amount, - currency = self.currency + amount=Decimal(float(other)) - self.amount, currency=self.currency ) __rmul__ = __mul__ @@ -148,10 +131,7 @@ class Money(object): if other is None: return False if isinstance(other, Money): - return ( - self.amount == other.amount and - self.currency == other.currency - ) + return self.amount == other.amount and self.currency == other.currency if self.strict_mode: raise TypeError("In strict mode only two Moneys can be compared") else: @@ -196,8 +176,8 @@ class Money(object): def __hash__(self): return self.__repr__ - AMOUNT_RE = re.compile("^([+|-]?)(\d+)(\.\d+)$") - CURRENCY_RE = re.compile("^[A-Z][A-Z][A-Z]$") + AMOUNT_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[Decimal, str]]: @@ -220,7 +200,7 @@ class Money(object): return None @classmethod - def parse(cls, s: str) -> T: + def parse(cls, s: str) -> 'Money': chunks = Money._parse(s) if chunks is not None: return Money(chunks[0], chunks[1])