From f5747bc592f33765cbb5738f049885df6d690de3 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Mon, 20 Jun 2011 20:53:35 +0000 Subject: add an EAN (International Article Number) module git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@70 9dea7c4f-944c-4273-ac1a-574ede026edc --- stdnum/__init__.py | 1 + stdnum/ean.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 stdnum/ean.py (limited to 'stdnum') 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] + -- cgit v1.2.3