Registry / serialization / checkdigit

checkdigit

JSON →
library0.5.0pypypi✓ verified 26d ago

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.

pip install checkdigit
INSTALL
IMPORT
SIG · CHECKDIGIT
C
checkdigit
serializationpythonv0.5.0
Install
1.5s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.010s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

luhn
from checkdigit import luhn
Import specific algorithm modules (e.g., luhn, isbn, upc) directly from the top-level package.
isbn
from checkdigit import isbn
from checkdigit.formats import isbn10, isbn13
As of v0.3.0, dedicated isbn10 and isbn13 functions were removed. Use the unified `isbn` module.
gs1
from checkdigit import gs1

This quickstart demonstrates how to import and use the `luhn` and `isbn` modules to validate and generate check digits. The `isbn` module automatically handles both ISBN-10 and ISBN-13 formats.

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}")
Debug
Known issues
breakingThe 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.
fix
Migrate usage from `checkdigit.formats.isbn10` or `checkdigit.formats.isbn13` to `checkdigit.isbn.validate()` or `checkdigit.isbn.generate()`. The unified `isbn` module automatically detects the ISBN type.
affects: >=0.3.0
breakingParity 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.
fix
Adjust code to expect only the missing bit as the return value from parity methods, rather than a full data structure.
affects: >=0.2.0
breakingA 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.
fix
Consult the official documentation or changelog for specific function name changes if migrating from versions prior to 0.1. It's generally recommended to upgrade and adapt to the current API.
affects: >=0.1
gotchaPrior 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.
fix
Upgrade to checkdigit version 0.5.0 or newer to ensure correct ISBN-13 validation behavior.
affects: <0.5.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'checkdigit'
The 'checkdigit' library has not been installed in your Python environment.
fix
Install the library using pip: `pip install checkdigit`
ValueError: Data must be a numeric string
A function in the `checkdigit` library that expects a string containing only digits received a string with non-numeric characters.
fix
Ensure the input string passed to the checkdigit function contains only numeric characters. For ISBN-10, handle 'X' as a special case if the function doesn't automatically convert it, typically by ensuring your input is sanitized.
ValueError: Invalid length for <algorithm> data
The input string provided to a checkdigit function does not match the expected length for that specific algorithm (e.g., ISBN-10 requires 10 digits, ISBN-13 requires 13 digits).
fix
Provide an input string with the correct number of digits required by the specific check digit algorithm being used. Refer to the library's documentation for exact length requirements for each algorithm.
TypeError: <function_name> expected str, got int
A checkdigit function expecting a string input received an integer (or another incorrect data type) instead.
fix
Convert the input number to a string before passing it to the checkdigit function. For example, use `str(12345)` instead of `12345`.
Upgrade
Version history
0.5.0latest on PyPI · released Apr 9, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
18
OpenAI (training)
1
Resources
checkdigit — pip install checkdigit · libregistry