lists.arthurdejong.org
RSS feed

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

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

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



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

Log:
add functions for handling the Luhn and Luhn mod N algorithms

Added:
   python-stdnum/stdnum/luhn.py
   python-stdnum/tests/test_luhn.doctest
Modified:
   python-stdnum/README

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Fri Aug 20 13:56:45 2010        (r24)
+++ python-stdnum/README        Fri Aug 20 14:16:23 2010        (r25)
@@ -13,6 +13,7 @@
 - ISSN (International Standard Serial Number)
 - BSN (Burgerservicenummer, the Dutch national identification number)
 - Verhoeff (generic functions for the Verhoeff algorithm)
+- Luhn (generic functions for the Luhn and Luhn mod N algorithms)
 
 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/luhn.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/luhn.py        Fri Aug 20 14:16:23 2010        (r25)
@@ -0,0 +1,66 @@
+# luhn.py - functions for performing the Luhn and Luhn mod N algorithms
+#
+# 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 Luhn algorithm.
+
+Validation can be done with is_valid() which validates that the
+calculated checksum is 0. A valid number can be made by calculating
+the check digit and appending it.
+
+>>> is_valid('7894')
+False
+>>> checksum('7894')
+6
+>>> calc_check_digit('7894')
+'9'
+>>> is_valid('78949')
+True
+
+An alternative alphabet can be provided to use the Luhn mod N algorithm.
+The default alphabet is '0123456789'.
+
+>>> is_valid('1234', alphabet='0123456789abcdef')
+False
+>>> checksum('1234', alphabet='0123456789abcdef')
+14
+
+"""
+
+
+def checksum(number, alphabet='0123456789'):
+    """Calculate the Luhn checksum over the provided number. The checksum
+    is returned as an int. Valid numbers should have a checksum of 0."""
+    n = len(alphabet)
+    number = tuple( alphabet.index(i) for i in str(number) )
+    return ( sum(number[::-2]) +
+             sum( sum(divmod(i * 2, n)) for i in number[-2::-2] ) ) % n
+
+def is_valid(number, alphabet='0123456789'):
+    """Checks to see if the number provided passes the Luhn checksum."""
+    try:
+        return bool(number) and checksum(number, alphabet) == 0
+    except:
+        return False
+
+def calc_check_digit(number, alphabet='0123456789'):
+    """With the provided number, calculate the extra digit that should be
+    appended to make it pass the Luhn checksum."""
+    ck = checksum(str(number) + alphabet[0], alphabet)
+    return alphabet[-ck]

Added: python-stdnum/tests/test_luhn.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_luhn.doctest       Fri Aug 20 14:16:23 2010        
(r25)
@@ -0,0 +1,75 @@
+test_luhn.doctest - more detailed doctests for stdnum.luhn 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.luhn module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum import luhn
+
+
+These are normal variations that should just work. Calculating checksums:
+
+>>> luhn.checksum('4992739871')
+9
+>>> luhn.checksum('490154203237518')
+0
+>>> luhn.checksum('abcdefe', alphabet='abcdef')
+0
+
+
+The same numbers but now simply ask for validation:
+
+>>> luhn.is_valid('4992739871')
+False
+>>> luhn.is_valid('490154203237518')
+True
+>>> luhn.is_valid('abcdefe', alphabet='abcdef')
+True
+
+Adding a check digit to the numbers so they are all valid:
+
+>>> luhn.calc_check_digit('4992739871')
+'6'
+>>> luhn.is_valid('49927398716')
+True
+>>> luhn.calc_check_digit('142857')
+'2'
+>>> luhn.is_valid('1428572')
+True
+>>> luhn.calc_check_digit('398438246238642378648236487236482734')
+'7'
+>>> luhn.is_valid('3984382462386423786482364872364827347')
+True
+
+
+Furthermore the is_valid() method should be fairly robust against invalid
+junk passed:
+
+>>> luhn.is_valid(None)
+False
+>>> luhn.is_valid('')
+False
+>>> luhn.is_valid(0)
+False
+>>> luhn.is_valid(object())
+False
+>>> luhn.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