checkdigit
Advanced tools
| # /usr/bin/env python | ||
| # This file is part of checkdigit. | ||
| # checkdigit is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # checkdigit is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with checkdigit. If not, see <http://www.gnu.org/licenses/>. | ||
| """GS1 Validation Functions.""" | ||
| import math | ||
| from checkdigit._data import cleanse, convert | ||
| # WARNING: Data beginning with 0 must be as a string due to PEP 3127 | ||
| def calculate(data: str) -> str: | ||
| """Calculates GS1 Check Digit. | ||
| This method works for all fixed length numeric GS1 data structures | ||
| (including GDTI, GLN, GRAI, etc.) that require a check digit. | ||
| Args: | ||
| data: A string of characters | ||
| Returns: | ||
| str: The check digit that was missing | ||
| """ | ||
| data = cleanse(data) | ||
| data = data[::-1] # Reverse the barcode, as last digit is always multiplied by 3 | ||
| total_sum = 0 | ||
| for index, value in enumerate(data): | ||
| if index % 2 == 0: | ||
| total_sum += int(value) * 3 | ||
| else: | ||
| total_sum += int(value) | ||
| next_multiple_of_ten = int(math.ceil(total_sum / 10.0)) * 10 | ||
| check_digit = next_multiple_of_ten - total_sum | ||
| return convert(check_digit, "gs1") | ||
| def validate(data: str) -> bool: | ||
| """Validates GS1. | ||
| This method works for all fixed length numeric GS1 data structures | ||
| (including GDTI, GLN, GRAI, etc.) that require a check digit. | ||
| Args: | ||
| data: A string of characters representing a full GS1 code | ||
| Returns: | ||
| bool: A boolean representing whether the | ||
| check digit validates the data or not | ||
| """ | ||
| data = cleanse(data) | ||
| return calculate(data[:-1]) == data[-1] | ||
| def missing(data: str) -> str: | ||
| """Calculates a missing digit in a GS1 Code. | ||
| This method works for all fixed length numeric GS1 data structures | ||
| (including GDTI, GLN, GRAI, etc.) that require a check digit. | ||
| Args: | ||
| data: A string of characters representing a full ISBN code | ||
| with a question mark representing a missing character | ||
| Returns: | ||
| str: The missing value that should've been where the question mark was | ||
| """ | ||
| data = cleanse(data) | ||
| for poss_digit in range(10): # Brute Force the 10 options | ||
| option = convert(poss_digit) | ||
| # tests it with the generated number | ||
| # If this fails, the next number is tried | ||
| if validate(data.replace("?", option)): | ||
| return option | ||
| return "Invalid" |
+2
-1
| Metadata-Version: 2.1 | ||
| Name: checkdigit | ||
| Version: 0.1.1 | ||
| Version: 0.1.2 | ||
| Summary: A check digit library for data validation | ||
@@ -48,2 +48,3 @@ Home-page: https://github.com/harens/checkdigit | ||
| * Contains various functions relating to __Luhn, ISBN, UPC and many other codes__. | ||
| * __[PEP 561 compatible](https://www.python.org/dev/peps/pep-0561)__, with built in support for type checking. | ||
| * Extensive __in-code comments and docstrings__ to explain how the functions work. | ||
@@ -50,0 +51,0 @@ * Written in __pure Python__ with __no dependencies__ required to run the program. |
+6
-6
| [tool.poetry] | ||
| name = "checkdigit" | ||
| version = "0.1.1" | ||
| version = "0.1.2" | ||
| description = "A check digit library for data validation" | ||
@@ -30,8 +30,8 @@ authors = ["harens <harensdeveloper@gmail.com>"] | ||
| error404 = "^1.1.8a0" | ||
| pylint = "^2.6.0" | ||
| mypy = "^0.790" | ||
| pylint = "^2.8.2" | ||
| mypy = "^0.812" | ||
| black = "^20.8b1" | ||
| isort = "^5.6.4" | ||
| pydocstyle = "^5.1.1" | ||
| coverage = "^5.3" | ||
| isort = "^5.8.0" | ||
| pydocstyle = "^6.0.0" | ||
| coverage = "^5.5" | ||
@@ -38,0 +38,0 @@ [tool.coverage.run] |
+1
-0
@@ -21,2 +21,3 @@ # checkdigit | ||
| * Contains various functions relating to __Luhn, ISBN, UPC and many other codes__. | ||
| * __[PEP 561 compatible](https://www.python.org/dev/peps/pep-0561)__, with built in support for type checking. | ||
| * Extensive __in-code comments and docstrings__ to explain how the functions work. | ||
@@ -23,0 +24,0 @@ * Written in __pure Python__ with __no dependencies__ required to run the program. |
+2
-2
@@ -12,5 +12,5 @@ # -*- coding: utf-8 -*- | ||
| 'name': 'checkdigit', | ||
| 'version': '0.1.1', | ||
| 'version': '0.1.2', | ||
| 'description': 'A check digit library for data validation', | ||
| 'long_description': '# checkdigit\n\nA check digit library for data validation.\n \n| Test Status | [](https://github.com/harens/checkdigit/actions) [](https://codecov.io/gh/harens/checkdigit) |\n|:--|:--|\n| __Version Info__ | [](https://pypi.org/project/checkdigit/) [](https://github.com/harens/checkdigit/releases) [](https://pypi.org/project/checkdigit/) |\n| __Code Analysis__ |[](https://codeclimate.com/github/harens/checkdigit) [](https://www.codefactor.io/repository/github/harens/checkdigit) [](https://lgtm.com/projects/g/harens/checkdigit/)|\n\n## 🔨 Installation\n\n```shell\npip install checkdigit\n```\n\nOr download the project [here](https://github.com/harens/checkdigit/archive/master.zip).\n\n## ✨ Features\n\n* Contains various functions relating to __Luhn, ISBN, UPC and many other codes__.\n* Extensive __in-code comments and docstrings__ to explain how the functions work.\n* Written in __pure Python__ with __no dependencies__ required to run the program.\n\nCheck out the [documentation](https://github.com/harens/checkdigit/wiki) for more details on how to use the library.\n\n## 🏗️ Contributing\n\nAny change, big or small, that you think can help improve this project is more than welcome 🎉.\n\nAs well as this, feel free to open an issue with any new suggestions or bug reports. Every contribution is valued.\n\nFor smaller tasks (that are still really appreciated 😃), be sure to check the [good first issue](https://github.com/harens/checkdigit/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) tag.\n\n## 💻 Setup\n\nClone the project and install the dev dependencies:\n\n```shell\ngit clone https://github.com/harens/checkdigit\ncd checkdigit\npoetry install\n```\n\nIf you want to send a PR, please run the following:\n\n```bash\nbash ./scripts/format.sh # Format files\nbash ./scripts/tests.sh # Run tests\n\n# NB shellcheck is not installed by poetry\nshellcheck scripts/*.sh\n```\n\n## 📒 License\n\nThis project is licensed under [GPL-3.0-or-later](https://github.com/harens/checkdigit/blob/master/LICENSE).\n', | ||
| 'long_description': '# checkdigit\n\nA check digit library for data validation.\n \n| Test Status | [](https://github.com/harens/checkdigit/actions) [](https://codecov.io/gh/harens/checkdigit) |\n|:--|:--|\n| __Version Info__ | [](https://pypi.org/project/checkdigit/) [](https://github.com/harens/checkdigit/releases) [](https://pypi.org/project/checkdigit/) |\n| __Code Analysis__ |[](https://codeclimate.com/github/harens/checkdigit) [](https://www.codefactor.io/repository/github/harens/checkdigit) [](https://lgtm.com/projects/g/harens/checkdigit/)|\n\n## 🔨 Installation\n\n```shell\npip install checkdigit\n```\n\nOr download the project [here](https://github.com/harens/checkdigit/archive/master.zip).\n\n## ✨ Features\n\n* Contains various functions relating to __Luhn, ISBN, UPC and many other codes__.\n* __[PEP 561 compatible](https://www.python.org/dev/peps/pep-0561)__, with built in support for type checking.\n* Extensive __in-code comments and docstrings__ to explain how the functions work.\n* Written in __pure Python__ with __no dependencies__ required to run the program.\n\nCheck out the [documentation](https://github.com/harens/checkdigit/wiki) for more details on how to use the library.\n\n## 🏗️ Contributing\n\nAny change, big or small, that you think can help improve this project is more than welcome 🎉.\n\nAs well as this, feel free to open an issue with any new suggestions or bug reports. Every contribution is valued.\n\nFor smaller tasks (that are still really appreciated 😃), be sure to check the [good first issue](https://github.com/harens/checkdigit/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) tag.\n\n## 💻 Setup\n\nClone the project and install the dev dependencies:\n\n```shell\ngit clone https://github.com/harens/checkdigit\ncd checkdigit\npoetry install\n```\n\nIf you want to send a PR, please run the following:\n\n```bash\nbash ./scripts/format.sh # Format files\nbash ./scripts/tests.sh # Run tests\n\n# NB shellcheck is not installed by poetry\nshellcheck scripts/*.sh\n```\n\n## 📒 License\n\nThis project is licensed under [GPL-3.0-or-later](https://github.com/harens/checkdigit/blob/master/LICENSE).\n', | ||
| 'author': 'harens', | ||
@@ -17,0 +17,0 @@ 'author_email': 'harensdeveloper@gmail.com', |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
62368
5.39%13
8.33%370
22.11%