Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2011-06-20 22:53:35 +0200
committerArthur de Jong <arthur@arthurdejong.org>2011-06-20 22:53:35 +0200
commitf5747bc592f33765cbb5738f049885df6d690de3 (patch)
treea0aac27a66fa4e7660dcd5444bfa7175e829634d /stdnum
parent1b904ba0fb81afe38ba41facb9ed8dadebc7d5e2 (diff)
add an EAN (International Article Number) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@70 9dea7c4f-944c-4273-ac1a-574ede026edc
Diffstat (limited to 'stdnum')
-rw-r--r--stdnum/__init__.py1
-rw-r--r--stdnum/ean.py50
2 files changed, 51 insertions, 0 deletions
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]
+