summaryrefslogtreecommitdiff
path: root/type
diff options
context:
space:
mode:
authorScott <[email protected]>2022-02-02 13:09:30 -0800
committerScott <[email protected]>2022-02-02 13:09:30 -0800
commit6ba90a1f30f1c0cf4df12fcd0c62181f29bc3668 (patch)
tree6c4d558173ed76df50f7b2f88af8f99119d932ab /type
parent31c81f6539969a5eba864d3305f9fb7bf716a367 (diff)
Make subdirs type clean too.
Diffstat (limited to 'type')
-rw-r--r--type/centcount.py45
-rw-r--r--type/money.py62
-rw-r--r--type/rate.py17
3 files changed, 42 insertions, 82 deletions
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])
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])
diff --git a/type/rate.py b/type/rate.py
index 3161131..64a4726 100644
--- a/type/rate.py
+++ b/type/rate.py
@@ -5,11 +5,11 @@ from typing import Optional
class Rate(object):
def __init__(
- self,
- multiplier: Optional[float] = None,
- *,
- percentage: Optional[float] = None,
- percent_change: Optional[float] = None,
+ self,
+ multiplier: Optional[float] = None,
+ *,
+ percentage: Optional[float] = None,
+ percent_change: Optional[float] = None,
):
count = 0
if multiplier is not None:
@@ -17,7 +17,7 @@ class Rate(object):
multiplier = multiplier.replace('%', '')
m = float(multiplier)
m /= 100
- self.multiplier = m
+ self.multiplier: float = m
else:
self.multiplier = multiplier
count += 1
@@ -78,10 +78,7 @@ class Rate(object):
def __hash__(self):
return self.multiplier
- def __repr__(self,
- *,
- relative=False,
- places=3):
+ def __repr__(self, *, relative=False, places=3):
if relative:
percentage = (self.multiplier - 1.0) * 100.0
else: