lists.arthurdejong.org
RSS feed

python-stdnum commit: r27 - python-stdnum/stdnum

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

python-stdnum commit: r27 - python-stdnum/stdnum



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

Log:
make checksum calculation a little more readable

Modified:
   python-stdnum/stdnum/luhn.py

Modified: python-stdnum/stdnum/luhn.py
==============================================================================
--- python-stdnum/stdnum/luhn.py        Fri Aug 20 16:16:55 2010        (r26)
+++ python-stdnum/stdnum/luhn.py        Fri Aug 20 16:44:33 2010        (r27)
@@ -48,9 +48,9 @@
     """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
+    number = tuple( alphabet.index(i) for i in reversed(str(number)) )
+    return ( sum(number[::2]) +
+             sum( sum(divmod(i * 2, n)) for i in number[1::2] ) ) % n
 
 def is_valid(number, alphabet='0123456789'):
     """Checks to see if the number provided passes the Luhn checksum."""
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits