summaryrefslogtreecommitdiff
path: root/talersurvey/survey/amount.py
diff options
context:
space:
mode:
Diffstat (limited to 'talersurvey/survey/amount.py')
-rw-r--r--talersurvey/survey/amount.py135
1 files changed, 0 insertions, 135 deletions
diff --git a/talersurvey/survey/amount.py b/talersurvey/survey/amount.py
deleted file mode 100644
index 46e3446..0000000
--- a/talersurvey/survey/amount.py
+++ /dev/null
@@ -1,135 +0,0 @@
-# This file is part of TALER
-# (C) 2017 TALER SYSTEMS
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-#
-# @author Marcello Stanisci
-# @version 0.1
-# @repository https://git.taler.net/copylib.git/
-# This code is "copylib", it is versioned under the Git repository
-# mentioned above, and it is meant to be manually copied into any project
-# which might need it.
-
-class CurrencyMismatch(Exception):
- def __init__(self, curr1, curr2):
- super(CurrencyMismatch, self).__init__(
- "%s vs %s" % (curr1, curr2))
-
-class BadFormatAmount(Exception):
- def __init__(self, faulty_str):
- super(BadFormatAmount, self).__init__(
- "Bad format amount: " + faulty_str)
-
-class Amount:
- # How many "fraction" units make one "value" unit of currency
- # (Taler requires 10^8). Do not change this 'constant'.
- @staticmethod
- def _fraction():
- return 10 ** 8
-
- @staticmethod
- def _max_value():
- return (2 ** 53) - 1
-
- def __init__(self, currency, value=0, fraction=0):
- # type: (str, int, int) -> Amount
- assert value >= 0 and fraction >= 0
- self.value = value
- self.fraction = fraction
- self.currency = currency
- self.__normalize()
- assert self.value <= Amount._max_value()
-
- # Normalize amount
- def __normalize(self):
- if self.fraction >= Amount._fraction():
- self.value += int(self.fraction / Amount._fraction())
- self.fraction = self.fraction % Amount._fraction()
-
- # Parse a string matching the format "A:B.C"
- # instantiating an amount object.
- @classmethod
- def parse(cls, amount_str):
- exp = r'^\s*([-_*A-Za-z0-9]+):([0-9]+)\.([0-9]+)\s*$'
- import re
- parsed = re.search(exp, amount_str)
- if not parsed:
- raise BadFormatAmount(amount_str)
- value = int(parsed.group(2))
- fraction = 0
- for i, digit in enumerate(parsed.group(3)):
- fraction += int(int(digit) * (Amount._fraction() / 10 ** (i+1)))
- return cls(parsed.group(1), value, fraction)
-
- # Comare two amounts, return:
- # -1 if a < b
- # 0 if a == b
- # 1 if a > b
- @staticmethod
- def cmp(am1, am2):
- if am1.currency != am2.currency:
- raise CurrencyMismatch(am1.currency, am2.currency)
- if am1.value == am2.value:
- if am1.fraction < am2.fraction:
- return -1
- if am1.fraction > am2.fraction:
- return 1
- return 0
- if am1.value < am2.value:
- return -1
- return 1
-
- def set(self, currency, value=0, fraction=0):
- self.currency = currency
- self.value = value
- self.fraction = fraction
-
- # Add the given amount to this one
- def add(self, amount):
- if self.currency != amount.currency:
- raise CurrencyMismatch(self.currency, amount.currency)
- self.value += amount.value
- self.fraction += amount.fraction
- self.__normalize()
-
- # Subtract passed amount from this one
- def subtract(self, amount):
- if self.currency != amount.currency:
- raise CurrencyMismatch(self.currency, amount.currency)
- if self.fraction < amount.fraction:
- self.fraction += Amount._fraction()
- self.value -= 1
- if self.value < amount.value:
- raise ValueError('self is lesser than amount to be subtracted')
- self.value -= amount.value
- self.fraction -= amount.fraction
-
- # Dump string from this amount, will put 'ndigits' numbers
- # after the dot.
- def stringify(self, ndigits):
- assert ndigits > 0
- ret = '%s:%s.' % (self.currency, str(self.value))
- fraction = self.fraction
- while ndigits > 0:
- ret += str(int(fraction / (Amount._fraction() / 10)))
- fraction = (fraction * 10) % (Amount._fraction())
- ndigits -= 1
- return ret
-
- # Dump the Taler-compliant 'dict' amount
- def dump(self):
- return dict(value=self.value,
- fraction=self.fraction,
- currency=self.currency)