Skip to content

Commit

Permalink
type consistent returns
Browse files Browse the repository at this point in the history
  • Loading branch information
xlcnd committed Jun 5, 2020
1 parent 264868a commit 9993802
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
37 changes: 15 additions & 22 deletions isbnlib/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@
LEGAL = '0123456789xXisbnISBN- '


# TODO(make return type consistent)!
# pylint: disable=broad-except
def check_digit10(firstninedigits):
"""Check sum ISBN-10."""
# minimum checks
if len(firstninedigits) != 9:
return None
return ''
try:
int(firstninedigits)
except Exception: # pragma: no cover
return None
return ''
# checksum
val = sum(
(i + 2) * int(x) for i, x in enumerate(reversed(firstninedigits)))
Expand All @@ -70,17 +69,16 @@ def check_digit10(firstninedigits):
return str(tenthdigit)


# TODO(make return type consistent)!
# pylint: disable=broad-except
def check_digit13(firsttwelvedigits):
"""Check sum ISBN-13."""
# minimum checks
if len(firsttwelvedigits) != 12:
return None
return ''
try:
int(firsttwelvedigits)
except Exception: # pragma: no cover
return None
return ''
# checksum
val = sum(
(i % 2 * 2 + 1) * int(x) for i, x in enumerate(firsttwelvedigits))
Expand All @@ -90,19 +88,16 @@ def check_digit13(firsttwelvedigits):
return str(thirteenthdigit)


# pylint: disable=simplifiable-if-expression
def _check_structure10(isbn10like):
"""Check structure of an ISBN-10."""
return bool(re.match(RE_ISBN10, isbn10like))


# pylint: disable=simplifiable-if-expression
def _check_structure13(isbn13like):
"""Check structure of an ISBN-13."""
return bool(re.match(RE_ISBN13, isbn13like))


# pylint: disable=simplifiable-if-expression
def is_isbn10(isbn10):
"""Validate as ISBN-10."""
isbn10 = canonical(isbn10)
Expand All @@ -123,32 +118,30 @@ def is_isbn13(isbn13):
return bool(not check_digit13(isbn13[:-1]) != isbn13[-1])


# TODO(make return type consistent)!
def to_isbn10(isbn13):
"""Transform isbn-13 to isbn-10."""
isbn13 = canonical(isbn13)
# Check prefix
if isbn13[:3] != ISBN13_PREFIX:
return isbn13 if len(isbn13) == 10 and is_isbn10(isbn13) else None
return isbn13 if len(isbn13) == 10 and is_isbn10(isbn13) else ''
if not is_isbn13(isbn13):
return None
return ''
isbn10 = isbn13[3:]
check = check_digit10(isbn10[:-1])
# Change check digit
return isbn10[:-1] + check if check else None
return isbn10[:-1] + check if check else ''


# TODO(make return type consistent)!
def to_isbn13(isbn10):
"""Transform isbn-10 to isbn-13."""
isbn10 = canonical(isbn10)
if len(isbn10) == 13 and is_isbn13(isbn10):
return isbn10
if not is_isbn10(isbn10):
return None
return ''
isbn13 = ISBN13_PREFIX + isbn10[:-1]
check = check_digit13(isbn13)
return isbn13 + check if check else None
return isbn13 + check if check else ''


def canonical(isbnlike):
Expand Down Expand Up @@ -212,7 +205,7 @@ def get_isbnlike(text, level='normal'):
isbnlike = RE_LOOSE
else:
LOGGER.error('level as no option %s', level)
return None
return []
return isbnlike.findall(text)


Expand All @@ -227,7 +220,7 @@ def get_canonical_isbn(isbnlike, output='bouth'):
"""
if output not in ('bouth', 'isbn10', 'isbn13'): # pragma: no cover
LOGGER.error('output as no option %s', output)
return None
return ''

regex = RE_NORMAL

Expand Down Expand Up @@ -255,18 +248,18 @@ def get_canonical_isbn(isbnlike, output='bouth'):
if output == 'isbn10':
return cisbn if len(cisbn) == 10 else to_isbn10(cisbn)
return to_isbn13(cisbn) if len(cisbn) == 10 else cisbn
return None
return ''


# TODO(make return type consistent)!
def ean13(isbnlike):
"""Transform an `isbnlike` string in an EAN number (canonical ISBN-13)."""
ib = canonical(isbnlike)
if len(ib) == 13:
return ib if is_isbn13(ib) else None
return ib if is_isbn13(ib) else ''
elif len(ib) == 10:
return to_isbn13(ib) if is_isbn10(ib) else None
return None
return to_isbn13(ib) if is_isbn10(ib) else ''
return ''


# Alias
Expand Down
38 changes: 19 additions & 19 deletions isbnlib/test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def test_check_digit10():
"""Test check digit algo for ISBN-10."""
assert_equals(check_digit10('082649752'), '7')
assert_equals(check_digit10('585270001'), '0')
assert_equals(check_digit10('08264975X'), None)
assert_equals(check_digit10('08264975'), None)
assert_equals(check_digit10('08264975X'), '')
assert_equals(check_digit10('08264975'), '')


def test_check_digit13():
"""Test check digit algo for ISBN-13."""
assert_equals(check_digit13('978082649752'), '9')
assert_equals(check_digit13('97808264975'), None)
assert_equals(check_digit13('97808264975X'), None)
assert_equals(check_digit13('97808264975'), '')
assert_equals(check_digit13('97808264975X'), '')


def test__check_structure10():
Expand Down Expand Up @@ -76,12 +76,12 @@ def test_to_isbn10():
"""Test transformation of ISBN to ISBN-10."""
assert_equals(to_isbn10('9780826497529'), '0826497527')
assert_equals(to_isbn10('0826497527'), '0826497527')
assert_equals(to_isbn10('9780826497520'), None) # ISBN13 not valid
assert_equals(to_isbn10('9790826497529'), None)
assert_equals(to_isbn10('97808264975X3'), None)
assert_equals(to_isbn10('978-826497'), None) # (bug #14)
assert_equals(to_isbn10('9780826497520'), '') # ISBN13 not valid
assert_equals(to_isbn10('9790826497529'), '')
assert_equals(to_isbn10('97808264975X3'), '')
assert_equals(to_isbn10('978-826497'), '') # (bug #14)
assert_equals(to_isbn10('isbn 0-8264-9752-7'), '0826497527')
assert_equals(to_isbn10('isbn 979-10-90636-07-1'), None)
assert_equals(to_isbn10('isbn 979-10-90636-07-1'), '')
assert_equals(to_isbn10('isbn 978-0-8264-9752-9'), '0826497527')
assert_equals(to_isbn10('asdadv isbn 978-0-8264-9752-9'), '0826497527')

Expand All @@ -90,8 +90,8 @@ def test_to_isbn13():
"""Test transformation of ISBN to ISBN-13."""
assert_equals(to_isbn13('0826497527'), '9780826497529')
assert_equals(to_isbn13('9780826497529'), '9780826497529')
assert_equals(to_isbn13('0826497520'), None) # ISBN10 not valid
assert_equals(to_isbn13('08X6497527'), None)
assert_equals(to_isbn13('0826497520'), '') # ISBN10 not valid
assert_equals(to_isbn13('08X6497527'), '')
assert_equals(to_isbn13('91-43-01019-9'), '9789143010190') # (bug #14)
assert_equals(to_isbn13('isbn 91-43-01019-9'), '9789143010190')
assert_equals(to_isbn13('asd isbn 979-10-90636-07-1 blabla'), '9791090636071')
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_get_isbnlike():
assert_equals(len(get_isbnlike(ISBNs, 'normal')), 78)
assert_equals(len(get_isbnlike(ISBNs, 'strict')), 69)
assert_equals(len(get_isbnlike(ISBNs, 'loose')), 81)
assert_equals(get_isbnlike(ISBNs, 'e'), None)
assert_equals(get_isbnlike(ISBNs, 'e'), [])


def test_get_canonical_isbn():
Expand All @@ -139,10 +139,10 @@ def test_get_canonical_isbn():
assert_equals(
get_canonical_isbn('ISBN 0826497527', output='isbn13'), '9780826497529'
)
assert_equals(get_canonical_isbn('ISBN 0826497527', output='NOOPTION'), None)
assert_equals(get_canonical_isbn('0826497520'), None)
assert_equals(get_canonical_isbn('ISBN 0826497527', output='NOOPTION'), '')
assert_equals(get_canonical_isbn('0826497520'), '')
assert_equals(get_canonical_isbn('9780826497529'), '9780826497529')
assert_equals(get_canonical_isbn('9780826497520'), None)
assert_equals(get_canonical_isbn('9780826497520'), '')
assert_equals(get_canonical_isbn('OSX 9780826497529.pdf'), '9780826497529')


Expand All @@ -164,12 +164,12 @@ def test_canonical():

def test_EAN13():
"""Test the extraction and validation of EAN13 from ISBN-like string."""
assert_equals(EAN13('ISBN 9789720404427'), None)
assert_equals(EAN13('ISBN 9789720404427'), '')
assert_equals(EAN13('ISBN 9789720404428'), '9789720404428')
assert_equals(EAN13('ISBN-9780826497529'), '9780826497529')
assert_equals(EAN13('ISBN9780826497529'), '9780826497529')
assert_equals(EAN13('isbn9780826497529'), '9780826497529')
assert_equals(EAN13('isbn 0826497527'), '9780826497529')
assert_equals(EAN13('9700000000000'), None)
assert_equals(EAN13('9000000000000'), None)
assert_equals(EAN13('9710000000000'), None)
assert_equals(EAN13('9700000000000'), '')
assert_equals(EAN13('9000000000000'), '')
assert_equals(EAN13('9710000000000'), '')
4 changes: 2 additions & 2 deletions isbnlib/test/test_isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def test_doi():
assert_equals(isbn.doi, '10.978.1250/158062')

def test_issued():
"""Test the 'Isbn class' for 'info'."""
"""Test the 'Isbn class' for 'issued'."""
assert_equals(isbn.issued, True)
isbn2=Isbn('9786610326266')
assert_equals(isbn2.issued, False)

def test_info():
"""Test the 'Isbn class' for 'issued'."""
"""Test the 'Isbn class' for 'info'."""
assert_equals(isbn.info, 'English language')

def test_errors():
Expand Down

0 comments on commit 9993802

Please sign in to comment.