lists.arthurdejong.org
RSS feed

python-stdnum commit: r80 - in python-stdnum: stdnum tests

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum commit: r80 - in python-stdnum: stdnum tests



Author: arthur
Date: Tue Sep 20 23:14:48 2011
New Revision: 80
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=80&view=revision

Log:
implement a conversion function from ISBN13 to ISBN10

Modified:
   python-stdnum/stdnum/isbn.py
   python-stdnum/tests/test_isbn.doctest

Modified: python-stdnum/stdnum/isbn.py
==============================================================================
--- python-stdnum/stdnum/isbn.py        Tue Sep 20 22:53:36 2011        (r79)
+++ python-stdnum/stdnum/isbn.py        Tue Sep 20 23:14:48 2011        (r80)
@@ -37,6 +37,8 @@
 '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 @@
         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

Modified: python-stdnum/tests/test_isbn.doctest
==============================================================================
--- python-stdnum/tests/test_isbn.doctest       Tue Sep 20 22:53:36 2011        
(r79)
+++ python-stdnum/tests/test_isbn.doctest       Tue Sep 20 23:14:48 2011        
(r80)
@@ -51,6 +51,26 @@
 '9781857982183'
 
 
+See if ISBN13 to 10 conversion works.
+
+>>> isbn.to_isbn10('1-85798-218-5')  # ISBN10 should stay ISBN10
+'1-85798-218-5'
+>>> isbn.to_isbn10('978 1 85798218 3')
+'1 85798218 5'
+>>> isbn.to_isbn10('9781857982183')
+'1857982185'
+>>> isbn.to_isbn10('978-1-85798-218-3')
+'1-85798-218-5'
+>>> isbn.to_isbn10('979-20-1234567-8')  # incorrect check digit
+Traceback (most recent call last):
+    ...
+ValueError: Not a valid ISBN13.
+>>> isbn.to_isbn10('9791843123391')
+Traceback (most recent call last):
+    ...
+ValueError: Does not use 978 Bookland prefix.
+
+
 Regrouping tests.
 
 >>> isbn.split('9024538270')  # normal ISBN10
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/