lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.4-5-g1622873

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.4-5-g1622873



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  1622873beb762ff31ce45b0356edb7c7ab834dd8 (commit)
       via  7d969bee07169a4c3f412bf81f8158ce37fbe247 (commit)
       via  294f8721799f6562b7d7f3f31a68f25cb24c964f (commit)
      from  466cec8b4022e7b6c11f7d387580d9d05f2e6f22 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://arthurdejong.org/git/python-stdnum/commit/?id=1622873beb762ff31ce45b0356edb7c7ab834dd8

commit 1622873beb762ff31ce45b0356edb7c7ab834dd8
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Sep 6 19:43:46 2016 +0200

    Add to_iban() function to Spanish CCC

diff --git a/stdnum/es/ccc.py b/stdnum/es/ccc.py
index 3f69ff9..3800bb3 100644
--- a/stdnum/es/ccc.py
+++ b/stdnum/es/ccc.py
@@ -58,6 +58,8 @@ Traceback (most recent call last):
 InvalidChecksum: ...
 >>> format('12341234161234567890')
 '1234 1234 16 12345 67890'
+>>> to_iban('21000418450200051331')
+'ES2121000418450200051331'
 """
 
 from stdnum.exceptions import *
@@ -114,3 +116,12 @@ def is_valid(number):
         return bool(validate(number))
     except ValidationError:
         return False
+
+
+def to_iban(number):
+    """Convert the number to an IBAN."""
+    from stdnum import iban
+    separator = ' ' if ' ' in number else ''
+    return separator.join((
+        'ES' + iban.calc_check_digits('ES00' + number),
+        number))

http://arthurdejong.org/git/python-stdnum/commit/?id=7d969bee07169a4c3f412bf81f8158ce37fbe247

commit 7d969bee07169a4c3f412bf81f8158ce37fbe247
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Thu Sep 8 18:54:46 2016 +0200

    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.

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

http://arthurdejong.org/git/python-stdnum/commit/?id=294f8721799f6562b7d7f3f31a68f25cb24c964f

commit 294f8721799f6562b7d7f3f31a68f25cb24c964f
Author: David García Garzón <voki@canvoki.net>
Date:   Wed Aug 31 20:21:52 2016 +0200

    Add Spanish Código Cuenta Corriente (CCC)

diff --git a/stdnum/es/ccc.py b/stdnum/es/ccc.py
new file mode 100644
index 0000000..3f69ff9
--- /dev/null
+++ b/stdnum/es/ccc.py
@@ -0,0 +1,116 @@
+# ccc.py - functions for handling Spanish CCC bank account code
+# coding: utf-8
+#
+# Copyright (C) 2016 David García Garzón
+# Copyright (C) 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
+# 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
+
+"""CCC (Código Cuenta Corriente, Spanish Bank Account Code)
+
+CCC code is the country-specific part in Spanish IBAN codes. In order to
+fully validate an Spanish IBAN you have to validate as well the country
+specific part as a valid CCC. It was used for home banking transactions until
+February 1st 2014 when IBAN codes started to be used as an account ID.
+
+The CCC has 20 digits, all being numbers: EEEE OOOO DD NNNNNNNNNN
+
+* EEEE: banking entity
+* OOOO: office
+* DD: check digits
+* NNNNN NNNNN: account identifier
+
+This module does not check if the bank code to exist. Existing bank codes are
+published on the 'Registro de Entidades' by 'Banco de España' (Spanish
+Central Bank).
+
+More information:
+
+* https://es.wikipedia.org/wiki/Código_cuenta_cliente
+* 
http://www.bde.es/bde/es/secciones/servicios/Particulares_y_e/Registros_de_Ent/
+
+>>> validate('1234-1234-16 1234567890')
+'12341234161234567890'
+>>> validate('134-1234-16 1234567890')  # wrong length
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> validate('12X4-1234-16 1234567890')  # non numbers
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> validate('1234-1234-00 1234567890')  # invalid check digits
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> format('12341234161234567890')
+'1234 1234 16 12345 67890'
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    return clean(number, ' -').strip().upper()
+
+
+def format(number):
+    """Reformat the passed number to the standard format."""
+    number = compact(number)
+    return ' '.join([
+        number[0:4],
+        number[4:8],
+        number[8:10],
+        number[10:15],
+        number[15:20],
+    ])
+
+
+def _calc_check_digit(number):
+    """Calculate a single check digit on the provided part of the number."""
+    check = sum(int(n) * 2 ** i for i, n in enumerate(number)) % 11
+    return str(check if check < 2 else 11 - check)
+
+
+def calc_check_digits(number):
+    """Calculate the check digits for the number. The supplied number should
+    have check digits included but are ignored."""
+    number = compact(number)
+    return (
+        _calc_check_digit('00' + number[:8]) + _calc_check_digit(number[10:]))
+
+
+def validate(number):
+    """Checks to see if the number provided is a valid CCC."""
+    number = compact(number)
+    if len(number) != 20:
+        raise InvalidLength()
+    if not number.isdigit():
+        raise InvalidFormat()
+    if number[8:10] != calc_check_digits(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid CCC."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False

-----------------------------------------------------------------------

Summary of changes:
 stdnum/es/ccc.py | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 stdnum/iban.py   |  25 ++++++++---
 2 files changed, 145 insertions(+), 7 deletions(-)
 create mode 100644 stdnum/es/ccc.py


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
https://lists.arthurdejong.org/python-stdnum-commits/