allisbns.isbn¶
Classes and functions to work with ISBNs.
- allisbns.isbn.ISBN_PATTERN: Pattern[str] = re.compile('^(?:978|979)?\\d{9}[\\dXx]$')¶
A simple pattern to match ISBN-10 and ISBN-13 values.
- allisbns.isbn.TOTAL_ISBNS: Final = 2000000000¶
Total number of ISBNs.
- allisbns.isbn.ISBN12_LENGTH = 12¶
The length of ISBN-12 numbers.
- class allisbns.isbn.ISBNBounds(start: ISBN12, end: ISBN12)¶
Bases:
NamedTupleRepresents ISBN bounds.
- class allisbns.isbn.CanonicalISBN(value: MaybeISBN)¶
Bases:
objectRepresents a canonical (normalized) ISBN-13 string.
Only the ISBN-10 and ISBN-13
patterncheck is performed upon creation. So canonical ISBN values are assumed to be valid even if: the registration group or registrant range are undefined, the correct digit is incorrectly calculated. For validation, seevalidate_isbn().See also
- class allisbns.isbn.MaskedISBN(bookland: Literal['978', '979'], group: str, registrant: str, publication: str, check_digit: str)¶
Bases:
objectRepresents a masked structure of an ISBN-13 string.
Example
- It is more practical to create it from a canonical ISBN:
>>> canonical = normalize_isbn("978-000-000-000-X") >>> MaskedISBN.from_canonical(canonical) MaskedISBN( bookland='978', group='0', registrant='00', publication='000000', check_digit='X', )
- classmethod from_canonical(isbn: CanonicalISBN) Self¶
Creates a masked structure from a canonical ISBN-13 string.
- allisbns.isbn.ensure_isbn12(value: int | integer) ISBN12¶
Ensures that a value is an ISBN-12 number.
- allisbns.isbn.calculate_check_digit(isbn: CanonicalISBN | ISBN12) int¶
Calculates the check digit for an ISBN.
- allisbns.isbn.normalize_isbn(value: MaybeISBN | ISBN12, correct: bool = False) CanonicalISBN¶
Normalizes an ISBN to a canonical form.
- Parameters:
value – An value to normalize.
correct – Whether to check and replace the incorrect check digit with the correct one. Defaults to False.
Examples
The input ISBN-13 string with the correct check digit:
>>> normalize_isbn("978-000-000-000-2") CanonicalISBN(9780000000002)
The input ISBN-13 string with the incorrect check digit:
>>> # Keep it as it is >>> normalize_isbn("978-000-000-000-3") CanonicalISBN(9780000000003)
>>> # Or, correct the check digit >>> normalize_isbn("978-000-000-000-3", correct=True) CanonicalISBN(9780000000002)
The input
ISBN12integer value:
>>> value: ISBN12 = 978_000_000_000 >>> normalize_isbn(value) CanonicalISBN(9780000000002)
- allisbns.isbn.validate_isbn(value: CanonicalISBN, *, return_reasons: bool = False) bool | tuple[bool, list[InvalidISBNReason]]¶
Validates a canonical ISBN.
The validation includes checks for: (1) an undefined registration group, (2) an undefined registrant, and (3) an incorrect check digit. To check if a string is an ISBN or not, see
match_isbn()instead.- Parameters:
value – A canonical ISBN to validate.
return_reasons – Whether to return reasons for failed checks or not.
- Returns:
A boolean value or, if
return_reasonsis requested, a boolean value and a list of reasons for failed checks.
See also
Examples
If you need to check a list of unsure ISBNs for validity, you can combine this function with
match_isbn():def check_isbn_for_errors(x: MaybeISBN) -> list[InvalidISBNReason]: if not match_isbn(x): return [InvalidISBNReason.NOT_ISBN] else: canonical_isbn = normalize_isbn(x) _, errors = validate_isbn(canonical_isbn, return_reasons=True) return errors
- allisbns.isbn.get_prefix_bounds(prefix: str) ISBNBounds¶
Gets bounds for an ISBN prefix.
- allisbns.isbn.extract_group_prefix(isbn: CanonicalISBN) str¶
Extracts the group prefix from the ISBN.
- Parameters:
isbn – A canonical ISBN-13 string.
- Returns:
A found full ISBN group prefix, e.g. ‘978-1’.
- Raises:
InvalidISBNError – If the ISBN does not contain valid group prefix.
Note
Group ranges were periodically updated from the RangeMessage.xml file available from the International ISBN Agency and may not be the most recent.
See also
- allisbns.isbn.generate_isbns(start_isbn: ISBN12 | None = None, limit: int | None = None) Generator[CanonicalISBN]¶
Yields canonical ISBN-13 string values.
- Parameters:
start_isbn – A start ISBN-12 number. Defaults to None, meaning yields from the first ISBN-13, ‘978-000-000-000-2’.
limit – A maximum number of ISBNs to yields. Defaults to None, meaning yields till the last ISBN-13, ‘979-999-999-999-0’.