lists.arthurdejong.org
RSS feed

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

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

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



Author: arthur
Date: Mon Jun 20 22:53:35 2011
New Revision: 70
URL: http://arthurdejong.org/viewvc/python-stdnum?view=rev&revision=70

Log:
add an EAN (International Article Number) module

Added:
   python-stdnum/stdnum/ean.py
Modified:
   python-stdnum/README
   python-stdnum/stdnum/__init__.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Sun Mar  6 22:56:20 2011        (r69)
+++ python-stdnum/README        Mon Jun 20 22:53:35 2011        (r70)
@@ -13,6 +13,7 @@
  * ISSN (International Standard Serial Number)
  * ISMN (International Standard Music Number)
  * ISAN (International Standard Audiovisual Number)
+ * EAN (International Article Number)
  * BSN (Burgerservicenummer, the Dutch national identification number)
  * CPF (Cadastro de Pessoas Físicas, the Brazillian national identification
    number)

Modified: python-stdnum/stdnum/__init__.py
==============================================================================
--- python-stdnum/stdnum/__init__.py    Sun Mar  6 22:56:20 2011        (r69)
+++ python-stdnum/stdnum/__init__.py    Mon Jun 20 22:53:35 2011        (r70)
@@ -27,6 +27,7 @@
  * ISSN (International Standard Serial Number)
  * ISMN (International Standard Music Number)
  * ISAN (International Standard Audiovisual Number)
+ * EAN (International Article Number)
  * BSN (Burgerservicenummer, the Dutch national identification number)
  * CPF (Cadastro de Pessoas Físicas, the Brazillian national identification
    number)

Added: python-stdnum/stdnum/ean.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/ean.py Mon Jun 20 22:53:35 2011        (r70)
@@ -0,0 +1,50 @@
+# ean.py - functions for handling EANs
+#
+# Copyright (C) 2011 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 handling EAN (International Article Number) codes. This
+module handles numbers EAN-13, EAN-8 and UPC (12-digit) format.
+
+>>> is_valid('73513537')
+True
+>>> is_valid('978-0-471-11709-4') # ISBN-13 format
+True
+"""
+
+def compact(number):
+    """Convert the EAN to the minimal representation. This strips the number
+    of any valid separators and removes surrounding whitespace."""
+    return number.replace(' ','').replace('-','').strip()
+
+def calc_check_digit(number):
+    """Calculate the EAN check digit for 13-digit numbers. The number passed
+    should not have the check bit included."""
+    return str((10 - sum( (3 - 2 * (i % 2)) * int(n) for i, n in 
enumerate(reversed(number)))) % 10)
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid EAN-13. This checks
+    the length and the check bit but does not check whether a known GS1
+    Prefix and company identifier are referenced."""
+    try:
+        number = compact(number)
+    except:
+        return False
+    return len(number) in (13, 12, 8) and \
+           number.isdigit() and \
+           calc_check_digit(number[:-1]) == number[-1]
+

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Sun Mar  6 22:56:20 2011        
(r69)
+++ python-stdnum/tests/test_robustness.doctest Mon Jun 20 22:53:35 2011        
(r70)
@@ -24,7 +24,7 @@
 
 >>> testvalues = ( None, '*&^%$', '', 0, False, object(),  )
 
->>> from stdnum import grid, iban, imei, isan, isbn, isil, ismn, issn
+>>> from stdnum import grid, iban, imei, isan, isbn, isil, ismn, issn, ean
 >>> from stdnum import luhn, meid, verhoeff
 >>> from stdnum.iso7064 import mod_11_10, mod_11_2, mod_37_2, mod_37_36, 
 >>> mod_97_10
 >>> from stdnum.nl import bsn
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits