summaryrefslogtreecommitdiff
path: root/talerbank/app/amount.py
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2017-12-06 17:51:55 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2017-12-06 17:51:55 +0100
commit4efff7788f052cfdd2949af9198eb2c18a4cd4e8 (patch)
tree04cf19060f3c3d029af5290e2165b0b33432c695 /talerbank/app/amount.py
parente10115be96dfcbbfae2c0d004e5498a224ba9a3b (diff)
downloadbank-4efff7788f052cfdd2949af9198eb2c18a4cd4e8.tar.gz
bank-4efff7788f052cfdd2949af9198eb2c18a4cd4e8.tar.bz2
bank-4efff7788f052cfdd2949af9198eb2c18a4cd4e8.zip
annotating types for config parser
Diffstat (limited to 'talerbank/app/amount.py')
-rw-r--r--talerbank/app/amount.py29
1 files changed, 15 insertions, 14 deletions
diff --git a/talerbank/app/amount.py b/talerbank/app/amount.py
index 46e3446..45e306e 100644
--- a/talerbank/app/amount.py
+++ b/talerbank/app/amount.py
@@ -22,13 +22,15 @@
# mentioned above, and it is meant to be manually copied into any project
# which might need it.
+from typing import Type
+
class CurrencyMismatch(Exception):
- def __init__(self, curr1, curr2):
+ def __init__(self, curr1, curr2) -> None:
super(CurrencyMismatch, self).__init__(
"%s vs %s" % (curr1, curr2))
class BadFormatAmount(Exception):
- def __init__(self, faulty_str):
+ def __init__(self, faulty_str) -> None:
super(BadFormatAmount, self).__init__(
"Bad format amount: " + faulty_str)
@@ -36,15 +38,14 @@ class Amount:
# How many "fraction" units make one "value" unit of currency
# (Taler requires 10^8). Do not change this 'constant'.
@staticmethod
- def _fraction():
+ def _fraction() -> int:
return 10 ** 8
@staticmethod
- def _max_value():
+ def _max_value() -> int:
return (2 ** 53) - 1
- def __init__(self, currency, value=0, fraction=0):
- # type: (str, int, int) -> Amount
+ def __init__(self, currency, value=0, fraction=0) -> None:
assert value >= 0 and fraction >= 0
self.value = value
self.fraction = fraction
@@ -53,7 +54,7 @@ class Amount:
assert self.value <= Amount._max_value()
# Normalize amount
- def __normalize(self):
+ def __normalize(self) -> None:
if self.fraction >= Amount._fraction():
self.value += int(self.fraction / Amount._fraction())
self.fraction = self.fraction % Amount._fraction()
@@ -61,7 +62,7 @@ class Amount:
# Parse a string matching the format "A:B.C"
# instantiating an amount object.
@classmethod
- def parse(cls, amount_str):
+ def parse(cls: Type[Amount], amount_str: str) -> Amount:
exp = r'^\s*([-_*A-Za-z0-9]+):([0-9]+)\.([0-9]+)\s*$'
import re
parsed = re.search(exp, amount_str)
@@ -78,7 +79,7 @@ class Amount:
# 0 if a == b
# 1 if a > b
@staticmethod
- def cmp(am1, am2):
+ def cmp(am1: Amount, am2: Amount) -> int:
if am1.currency != am2.currency:
raise CurrencyMismatch(am1.currency, am2.currency)
if am1.value == am2.value:
@@ -91,13 +92,13 @@ class Amount:
return -1
return 1
- def set(self, currency, value=0, fraction=0):
+ def set(self, currency: str, value=0, fraction=0) -> None:
self.currency = currency
self.value = value
self.fraction = fraction
# Add the given amount to this one
- def add(self, amount):
+ def add(self, amount: Amount) -> None:
if self.currency != amount.currency:
raise CurrencyMismatch(self.currency, amount.currency)
self.value += amount.value
@@ -105,7 +106,7 @@ class Amount:
self.__normalize()
# Subtract passed amount from this one
- def subtract(self, amount):
+ def subtract(self, amount: Amount) -> None:
if self.currency != amount.currency:
raise CurrencyMismatch(self.currency, amount.currency)
if self.fraction < amount.fraction:
@@ -118,7 +119,7 @@ class Amount:
# Dump string from this amount, will put 'ndigits' numbers
# after the dot.
- def stringify(self, ndigits):
+ def stringify(self, ndigits: int) -> str:
assert ndigits > 0
ret = '%s:%s.' % (self.currency, str(self.value))
fraction = self.fraction
@@ -129,7 +130,7 @@ class Amount:
return ret
# Dump the Taler-compliant 'dict' amount
- def dump(self):
+ def dump(self) -> dict:
return dict(value=self.value,
fraction=self.fraction,
currency=self.currency)