diff options
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | stdnum/__init__.py | 1 | ||||
-rw-r--r-- | stdnum/ean.py | 50 | ||||
-rw-r--r-- | tests/test_robustness.doctest | 2 |
4 files changed, 53 insertions, 1 deletions
@@ -13,6 +13,7 @@ Currently this package supports the following formats: * 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) diff --git a/stdnum/__init__.py b/stdnum/__init__.py index eddc062..f62ba8d 100644 --- a/stdnum/__init__.py +++ b/stdnum/__init__.py @@ -27,6 +27,7 @@ Currently this module supports the following formats: * 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) diff --git a/stdnum/ean.py b/stdnum/ean.py new file mode 100644 index 0000000..0f1d6fe --- /dev/null +++ b/stdnum/ean.py @@ -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] + diff --git a/tests/test_robustness.doctest b/tests/test_robustness.doctest index cbb7b56..b7617a1 100644 --- a/tests/test_robustness.doctest +++ b/tests/test_robustness.doctest @@ -24,7 +24,7 @@ invalid junk. >>> 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 |