8000 add a convert parameter to most isbn functions to automatically conve… · sharoonthomas/python-stdnum@6724e50 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6724e50

Browse files
committed
add a convert parameter to most isbn functions to automatically convert to ISBN-13
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@53 9dea7c4f-944c-4273-ac1a-574ede026edc
1 parent 411874e commit 6724e50

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

stdnum/isbn.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# __init__.py - functions for handling ISBNs
22
#
3-
# Copyright (C) 2010 Arthur de Jong
3+
# Copyright (C) 2010, 2011 Arthur de Jong
44
#
55
# This library is free software; you can redistribute it and/or
66
# modify it under the terms of the GNU Lesser General Public
@@ -18,7 +18,7 @@
1818
# 02110-1301 USA
1919

2020
"""Module for handling ISBNs (International Standard Book Number). This
21-
module handles both numbers in ISBN10 (10-digit) and ISBN13 (13-digit)
21+
module handles both numbers in ISBN-10 (10-digit) and ISBN-13 (13-digit)
2222
format.
2323
2424
>>> is_valid('978-9024538270')
@@ -40,12 +40,16 @@
4040
"""
4141

4242

43-
def compact(number):
43+
def compact(number, convert=False):
4444
"""Convert the ISBN to the minimal representation. This strips the number
45-
of any valid ISBN separators and removes surrounding whitespace."""
45+
of any valid ISBN separators and removes surrounding whitespace. If the
46+
covert parameter is True the number is also converted to ISBN-13
47+
format."""
4648
number = number.replace(' ','').replace('-','').strip().upper()
4749
if len(number) == 9:
4850
number = '0' + number
51+
if convert:
52+
return to_isbn13(numer)
4953
return number
5054

5155
def _calc_isbn10_check_digit(number):
@@ -89,11 +93,11 @@ def is_valid(number):
8993
return isbn_type(number) is not None
9094

9195
def to_isbn13(number):
92-
"""Convert the number to ISBN13 format."""
96+
"""Convert the number to ISBN-13 format."""
9397
number = number.strip()
9498
min_number = compact(number)
9599
if len(min_number) == 13:
96-
return number # nothing to do, already ISBN13
100+
return number # nothing to do, already ISBN-13
97101
# put new check digit in place
98102
number = number[:-1] + _calc_isbn13_check_digit('978' + min_number[:-1])
99103
# add prefix
@@ -104,13 +108,14 @@ def to_isbn13(number):
104108
else:
105109
return '978' + number
106110

107-
def split(number):
111+
def split(number, convert=False):
108112
"""Split the specified ISBN into an EAN.UCC prefix, a group prefix, a
109-
registrant, an item number and a check-digit. If the number is in ISBN10
110-
format the returned EAN.UCC prefix is '978'."""
113+
registrant, an item number and a check-digit. If the number is in ISBN-10
114+
format the returned EAN.UCC prefix is '978'. If the covert parameter is
115+
True the number is converted to ISBN-13 format first."""
111116
from stdnum import numdb
112117
# clean up number
113-
number = compact(number)
118+
number = compact(number, convert)
114119
# get Bookland prefix if any
115120
if len(number) == 10:
116121
oprefix = ''
@@ -126,10 +131,11 @@ def split(number):
126131
# return results
127132
return ( oprefix, group, publisher, itemnr, number[-1] )
128133

129-
def format(number, separator='-'):
134+
def format(number, separator='-', convert=False):
130135
"""Reformat the passed number to the standa 8000 rd format with the EAN.UCC
131136
prefix (if any), the group prefix, the registrant, the item number and
132137
the check-digit separated (if possible) by the specified separator.
133138
Passing an empty separator should equal compact() though this is less
134-
efficient."""
135-
return separator.join(x for x in split(number) if x)
139+
efficient. If the covert parameter is True the number is converted to
140+
ISBN-13 format first."""
141+
return separator.join(x for x in split(number, convert) if x)

tests/test_isbn.doctest

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_isbn.doctest - more detailed doctests for stdnum.isbn module
22

3-
Copyright (C) 2010 Arthur de Jong
3+
Copyright (C) 2010, 2011 Arthur de Jong
44

55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
@@ -67,6 +67,8 @@ See if ISBN10 to 13 conversion works.
6767
'978-1-85798-218-3'
6868
>>> isbn.is_valid(isbn.to_isbn13('1 85798218 5'))
6969
True
70+
>>> isbn.compact('1 85798218 5', convert=True)
71+
'9781857982183'
7072

7173

7274
Regrouping tests.

0 commit comments

Comments
 (0)
0