Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2016-09-08 18:54:46 +0200
committerArthur de Jong <arthur@arthurdejong.org>2016-09-10 15:58:06 +0200
commit7d969bee07169a4c3f412bf81f8158ce37fbe247 (patch)
tree0340e9a0f939a9cdd587269fbfaa1d0eac9a9c6c
parent294f8721799f6562b7d7f3f31a68f25cb24c964f (diff)
Implement calc_check_digits() in IBAN
Introduce a function to calculate the two check digits of an IBAN. Since the check digits are the third and fourth characters in the number, placeholders need to be provided when calling this function.
-rw-r--r--stdnum/iban.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/stdnum/iban.py b/stdnum/iban.py
index ae01a02..e0d4898 100644
--- a/stdnum/iban.py
+++ b/stdnum/iban.py
@@ -1,6 +1,6 @@
# iban.py - functions for handling International Bank Account Numbers (IBANs)
#
-# Copyright (C) 2011-2015 Arthur de Jong
+# Copyright (C) 2011-2016 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -40,6 +40,8 @@ More information:
'GR1601101050000010547023795'
>>> format('GR1601101050000010547023795')
'GR16 0110 1050 0000 1054 7023 795'
+>>> calc_check_digits('BExx435411161155')
+'31'
"""
import re
@@ -71,7 +73,20 @@ def _to_base10(number):
check digits to the end) so it can be checked with the ISO 7064
Mod 97, 10 algorithm."""
# TODO: find out whether this should be in the mod_97_10 module
- return ''.join(str(_alphabet.index(x)) for x in number[4:] + number[:4])
+ try:
+ return ''.join(
+ str(_alphabet.index(x)) for x in number[4:] + number[:4])
+ except Exception:
+ raise InvalidFormat()
+
+
+def calc_check_digits(number):
+ """Calculate the check digits that should be put in the number to make
+ it valid. Check digits in the supplied number are ignored.."""
+ number = compact(number)
+ # replace check digits with placeholders
+ number = ''.join((number[:2], '00', number[4:]))
+ return mod_97_10.calc_check_digits(_to_base10(number)[:-2])
def _struct_to_re(structure):
@@ -90,12 +105,8 @@ def _struct_to_re(structure):
def validate(number):
"""Checks to see if the number provided is a valid IBAN."""
number = compact(number)
- try:
- test_number = _to_base10(number)
- except Exception:
- raise InvalidFormat()
# ensure that checksum is valid
- mod_97_10.validate(test_number)
+ mod_97_10.validate(_to_base10(number))
# look up the number
info = _ibandb.info(number)
# check if the bban part of number has the correct structure