lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.3-25-g3e344d1

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

python-stdnum branch master updated. 1.3-25-g3e344d1



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  3e344d1e51b7abd8a68d3688973e7d0215ba55b5 (commit)
       via  aa1c298cf706eda127b07a2c2d6705fff423b569 (commit)
       via  411d0381331334dd6b739f8d0527b56af741a90a (commit)
       via  06e41651623798b0bffe37b141374e9ef56c8102 (commit)
       via  1907c67e756ce5f38422ab75fa1de98fd8368ddc (commit)
       via  cf428ac4c2f7f8d36042860e02e7d5b929b24b8f (commit)
       via  011c0f0c4b1d2e08f2987de1cc9c2cfb16e39998 (commit)
      from  2409ee943859b11539be67b11da078e15e2bdc0d (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=3e344d1e51b7abd8a68d3688973e7d0215ba55b5

commit 3e344d1e51b7abd8a68d3688973e7d0215ba55b5
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 21:56:08 2016 +0200

    Add formatting of ISO 6346 container codes

diff --git a/stdnum/iso6346.py b/stdnum/iso6346.py
index fd6f1c9..1956dfc 100644
--- a/stdnum/iso6346.py
+++ b/stdnum/iso6346.py
@@ -38,6 +38,8 @@ More information:
 Traceback (most recent call last):
     ...
 InvalidChecksum: ...
+>>> format('tasu117 000 0')
+'TASU 117000 0'
 """
 
 import re
@@ -53,7 +55,7 @@ _iso6346_re = re.compile(r'^\w{3}(U|J|Z|R)\d{7}$')
 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()
+    return clean(number, ' ').strip().upper()
 
 
 def calc_check_digit(number):
@@ -85,3 +87,9 @@ def is_valid(number):
         return bool(validate(number))
     except ValidationError:
         return False
+
+
+def format(number):
+    """Reformat the passed number to the standard format."""
+    number = compact(number)
+    return ' '.join((number[:4], number[4:-1], number[-1:]))

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

commit aa1c298cf706eda127b07a2c2d6705fff423b569
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 21:38:15 2016 +0200

    Implement Damm algorithm
    
    This is a generic check digit algorithm.

diff --git a/getnumlist.py b/getnumlist.py
index 4c74479..c8b9b4d 100755
--- a/getnumlist.py
+++ b/getnumlist.py
@@ -28,7 +28,8 @@ from stdnum import util
 
 
 # these are excluded
-algortihms = ('stdnum.verhoeff', 'stdnum.luhn', 'stdnum.iso7064')
+algortihms = (
+    'stdnum.verhoeff', 'stdnum.luhn', 'stdnum.iso7064', 'stdnum.damm')
 
 
 def get_number_modules():
@@ -36,7 +37,7 @@ def get_number_modules():
     for module in util.get_number_modules():
         if module.__name__ not in algortihms and \
            not module.__name__.startswith('stdnum.iso7064'):
-             yield module
+            yield module
 
 if __name__ == '__main__':
     print 'For README:'
diff --git a/stdnum/damm.py b/stdnum/damm.py
new file mode 100644
index 0000000..dc93151
--- /dev/null
+++ b/stdnum/damm.py
@@ -0,0 +1,93 @@
+# damm.py - functions for performing the Damm checksum algorithm
+#
+# 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
+
+"""The Damm algorithm.
+
+The Damm algorithm is a check digit algorithm that should detect all
+single-digit errors and all adjacent transposition errors. Based on
+anti-symmetric quasigroup of order 10 it uses a substitution table.
+
+This implementation uses the table from Wikipedia by default but a custom
+table can be provided.
+
+More information:
+
+* https://en.wikipedia.org/wiki/Damm_algorithm
+
+>>> validate('572')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> calc_check_digit('572')
+'4'
+>>> validate('5724')
+'5724'
+"""
+
+from stdnum.exceptions import *
+
+
+_operation_table = (
+    (0, 3, 1, 7, 5, 9, 8, 6, 4, 2),
+    (7, 0, 9, 2, 1, 5, 4, 8, 6, 3),
+    (4, 2, 0, 6, 8, 7, 1, 3, 5, 9),
+    (1, 7, 5, 0, 9, 8, 3, 4, 2, 6),
+    (6, 1, 2, 3, 0, 4, 5, 9, 7, 8),
+    (3, 6, 7, 4, 2, 0, 9, 5, 8, 1),
+    (5, 8, 6, 9, 7, 2, 0, 1, 3, 4),
+    (8, 9, 4, 5, 3, 6, 2, 0, 1, 7),
+    (9, 4, 3, 8, 6, 1, 7, 2, 0, 5),
+    (2, 5, 8, 1, 4, 3, 6, 7, 9, 0))
+
+
+def checksum(number, table=None):
+    """Calculate the Damm checksum over the provided number. The checksum is
+    returned as an integer value and should be 0 when valid."""
+    table = table or _operation_table
+    i = 0
+    for n in str(number):
+        i = table[i][int(n)]
+    return i
+
+
+def validate(number, table=None):
+    """Checks to see if the number provided passes the Damm algorithm."""
+    if not bool(number):
+        raise InvalidFormat()
+    try:
+        valid = checksum(number, table=table) == 0
+    except Exception:
+        raise InvalidFormat()
+    if not valid:
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number, table=None):
+    """Checks to see if the number provided passes the Damm algorithm."""
+    try:
+        return bool(validate(number), table=table)
+    except ValidationError:
+        return False
+
+
+def calc_check_digit(number, table=None):
+    """With the provided number, calculate the extra digit that should be
+    appended to make it pass the Damm check."""
+    return str(checksum(number, table=table))
diff --git a/tests/test_damm.doctest b/tests/test_damm.doctest
new file mode 100644
index 0000000..13d8c23
--- /dev/null
+++ b/tests/test_damm.doctest
@@ -0,0 +1,93 @@
+test_damm.doctest - more detailed doctests for stdnum.damm module
+
+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
+
+
+This file contains more detailed doctests for the stdnum.damm module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import damm
+
+
+These are normal variations that should just work. Calculating checksums:
+
+>>> damm.checksum('572')
+4
+>>> damm.checksum('5724')
+0
+>>> damm.checksum('43881234567')
+9
+>>> damm.checksum('438812345679')
+0
+
+
+The same numbers but now simply ask for validation:
+
+>>> damm.validate('572')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> damm.validate('5724')
+'5724'
+>>> damm.validate('43881234567')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> damm.validate('438812345679')
+'438812345679'
+
+
+Adding a check digit to the numbers so they are all valid:
+
+>>> damm.calc_check_digit('572')
+'4'
+>>> damm.validate('5724')
+'5724'
+>>> damm.calc_check_digit('5724')
+'0'
+>>> damm.validate('57240')
+'57240'
+>>> damm.calc_check_digit('43881234567')
+'9'
+>>> damm.validate('438812345679')
+'438812345679'
+>>> damm.calc_check_digit('438812345679')
+'0'
+>>> damm.validate('4388123456790')
+'4388123456790'
+
+
+We can also use a different table if we really need to. This one is from
+http://www.md-software.de/math/DAMM_Quasigruppen.txt
+
+>>> table = (
+...     (0, 2, 3, 4, 5, 6, 7, 8, 9, 1),
+...     (2, 0, 4, 1, 7, 9, 5, 3, 8, 6),
+...     (3, 7, 0, 5, 2, 8, 1, 6, 4, 9),
+...     (4, 1, 8, 0, 6, 3, 9, 2, 7, 5),
+...     (5, 6, 2, 9, 0, 7, 4, 1, 3, 8),
+...     (6, 9, 7, 3, 1, 0, 8, 5, 2, 4),
+...     (7, 5, 1, 8, 4, 2, 0, 9, 6, 3),
+...     (8, 4, 6, 2, 9, 5, 3, 0, 1, 7),
+...     (9, 8, 5, 7, 3, 1, 6, 4, 0, 2),
+...     (1, 3, 9, 6, 8, 4, 2, 7, 5, 0))
+>>> damm.checksum('816', table=table)
+9
+>>> damm.checksum('8169', table=table)
+0

http://arthurdejong.org/git/python-stdnum/commit/?id=411d0381331334dd6b739f8d0527b56af741a90a

commit 411d0381331334dd6b739f8d0527b56af741a90a
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 18:03:02 2016 +0200

    Fix French NIF format test
    
    Fixes 2409ee9.

diff --git a/stdnum/fr/nif.py b/stdnum/fr/nif.py
index 7d5a802..4771562 100644
--- a/stdnum/fr/nif.py
+++ b/stdnum/fr/nif.py
@@ -28,7 +28,7 @@ issued by the French tax authorities to people for tax 
reporting purposes.
 More information:
 
 * https://ec.europa.eu/taxation_customs/tin/tinByCountry.html
-* https://fr.wikipedia.org/wiki/Num%C3%A9ro_d%27Immatriculation_Fiscale#France
+* https://fr.wikipedia.org/wiki/Numéro_d%27Immatriculation_Fiscale#France
 
 >>> validate('0701987765432')
 '0701987765432'
@@ -36,8 +36,8 @@ More information:
 Traceback (most recent call last):
     ...
 InvalidLength: ...
->>> format('295109912611193')
-'2 95 10 99 126 111 93'
+>>> format('0701987765432')
+'07 01 987 765 432'
 """
 
 from stdnum.exceptions import *

http://arthurdejong.org/git/python-stdnum/commit/?id=06e41651623798b0bffe37b141374e9ef56c8102

commit 06e41651623798b0bffe37b141374e9ef56c8102
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 18:00:26 2016 +0200

    Improve validation to Swiss SSN number
    
    The EAN-13 number should start with 756.

diff --git a/stdnum/ch/ssn.py b/stdnum/ch/ssn.py
index 78d72a2..57e88cf 100644
--- a/stdnum/ch/ssn.py
+++ b/stdnum/ch/ssn.py
@@ -1,4 +1,4 @@
-# vat.py - functions for handling Swiss social security numbers
+# ssn.py - functions for handling Swiss social security numbers
 #
 # Copyright (C) 2014 Denis Krienbuehl
 # Copyright (C) 2016 Arthur de Jong
@@ -25,10 +25,11 @@ to identify individuals for taxation and pension purposes.
 
 The number is validated using EAN-13, though dashes are substituted for dots.
 
->>> compact('756.9217.0769.85')
-'7569217076985'
->>> format('7569217076985')
-'756.9217.0769.85'
+More information:
+
+* https://en.wikipedia.org/wiki/National_identification_number#Switzerland
+* https://de.wikipedia.org/wiki/Sozialversicherungsnummer#Versichertennummer
+
 >>> validate('7569217076985')
 '7569217076985'
 >>> validate('756.9217.0769.85')
@@ -37,6 +38,12 @@ The number is validated using EAN-13, though dashes are 
substituted for dots.
 Traceback (most recent call last):
     ...
 InvalidChecksum: ...
+>>> validate('123.4567.8910.19')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> format('7569217076985')
+'756.9217.0769.85'
 """
 
 from stdnum import ean
@@ -62,6 +69,8 @@ def validate(number):
     number = compact(number)
     if len(number) != 13:
         raise InvalidLength()
+    if not number.startswith('756'):
+        raise InvalidComponent()
     return ean.validate(number)
 
 

http://arthurdejong.org/git/python-stdnum/commit/?id=1907c67e756ce5f38422ab75fa1de98fd8368ddc

commit 1907c67e756ce5f38422ab75fa1de98fd8368ddc
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 17:33:04 2016 +0200

    Add documentation to Brazillian CPF

diff --git a/stdnum/br/cpf.py b/stdnum/br/cpf.py
index 742d464..b8b4f6e 100644
--- a/stdnum/br/cpf.py
+++ b/stdnum/br/cpf.py
@@ -1,7 +1,7 @@
 # cpf.py - functions for handling CPF numbers
 # coding: utf-8
 #
-# Copyright (C) 2011, 2012, 2013 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
@@ -20,6 +20,14 @@
 
 """CPF (Cadastro de Pessoas Físicas, Brazillian national identifier).
 
+The Cadastro de Pessoas Físicas is the Brazilian identification number
+assigned to individuals for tax purposes. The number consists of 11 digits
+and includes two check digits.
+
+More information:
+
+* https://en.wikipedia.org/wiki/Cadastro_de_Pessoas_Físicas
+
 >>> validate('390.533.447-05')
 '39053344705'
 >>> validate('231.002.999-00')

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

commit cf428ac4c2f7f8d36042860e02e7d5b929b24b8f
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 17:19:58 2016 +0200

    Add pointer to ISO 6346 information

diff --git a/stdnum/iso6346.py b/stdnum/iso6346.py
index 5b936fc..fd6f1c9 100644
--- a/stdnum/iso6346.py
+++ b/stdnum/iso6346.py
@@ -28,6 +28,10 @@ digit), the owner, a country code, a size, type and 
equipment category as well
 as any operational marks. The standard is managed by the International
 Container Bureau (BIC).
 
+More information:
+
+* http://en.wikipedia.org/wiki/ISO_6346
+
 >>> validate('csqu3054383')
 'CSQU3054383'
 >>> validate('CSQU3054384')

http://arthurdejong.org/git/python-stdnum/commit/?id=011c0f0c4b1d2e08f2987de1cc9c2cfb16e39998

commit 011c0f0c4b1d2e08f2987de1cc9c2cfb16e39998
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 15:39:22 2016 +0200

    Fix copyright notice

diff --git a/stdnum/gb/nhs.py b/stdnum/gb/nhs.py
index 3773afc..c1367e3 100644
--- a/stdnum/gb/nhs.py
+++ b/stdnum/gb/nhs.py
@@ -1,6 +1,6 @@
 # nhs.py - functions for handling United Kingdom NHS numbers
 #
-# Copyright (C) 2012-2015 Arthur de Jong
+# 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

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

Summary of changes:
 getnumlist.py           |  5 +--
 stdnum/br/cpf.py        | 10 +++++-
 stdnum/ch/ssn.py        | 19 +++++++---
 stdnum/damm.py          | 93 +++++++++++++++++++++++++++++++++++++++++++++++++
 stdnum/fr/nif.py        |  6 ++--
 stdnum/gb/nhs.py        |  2 +-
 stdnum/iso6346.py       | 14 +++++++-
 tests/test_damm.doctest | 93 +++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 229 insertions(+), 13 deletions(-)
 create mode 100644 stdnum/damm.py
 create mode 100644 tests/test_damm.doctest


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