lists.arthurdejong.org
RSS feed

python-stdnum commit: r126 - in python-stdnum/stdnum: . es it

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

python-stdnum commit: r126 - in python-stdnum/stdnum: . es it



Author: arthur
Date: Sun Feb 12 23:09:29 2012
New Revision: 126
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=126&view=revision

Log:
use the luhn module where possible

Modified:
   python-stdnum/stdnum/es/cif.py
   python-stdnum/stdnum/it/iva.py
   python-stdnum/stdnum/util.py

Modified: python-stdnum/stdnum/es/cif.py
==============================================================================
--- python-stdnum/stdnum/es/cif.py      Sun Feb 12 16:30:02 2012        (r125)
+++ python-stdnum/stdnum/es/cif.py      Sun Feb 12 23:09:29 2012        (r126)
@@ -37,8 +37,8 @@
 ('A', '13', '58562', '5')
 """
 
+from stdnum import luhn
 from stdnum.es import dni
-from stdnum.util import digitsum
 
 
 __all__ = ['compact', 'is_valid', 'split']
@@ -52,11 +52,8 @@
     """Calculate the check digits for the specified number. The number
     passed should not have the check digit included. This function returns
     both the number and character check digit candidates."""
-    check = (10 - digitsum(
-                (2 - (i % 2)) * int(n)
-                for i, n in enumerate(number[1:])
-            )) % 10
-    return str(check) + 'JABCDEFGHI'[check]
+    check = luhn.calc_check_digit(number[1:])
+    return check + 'JABCDEFGHI'[int(check)]
 
 
 def is_valid(number):

Modified: python-stdnum/stdnum/it/iva.py
==============================================================================
--- python-stdnum/stdnum/it/iva.py      Sun Feb 12 16:30:02 2012        (r125)
+++ python-stdnum/stdnum/it/iva.py      Sun Feb 12 23:09:29 2012        (r126)
@@ -35,7 +35,8 @@
 False
 """
 
-from stdnum.util import clean, digitsum
+from stdnum import luhn
+from stdnum.util import clean
 
 
 def compact(number):
@@ -47,13 +48,6 @@
     return number
 
 
-def checksum(number):
-    """Calculate the checksum."""
-    return digitsum(
-            (1 + (i % 2)) * int(n)
-            for i, n in enumerate(number)) % 10
-
-
 def is_valid(number):
     """Checks to see if the number provided is a valid VAT number. This checks
     the length, formatting and check digit."""
@@ -65,4 +59,4 @@
            int(number[0:7]) > 0 and (
               '001' <= number[7:10] <= '100' or
               number[7:10] in ('120', '121', '888', '999')
-           ) and checksum(number) == 0
+           ) and luhn.is_valid(number)

Modified: python-stdnum/stdnum/util.py
==============================================================================
--- python-stdnum/stdnum/util.py        Sun Feb 12 16:30:02 2012        (r125)
+++ python-stdnum/stdnum/util.py        Sun Feb 12 23:09:29 2012        (r126)
@@ -27,13 +27,3 @@
     '123456789'
     """
     return ''.join(x for x in number if x not in deletechars)
-
-
-def digitsum(numbers):
-    """Returns the sum of the individual digits of the provided numbers.
-
-    >>> digitsum([12, 55])
-    13
-    """
-    # note: this only works for two-digit numbers
-    return sum((x // 10) + (x % 10) for x in numbers)
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/