checkdigit: Check Digit Library for Data Validation
checkdigit is a Python library providing algorithms for various check digit standards, facilitating data validation and error detection. It supports algorithms like Luhn, ISBN, UPC, EAN, ISIN, Modulo 10, Modulo 11, GS1, and CRC. The current version is 0.5.0, with releases occurring periodically to introduce new features, fix bugs, and refine existing algorithms.
Warnings
- breaking The separate ISBN-10 and ISBN-13 validation/generation functions (e.g., `isbn10`, `isbn13`) were removed. All ISBN operations are now handled by a single `isbn` module.
- breaking Parity methods (e.g., `checkdigit.parity.missing()`) no longer return the full block of data. They now return only the calculated missing bit, aligning with the return behavior of other check digit methods.
- breaking A significant refactoring in v0.1 led to many function renames to improve clarity and consistency. Code written for pre-0.1 versions will likely break.
- gotcha Prior to v0.5.0, ISBN-13 validation incorrectly allowed 'X' as a check digit, which is only valid for ISBN-10. This could lead to incorrect validations for ISBN-13 codes.
Install
-
pip install checkdigit
Imports
- luhn
from checkdigit import luhn
- isbn
from checkdigit import isbn
- gs1
from checkdigit import gs1
Quickstart
from checkdigit import luhn
from checkdigit import isbn
# Validate a number using the Luhn algorithm
is_valid_luhn = luhn.validate("79927398713")
print(f"'79927398713' is valid Luhn: {is_valid_luhn}")
# Generate a check digit for a number
luhn_with_check = luhn.generate("7992739871")
print(f"'7992739871' with Luhn check digit: {luhn_with_check}")
# Validate an ISBN (supports both ISBN-10 and ISBN-13)
is_valid_isbn = isbn.validate("978-3-16-148410-0")
print(f"'978-3-16-148410-0' is valid ISBN: {is_valid_isbn}")