lists.arthurdejong.org
RSS feed

python-stdnum commit: r28 - in python-stdnum: stdnum tests

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

python-stdnum commit: r28 - in python-stdnum: stdnum tests



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

Log:
add an add_check_digit option to the format() function to add a check digit if 
needed and possible

Modified:
   python-stdnum/stdnum/imei.py
   python-stdnum/tests/test_imei.doctest

Modified: python-stdnum/stdnum/imei.py
==============================================================================
--- python-stdnum/stdnum/imei.py        Fri Aug 20 16:44:33 2010        (r27)
+++ python-stdnum/stdnum/imei.py        Fri Aug 20 18:25:33 2010        (r28)
@@ -29,6 +29,8 @@
 '3568680000414120'
 >>> format('354178036859789')
 '35-417803-685978-9'
+>>> format('35686800-004141', add_check_digit=True)
+'35-686800-004141-8'
 >>> imei_type('35686800-004141-20')
 'IMEISV'
 """
@@ -62,8 +64,11 @@
     number."""
     return imei_type(number) is not None
 
-def format(number, separator='-'):
+def format(number, separator='-', add_check_digit=False):
     """Reformat the passed number to the standard format."""
     number = compact(number)
+    if len(number) == 14 and add_check_digit:
+        from stdnum import luhn
+        number += luhn.calc_check_digit(number)
     number = ( number[:2], number[2:8], number[8:14], number[14:] )
     return separator.join(x for x in number if x)

Modified: python-stdnum/tests/test_imei.doctest
==============================================================================
--- python-stdnum/tests/test_imei.doctest       Fri Aug 20 16:44:33 2010        
(r27)
+++ python-stdnum/tests/test_imei.doctest       Fri Aug 20 18:25:33 2010        
(r28)
@@ -63,3 +63,21 @@
 False
 >>> imei.is_valid('3abc6800-0041X1-20')
 False
+
+
+The format() function can add the check digit if needed. It should leave
+alone existing check digits (even invalid ones) and only add them to the
+14 digit format.
+
+>>> imei.format('354178036859789')
+'35-417803-685978-9'
+>>> imei.format('35417803685978')
+'35-417803-685978'
+>>> imei.format('354178036859786', add_check_digit=True)
+'35-417803-685978-6'
+>>> imei.format('35417803685978', add_check_digit=True)
+'35-417803-685978-9'
+>>> imei.format('35686800-004141', add_check_digit=True)
+'35-686800-004141-8'
+>>> imei.format('35686800-004141-20', add_check_digit=True)
+'35-686800-004141-20'
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits