lists.arthurdejong.org
RSS feed

python-stdnum commit: r23 - in python-stdnum: . stdnum tests

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

python-stdnum commit: r23 - in python-stdnum: . stdnum tests



Author: arthur
Date: Fri Aug 20 11:37:09 2010
New Revision: 23
URL: http://arthurdejong.org/viewvc/python-stdnum?view=rev&revision=23

Log:
add functions for handling the Verhoeff algorithm

Added:
   python-stdnum/stdnum/verhoeff.py
   python-stdnum/tests/test_verhoeff.doctest
Modified:
   python-stdnum/README

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Mon Aug 16 21:51:12 2010        (r22)
+++ python-stdnum/README        Fri Aug 20 11:37:09 2010        (r23)
@@ -12,6 +12,7 @@
 - ISBN (International Standard Book Number)
 - ISSN (International Standard Serial Number)
 - BSN (Burgerservicenummer, the Dutch national identification number)
+- Verhoeff (generic functions for the Verhoeff algorithm)
 
 Basically any number or code that has some validation mechanism available
 or some common formatting is eligible for inclusion into this library.

Added: python-stdnum/stdnum/verhoeff.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/verhoeff.py    Fri Aug 20 11:37:09 2010        (r23)
@@ -0,0 +1,83 @@
+# verhoeff.py - functions for performing the Verhoeff checksum
+#
+# Copyright (C) 2010 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
+
+"""Module for calculation and verifying the checksum of a number
+using the Verhoeff algorithm.
+
+Validation can be done with is_valid() which validates thet the
+calculated checksum is 0. A valid number can be made by calculating
+the check digit and appending it.
+
+>>> is_valid('1234')
+False
+>>> checksum('1234')
+1
+>>> calc_check_digit('1234')
+'0'
+>>> is_valid('12340')
+True
+"""
+
+# These are the multiplication and permutation tables used in the
+# Verhoeff algorithm.
+
+_multiplication_table = (
+    ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ),
+    ( 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 ),
+    ( 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 ),
+    ( 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 ),
+    ( 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 ),
+    ( 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 ),
+    ( 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 ),
+    ( 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 ),
+    ( 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 ),
+    ( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ) )
+
+_permutation_table = (
+    ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ),
+    ( 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 ),
+    ( 5, 8, 0, 3, 7, 9, 6, 1, 4, 2 ),
+    ( 8, 9, 1, 6, 0, 4, 3, 5, 2, 7 ),
+    ( 9, 4, 5, 3, 1, 2, 6, 8, 7, 0 ),
+    ( 4, 2, 8, 6, 5, 7, 3, 9, 0, 1 ),
+    ( 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 ),
+    ( 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 ) )
+
+def checksum(number):
+    """Calculate the Verhoeff checksum over the provided number. The checksum
+    is returned as an int. Valid numbers should have a checksum of 0."""
+    # transform number list
+    number = tuple( int(n) for n in reversed(str(number)) )
+    # calculate checksum
+    check = 0
+    for i in range(len(number)):
+        check = _multiplication_table[check][_permutation_table[i % 
8][number[i]]]
+    return check
+
+def is_valid(number):
+    """Checks to see if the number provided passes the Verhoeff checksum."""
+    try:
+        return bool(number) and checksum(number) == 0
+    except:
+        return False
+
+def calc_check_digit(number):
+    """With the provided number, calculate the extra digit that should be
+    appended to make it pass the Verhoeff checksum."""
+    return str(_multiplication_table[checksum(str(number) + '0')].index(0))

Added: python-stdnum/tests/test_verhoeff.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_verhoeff.doctest   Fri Aug 20 11:37:09 2010        
(r23)
@@ -0,0 +1,76 @@
+test_verhoeff.doctest - more detailed doctests for stdnum.verhoeff module
+
+Copyright (C) 2010 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.verhoeff module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import verhoeff
+
+
+These are normal variations that should just work. Calculating checksums:
+
+>>> verhoeff.checksum('654')
+1
+>>> verhoeff.checksum('1428570')
+0
+>>> verhoeff.checksum('398438246238642378648236487236482734')
+3
+
+
+The same numbers but now simply ask for validation:
+
+>>> verhoeff.is_valid('654')
+False
+>>> verhoeff.is_valid('1428570')
+True
+>>> verhoeff.is_valid('398438246238642378648236487236482734')
+False
+
+
+Adding a check digit to the numbers so they are all valid:
+
+>>> verhoeff.calc_check_digit('654')
+'8'
+>>> verhoeff.is_valid('6548')
+True
+>>> verhoeff.calc_check_digit('1428570')
+'8'
+>>> verhoeff.is_valid('1428570')
+True
+>>> verhoeff.calc_check_digit('398438246238642378648236487236482734')
+'7'
+>>> verhoeff.is_valid('3984382462386423786482364872364827347')
+True
+
+
+Furthermore the is_valid() method should be fairly robust against invalid
+junk passed:
+
+>>> verhoeff.is_valid(None)
+False
+>>> verhoeff.is_valid('')
+False
+>>> verhoeff.is_valid(0)
+False
+>>> verhoeff.is_valid(object())
+False
+>>> verhoeff.is_valid('65a4')
+False
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits