Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/isbn.py
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2011-09-20 23:14:48 +0200
committerArthur de Jong <arthur@arthurdejong.org>2011-09-20 23:14:48 +0200
commit4e8d7e4bcb0eeb49f6df38b530eb39c6cf953874 (patch)
treee71c3060b151c7822f4b2708bafdad493f763442 /stdnum/isbn.py
parentd6f9ba25c25e59099211016517c7a4a216ff13c6 (diff)
implement a conversion function from ISBN13 to ISBN10
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@80 9dea7c4f-944c-4273-ac1a-574ede026edc
Diffstat (limited to 'stdnum/isbn.py')
-rw-r--r--stdnum/isbn.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/stdnum/isbn.py b/stdnum/isbn.py
index 382b13d..59262ba 100644
--- a/stdnum/isbn.py
+++ b/stdnum/isbn.py
@@ -37,6 +37,8 @@ False
'ISBN13'
>>> to_isbn13('1-85798-218-5')
'978-1-85798-218-3'
+>>> to_isbn10('978-1-85798-218-3')
+'1-85798-218-5'
"""
from stdnum import ean
@@ -111,6 +113,28 @@ def to_isbn13(number):
return '978' + number
+def to_isbn10(number):
+ """Convert the number to ISBN-13 format."""
+ number = number.strip()
+ min_number = compact(number)
+ if len(min_number) == 10:
+ return number # nothing to do, already ISBN-13
+ elif isbn_type(min_number) != 'ISBN13':
+ raise ValueError('Not a valid ISBN13.')
+ elif not number.startswith('978'):
+ raise ValueError('Does not use 978 Bookland prefix.')
+ # strip EAN prefix
+ number = number[3:-1].strip().strip('-')
+ digit = _calc_isbn10_check_digit(min_number[3:-1])
+ # append the new check digit
+ if ' ' in number:
+ return number + ' ' + digit
+ elif '-' in number:
+ return number + '-' + digit
+ else:
+ return number + digit
+
+
def split(number, convert=False):
"""Split the specified ISBN into an EAN.UCC prefix, a group prefix, a
registrant, an item number and a check-digit. If the number is in ISBN-10