1
1
# __init__.py - functions for handling ISBNs
2
2
#
3
- # Copyright (C) 2010 Arthur de Jong
3
+ # Copyright (C) 2010, 2011 Arthur de Jong
4
4
#
5
5
# This library is free software; you can redistribute it and/or
6
6
# modify it under the terms of the GNU Lesser General Public
18
18
# 02110-1301 USA
19
19
20
20
"""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)
22
22
format.
23
23
24
24
>>> is_valid('978-9024538270')
40
40
"""
41
41
42
42
43
- def compact (number ):
43
+ def compact (number , convert = False ):
44
44
"""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."""
46
48
number = number .replace (' ' ,'' ).replace ('-' ,'' ).strip ().upper ()
47
49
if len (number ) == 9 :
48
50
number = '0' + number
51
+ if convert :
52
+ return to_isbn13 (numer )
49
53
return number
50
54
51
55
def _calc_isbn10_check_digit (number ):
@@ -89,11 +93,11 @@ def is_valid(number):
89
93
return isbn_type (number ) is not None
90
94
91
95
def to_isbn13 (number ):
92
- """Convert the number to ISBN13 format."""
96
+ """Convert the number to ISBN-13 format."""
93
97
number = number .strip ()
94
98
min_number = compact (number )
95
99
if len (min_number ) == 13 :
96
- return number # nothing to do, already ISBN13
100
+ return number # nothing to do, already ISBN-13
97
101
# put new check digit in place
98
102
number = number [:- 1 ] + _calc_isbn13_check_digit ('978' + min_number [:- 1 ])
99
103
# add prefix
@@ -104,13 +108,14 @@ def to_isbn13(number):
104
108
else :
105
109
return '978' + number
106
110
107
- def split (number ):
111
+ def split (number , convert = False ):
108
112
"""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."""
111
116
from stdnum import numdb
112
117
# clean up number
113
- number = compact (number )
118
+ number = compact (number , convert )
114
119
# get Bookland prefix if any
115
120
if len (number ) == 10 :
116
121
oprefix = ''
@@ -126,10 +131,11 @@ def split(number):
126
131
# return results
127
132
return ( oprefix , group , publisher , itemnr , number [- 1 ] )
128
133
129
- def format (number , separator = '-' ):
134
+ def format (number , separator = '-' , convert = False ):
130
135
"""Reformat the passed number to the standa
8000
rd format with the EAN.UCC
131
136
prefix (if any), the group prefix, the registrant, the item number and
132
137
the check-digit separated (if possible) by the specified separator.
133
138
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 )
0 commit comments