checkdigit is a pure Python library built for identification numbers. You want to validate a credit card number, or maybe even calculate a missing digit on an ISBN code? We've got you covered π.
Want to know more? Check out the API Reference and documentation!
MacPorts π
sudo port install py-checkdigit
PyPi π
pip install checkdigit
- PEP 561 compatible, with built in support for type checking.
- Capable of calculating missing digits or validating a block of data.
- Extensive in-code comments and docstrings to explain how it works behind the scenes. πͺ
- Even and odd binary parity
- Bookland
- CRC (credit to @sapieninja)
- EAN-13
- GS1 (credit to @OtherBarry)
- ISBN-10
- ISBN-13
- Luhn
- UPC-A
For each of these formats, we provide functions to validate them and calculate missing digits.
Do you have any formats that you'd like to see supported? π€ Feel free to raise an issue, or even to send a pull request!
- Issue Tracker: https://github.com/harens/checkdigit/issues
- Source Code: https://github.com/harens/checkdigit
Any change, big or small, that you think can help improve this project is more than welcome π.
As well as this, feel free to open an issue with any new suggestions or bug reports. Every contribution is appreciated.
First, fork the project to your account. Then, run the following with your GitHub handle in place of
INSERT_GITHUB_NAME
:
git clone https://github.com/INSERT_GITHUB_NAME/checkdigit
poetry install && poetry shell
pre-commit install
checkdigit βββ scripts β βββ format.sh β βββ tests.sh βββ checkdigit β βββ gs1.py β βββ isbn.py β βββ luhn.py β βββ etc. βββ tests
Each new format goes into a separate file which is named accordingly. Similar formats (e.g. ISBN-10 and ISBN-13) should go in the same file.
Before submitting any new changes, please run the format.sh
and tests.sh
scripts beforehand. Thank you :)
The documentation can be found in docs/source
.
We can use sphinx-autobuild to continuously rebuild the docs when changes are made.
sphinx-autobuild docs/source docs/_build/html
Each of the Python files follow the same general format:
# License + File docstring
from checkdigit._data import cleanse, convert
def calculate(data: str) -> str:
"""Determines check digit.
Args:
data: A string of data missing a check digit
Returns:
str: The single missing check digit (not the whole block of data)
"""
# This helps to deal with user formatting inconsistencies
# e.g. spaces, hyphens, etc.
data = cleanse(data)
# Deals with 10 or 11 being the possible check digit
return convert(...)
def validate(data: str) -> bool:
"""Validates a block of data from the check digit.
Args:
data: A string representing a full block of data
Returns:
bool: A boolean representing whether the data is valid or not
"""
data = cleanse(data)
# Remove the check digit and see if it matches
return calculate(data[:-1]) == data[-1]
def missing(data: str) -> str:
"""Returns the missing digit from a block of data.
Args:
data: A string with a question mark in the place of a missing digit.
Returns:
A string representing the missing digit (not the whole block of data)
"""
data = cleanse(data)
return ...
For similar data formats, the names can be adjusted accordingly (e.g. validate10
for ISBN-10 and validate13
for ISBN-13).
Thanks goes to these wonderful people (emoji key):
Timothy Langer |
Charlie Wilson π» |
Max Bowman π» |
This project follows the all-contributors specification. Contributions of any kind welcome!
This project is licensed under GPL-3.0-or-later.