Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2011-06-20 22:55:58 +0200
committerArthur de Jong <arthur@arthurdejong.org>2011-06-20 22:55:58 +0200
commitd101acfb563b495c0a38037c683980b91fe43259 (patch)
tree31829365ea25cf0632c62d566e2f9171d40586b6
parentf5747bc592f33765cbb5738f049885df6d690de3 (diff)
use the ean module for calculating the check digit
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@71 9dea7c4f-944c-4273-ac1a-574ede026edc
-rw-r--r--stdnum/isbn.py13
-rw-r--r--stdnum/ismn.py15
2 files changed, 11 insertions, 17 deletions
diff --git a/stdnum/isbn.py b/stdnum/isbn.py
index 7c15541..05f311c 100644
--- a/stdnum/isbn.py
+++ b/stdnum/isbn.py
@@ -1,4 +1,4 @@
-# __init__.py - functions for handling ISBNs
+# isbn.py - functions for handling ISBNs
#
# Copyright (C) 2010, 2011 Arthur de Jong
#
@@ -39,6 +39,8 @@ False
'978-1-85798-218-3'
"""
+from stdnum import ean
+
def compact(number, convert=False):
"""Convert the ISBN to the minimal representation. This strips the number
@@ -58,11 +60,6 @@ def _calc_isbn10_check_digit(number):
check = sum( (i + 1) * int(n) for i, n in enumerate(number) ) % 11
return 'X' if check == 10 else str(check)
-def _calc_isbn13_check_digit(number):
- """Calculate the ISBN check digit for 13-digit numbers. The number passed
- should not have the check bit included."""
- return str((10 - sum( (2 * (i % 2) + 1) * int(n) for i, n in enumerate(number))) % 10)
-
def isbn_type(number):
"""Check the passed number and returns 'ISBN13', 'ISBN10' or None (for
invalid) for checking the type of number passed."""
@@ -79,7 +76,7 @@ def isbn_type(number):
elif len(number) == 13:
if not number.isdigit():
return None
- if _calc_isbn13_check_digit(number[:-1]) != number[-1]:
+ if ean.calc_check_digit(number[:-1]) != number[-1]:
return None
return 'ISBN13'
else:
@@ -99,7 +96,7 @@ def to_isbn13(number):
if len(min_number) == 13:
return number # nothing to do, already ISBN-13
# put new check digit in place
- number = number[:-1] + _calc_isbn13_check_digit('978' + min_number[:-1])
+ number = number[:-1] + ean.calc_check_digit('978' + min_number[:-1])
# add prefix
if ' ' in number:
return '978 ' + number
diff --git a/stdnum/ismn.py b/stdnum/ismn.py
index 4dc6afc..38dc059 100644
--- a/stdnum/ismn.py
+++ b/stdnum/ismn.py
@@ -1,6 +1,6 @@
-# __init__.py - functions for handling ISMNs
+# ismn.py - functions for handling ISMNs
#
-# Copyright (C) 2010 Arthur de Jong
+# Copyright (C) 2010, 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
@@ -38,6 +38,8 @@ False
'9790230671187'
"""
+from stdnum import ean
+
def compact(number):
"""Convert the ISMN to the minimal representation. This strips the number
@@ -45,11 +47,6 @@ def compact(number):
number = number.replace(' ','').replace('-','').replace('.','').strip().upper()
return number
-def _calc_check_digit(number):
- """Calculate the ISMN check digit. The number passed should not have
- the check bit included and should be in the 13-digit form."""
- return str((10 - sum( (2 * (i % 2) + 1) * int(n) for i, n in enumerate(number))) % 10)
-
def is_valid(number):
"""Checks to see if the number provided is a valid ISMN (either a legacy
10-digit one or a 13-digit one). This checks the length and the check
@@ -59,10 +56,10 @@ def is_valid(number):
except:
return False
if len(number) == 10 and number[0] == 'M' and number[1:].isdigit():
- if _calc_check_digit('9790' + number[1:-1]) == number[-1]:
+ if ean.calc_check_digit('9790' + number[1:-1]) == number[-1]:
return True
elif len(number) == 13 and number.isdigit():
- if _calc_check_digit(number[:-1]) == number[-1]:
+ if ean.calc_check_digit(number[:-1]) == number[-1]:
return True
return False