Skip to content

Commit

Permalink
add more tests and examples on README
Browse files Browse the repository at this point in the history
  • Loading branch information
xlcnd committed Feb 8, 2015
1 parent ed4e419 commit c56a8c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
19 changes: 17 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ Typical usage (as library):
...
**NOTE**
The official form of an ISBN is something like 'ISBN 979-10-90636-07-1'. However for most
applications only the numbers are important and you can always masked them if you need (see below).
This library works mainly with 'striped' ISBNs (only numbers and X) like '0826497527'. You can
strip an isbn's like string by using ``canonical(isbnlike)`` (see below). You can
'mask' the isbn by using ``mask(isbn)``. So in the examples below, when you see 'isbn'
in the argument, it is a 'striped' ISBN, when the argument is an 'isbnlike' it is a string
like 'ISBN 979-10-90636-07-1' or even something dirty like 'asdf 979-10-90636-07-1 bla bla'.
Main Functions
--------------

Expand All @@ -71,13 +81,13 @@ Main Functions
Transform isbn-10 to isbn-13.

``canonical(isbnlike)``
Keep only numbers and X. You will get strings like `9780321534965`.
Keep only numbers and X. You will get strings like `9780321534965` and `954430603X`.

``clean(isbnlike)``
Clean ISBN (only legal characters).

``notisbn(isbnlike, level='strict')``
Check with the goal to invalidate isbn-like.
Check with the goal to invalidate isbn-like.

``get_isbnlike(text, level='normal')``
Extract all substrings that seem like ISBNs (very useful for scraping).
Expand Down Expand Up @@ -127,6 +137,7 @@ Main Functions
``ren(filename)``
Rename a file using metadata from an ISBN in his filename.

See the file test_core_ and test_ext_ for **a lot of examples**.


Install
Expand Down Expand Up @@ -278,3 +289,7 @@ Read ``isbnlib`` code in a very sctructured way at sourcegraph_ or 'the docs' at
.. _readthedocs: http://bit.ly/ISBNLib_rtd

.. _stackoverflow: http://stackoverflow.com/questions/tagged/isbnlib

.. _test_core: https://github.com/xlcnd/isbnlib/blob/master/isbnlib/test/test_core.py

.. _test_ext: https://github.com/xlcnd/isbnlib/blob/master/isbnlib/test/test_ext.py
6 changes: 5 additions & 1 deletion isbnlib/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ def to_isbn10(isbn13):
"""Transform isbn-13 to isbn-10."""
isbn13 = canonical(isbn13)
# Check prefix
if isbn13[:3] != ISBN13_PREFIX:
if isbn13[:3] != ISBN13_PREFIX:
return isbn13 if len(isbn13) == 10 and is_isbn10(isbn13) else None
if not is_isbn13(isbn13):
return None
isbn10 = isbn13[3:]
check = _check_digit10(isbn10[:-1])
# Change check digit
Expand All @@ -133,6 +135,8 @@ def to_isbn13(isbn10):
isbn10 = canonical(isbn10)
if len(isbn10) == 13 and is_isbn13(isbn10):
return isbn10
if not is_isbn10(isbn10):
return None
isbn13 = ISBN13_PREFIX + isbn10[:-1]
check = _check_digit13(isbn13)
return isbn13 + check if check else None
Expand Down
14 changes: 11 additions & 3 deletions isbnlib/test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ def test__check_structure13():

def test_is_isbn10():
assert_equals(is_isbn10('0826497527'), True)
assert_equals(is_isbn10('isbn 0-8264-9752-7'), True)
assert_equals(is_isbn10('0826497520'), False)
assert_equals(is_isbn10('954430603X'), True)


def test_is_isbn13():
assert_equals(is_isbn13('9780826497529'), True)
assert_equals(is_isbn13('9791090636071'), True)
assert_equals(is_isbn13('isbn 979-10-90636-07-1'), True)
assert_equals(is_isbn13('9780826497520'), False)
assert_equals(is_isbn13('9700000000000'), False)
assert_equals(is_isbn13('9000000000000'), False)
Expand All @@ -54,18 +56,24 @@ def test_is_isbn13():
def test_to_isbn10():
assert_equals(to_isbn10('9780826497529'), '0826497527')
assert_equals(to_isbn10('0826497527'), '0826497527')
assert_equals(to_isbn10('9780826497520'), '0826497527') # ISBN13 not valid
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('isbn 0-8264-9752-7'), '0826497527')
assert_equals(to_isbn10('isbn 979-10-90636-07-1'), None)
assert_equals(to_isbn10('isbn 978-0-8264-9752-9'), '0826497527')
assert_equals(to_isbn10('asdadv isbn 978-0-8264-9752-9'), '0826497527')


def test_to_isbn13():
assert_equals(to_isbn13('0826497527'), '9780826497529')
assert_equals(to_isbn13('9780826497529'), '9780826497529')
assert_equals(to_isbn13('0826497520'), '9780826497529') # ISBN10 not valid
assert_equals(to_isbn13('0826497520'), None) # ISBN10 not valid
assert_equals(to_isbn13('08X6497527'), None)
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')


def test_clean():
Expand Down Expand Up @@ -122,7 +130,7 @@ def test_canonical():
assert_equals(canonical('ISBN9780826497529'), '9780826497529')
assert_equals(canonical('isbn9780826497529'), '9780826497529')
assert_equals(canonical('isbn 0826497527'), '0826497527')
assert_equals(get_canonical_isbn('954430603x'), '954430603X')
assert_equals(canonical('954430603x'), '954430603X')


def test_EAN13():
Expand Down

0 comments on commit c56a8c4

Please sign in to comment.