Skip to main content

prettyisbns

Tests

A Python package for normalizing, fixing, and formatting ISBNs to make them pretty.

Installation

The package is available on PyPI:

pip install prettyisbns

Usage

Normalize and fix ISBNs

Normalize an ISBN and fix its check digit:

>>> from prettyisbns import normalize
>>> canonical = normalize("23-6590-117-X", fix=True)
>>> canonical
CanonicalISBN(9782365901178)

The result is a canonical, normalized ISBN-13 string without hyphens.

Format ISBNs

Format a canonical ISBN with hyphens to split it into elements:

>>> canonical.hyphenate()
'978-2-36590-117-8'

To normalize, fix, and format in one step, use the pretty() shortcut:

>>> from prettyisbns import pretty
>>> pretty("23-6590-117-X")
'978-2-36590-117-8'

Formatting relies on the registration groups and registrant ranges from the International ISBN Agency, which are stored in the auto-generated prettyisbns.ranges module. Ranges can be redefined, which may cause a discrepancy with the module. In that case, update the package or open an issue.

Access ISBN elements

Internally, formatting is done by ISBNLayout, which splits an ISBN into bookland, registration group, registrant, publication, and check digit elements. You can access it through the layout property, which supports slicing for a granular output:

# Split the canonical ISBN into its elements
>>> canonical.layout
ISBNLayout(
    bookland='978',
    group='2',
    registrant='36590',
    publication='117',
    check_digit='8',
)

# Get the group prefix
>>> canonical.layout[:2]
'978-2'

# Get the registrant prefix
>>> canonical.layout[:3]
'978-2-36590'

Validate ISBNs

Check an ISBN's check digit, registration group, and registrant range with validate(). It returns a ValidationResult, which is truthy only when the ISBN is valid:

if result := canonical.validate():
    print("valid")
else:
    print(f"invalid: {result.errors}")

Handle errors

Normalizing raises NotISBNError if a value is neither ISBN-10 nor ISBN-13, and formatting raises InvalidISBNError if the registration group or registrant range of an ISBN is not defined. Its reason attribute tells what exactly is wrong:

from prettyisbns import pretty
from prettyisbns.errors import InvalidISBNError, NotISBNError

try:
    prettied = pretty(value)
except NotISBNError:
    # The value is neither ISBN-10 nor ISBN-13
    ...
except InvalidISBNError as error:
    # The ISBN cannot be formatted, see error.reason
    ...

API

See the full API reference in the documentation.

License

MIT.

Metadata

Release files for prettyisbns 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prettyisbns 0.1.0
File Size Uploaded
prettyisbns-0.1.0.tar.gz 18.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prettyisbns 0.1.0
File Interpreter ABI Platform
prettyisbns-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.9 kB

Release files / prettyisbns-0.1.0.tar.gz

Download URL prettyisbns-0.1.0.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f7ace08c846d90363ed44575a7785624e1723ef70e70d1a09bfd20696facb9fe
BLAKE2b-256 checksum
How to use checksums
ee9fae0ba154451e59d0b8d1c964544b9c55886009dfff7d321278148d9a8c15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / prettyisbns-0.1.0-py3-none-any.whl

Download URL prettyisbns-0.1.0-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2b27dfda0ba0d5ffdb185d76b4f8a9b8e6a6f83921a5bc8043293f3c8806d58
BLAKE2b-256 checksum
How to use checksums
ff5004eb3e1759fb307d5bf0ab8eb287310ad981924a0c7871676053667956bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page