Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/isbn.py
diff options
context:
space:
mode:
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