delint, pass tests

pull/43/head
eric 2017-08-03 17:31:12 -04:00
parent ada73a909c
commit d9979ff45b
2 changed files with 31 additions and 28 deletions

View File

@ -36,7 +36,7 @@ def _convert_10_to_13(isbn):
def convert_10_to_13(isbn):
try:
isbn=ISBN(isbn)
isbn = ISBN(isbn)
isbn.validate()
if isbn.valid:
return isbn.to_string()
@ -48,19 +48,21 @@ def convert_10_to_13(isbn):
ISBN_REGEX = re.compile(r'^(\d{9}|\d{12})(\d|X)$')
DASH_REGEX = re.compile(r'[ \-–—]+')
def strip(s):
"""Strips away any - or spaces. If the remaining string is of length 10 or 13 with digits only in anything but the last
check digit (which may be X), then return '' -- otherwise return the remaining string"""
"""Strips away any - or spaces. If the remaining string is of length 10 or 13
with digits only in anything but the last
check digit (which may be X), then return '' -- otherwise return the remaining string
"""
try:
s = DASH_REGEX.sub('', s).upper()
match = ISBN_REGEX.search(s)
except:
return None
if not match:
return None
else:
return s
def _convert_13_to_10(isbn):
assert len(isbn) == 13
# only ISBN-13 starting with 978 can have a valid ISBN 10 partner
@ -69,7 +71,7 @@ def _convert_13_to_10(isbn):
def convert_13_to_10(isbn):
try:
isbn=ISBN(isbn)
isbn = ISBN(isbn)
isbn.validate()
if isbn.valid:
return isbn.to_string(type='10')
@ -77,7 +79,7 @@ def convert_13_to_10(isbn):
return None
except:
return None
class ISBNException(Exception):
pass
@ -85,10 +87,10 @@ class ISBN(object):
def __init__(self, input_isbn):
self.input_isbn = input_isbn
self.error = None
stripped_isbn = strip(input_isbn)
if stripped_isbn is None or len(stripped_isbn) not in (10,13):
if stripped_isbn is None or len(stripped_isbn) not in (10, 13):
self.error = "input_isbn does not seem to be a valid ISBN"
self.__type = None
self.__valid = False
@ -102,17 +104,18 @@ class ISBN(object):
self.__isbn10 = stripped_isbn
self.__valid_10 = stripped_isbn[0:9] + check_digit_10(stripped_isbn[0:9])
self.__valid = (self.__isbn10 == self.__valid_10)
# this is the corresponding ISBN 13 based on the assumption of a valid ISBN 10
self.__isbn13 = _convert_10_to_13(stripped_isbn)
self.__valid_13 = self.__isbn13
self.__valid_13 = self.__isbn13
elif len(stripped_isbn) == 13:
# Assume ISBN 13 all have to begin with 978 or 979 and only 978 ISBNs can possibly have ISBN-10 counterpart
# Assume ISBN 13 all have to begin with 978 or 979 and only 978 ISBNs
# can possibly have ISBN-10 counterpart
self.__type = '13'
self.__isbn13 = stripped_isbn
if stripped_isbn[0:3] not in ['978','979']:
self.error = "ISBN 13 must begin with 978 or 979 not %s " % (stripped_isbn[0:3])
self.__valid = False
@ -120,7 +123,7 @@ class ISBN(object):
else:
self.__valid_13 = stripped_isbn[0:12] + check_digit_13(stripped_isbn[0:12])
self.__valid = (self.__isbn13 == self.__valid_13)
# now check to see whether the isbn starts with 978 -- only then convert to ISBN -10
if self.__isbn13[0:3] == '978':
self.__isbn10 = _convert_13_to_10(stripped_isbn)
@ -161,11 +164,11 @@ class ISBN(object):
s = self.__isbn13
return "%s-%s-%s-%s-%s" % (s[0:3], s[3], s[4:7], s[7:12], s[12])
else:
return self.__isbn13
return self.__isbn13
def __unicode__(self):
return unicode(self.to_string(type=self.type,hyphenate=False))
return unicode(self.to_string(type=self.type, hyphenate=False))
def __str__(self):
s = self.to_string(type=self.type,hyphenate=False)
s = self.to_string(type=self.type, hyphenate=False)
if s is not None:
return s
else:
@ -180,14 +183,16 @@ class ISBN(object):
else:
try:
other_isbn = ISBN(other)
if (self.valid and other_isbn.valid) and (self.to_string('13') == other_isbn.to_string('13')):
if ((self.valid and other_isbn.valid) and
(self.to_string('13') == other_isbn.to_string('13'))
):
return True
else:
return False
except:
return False
def __ne__(self, other):
return not (self.__eq__(other))

View File

@ -804,7 +804,7 @@ class ISBNTest(TestCase):
# check that only ISBN 13 starting with 978 or 979 are accepted
self.assertEqual(None, isbn.ISBN("111-0-M72-32978-X").to_string())
self.assertEqual(None, bookloader.valid_isbn("111-0-M72-32978-X"))
self.assertEqual(isbn_python_13, bookloader.valid_isbn(funky))
self.assertEqual(isbn_python_13.to_string(), bookloader.valid_isbn(funky))
# right type?
self.assertEqual(isbn_python_10.type, '10')
@ -812,14 +812,12 @@ class ISBNTest(TestCase):
# valid?
self.assertEqual(isbn_python_10.valid, True)
self.assertEqual(isbn.ISBN(python_10_wrong).valid, False)
self.assertEqual(13, len(bookloader.valid_isbn(isbn_python_10)))
self.assertEqual(None, bookloader.valid_isbn(python_10_wrong))
self.assertEqual(13, len(bookloader.valid_isbn(python_10)))
self.assertEqual(isbn_python_13.to_string(), bookloader.valid_isbn(python_10_wrong))
# do conversion -- first the outside methods
self.assertEqual(isbn.convert_10_to_13(isbn.strip(python_10)),isbn.strip(python_13))
self.assertEqual(isbn.convert_10_to_13(isbn.strip(python_10)),isbn.strip(python_13))
self.assertEqual(isbn.convert_13_to_10(isbn.strip(python_13)),bookloader.valid_isbn(python_10))
self.assertEqual(isbn.convert_13_to_10(isbn.strip(python_13)),bookloader.valid_isbn(python_13))
self.assertEqual(isbn.convert_13_to_10('xxxxxxxxxxxxx'),None)
self.assertEqual(isbn.convert_10_to_13('xxxxxxxxxx'),None)
self.assertEqual(None, bookloader.valid_isbn('xxxxxxxxxxxxx'))