Change weights.
[python_utils.git] / type / money.py
index d7e6ffa2197c629949ecae30c855df5870cc3a3a..b84652b75b1f594c98566ff54fe0105a5b1fa810 100644 (file)
@@ -1,8 +1,10 @@
 #!/usr/bin/env python3
 
+"""A class to represent money.  See also centcount.py"""
+
 import re
 from decimal import Decimal
-from typing import Optional, Tuple, TypeVar
+from typing import Optional, Tuple
 
 import math_utils
 
@@ -39,9 +41,9 @@ class Money(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 Money(amount=self.amount, currency=self.currency)
@@ -60,7 +62,8 @@ 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):
@@ -74,7 +77,8 @@ 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):
@@ -82,7 +86,8 @@ 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):
@@ -90,7 +95,8 @@ 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):
@@ -119,7 +125,8 @@ 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__
@@ -173,8 +180,8 @@ class Money(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__)
 
     AMOUNT_RE = re.compile(r"^([+|-]?)(\d+)(\.\d+)$")
     CURRENCY_RE = re.compile(r"^[A-Z][A-Z][A-Z]$")