Skip to content

Commit

Permalink
Badges Added
Browse files Browse the repository at this point in the history
  • Loading branch information
harens committed Jan 19, 2019
1 parent 2c49c28 commit 9f601aa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# checkdigit
Checking digits with a digit!
![](https://img.shields.io/travis/com/harens/checkdigit.svg?style=for-the-badge) ![https://pypi.org/project/checkdigit/](https://img.shields.io/pypi/v/checkdigit.svg?logoColor=violet&style=for-the-badge) ![https://pypi.org/project/checkdigit/](https://img.shields.io/pypi/format/checkdigit.svg?style=for-the-badge) ![https://pypi.org/project/checkdigit/](https://img.shields.io/pypi/status/checkdigit.svg?style=for-the-badge) ![https://pypi.org/project/checkdigit/](https://img.shields.io/pypi/pyversions/checkdigit.svg?style=for-the-badge)
__Checking digits with a digit!__

## Installation
```shell
Expand All @@ -8,4 +9,4 @@ pip install checkdigit
Or download the project [here](https://github.com/harens/checkdigit/archive/master.zip)

## License
This project is licensed under the [GNU General Public License v3.0](https://github.com/harens/checkdigit/master/LICENSE)
This project is licensed under the [GNU General Public License v3.0](https://github.com/harens/checkdigit/blob/master/LICENSE)
25 changes: 25 additions & 0 deletions checkdigit/isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ def isbn13calculate(data):

def isbn13check(data):
return isbn13calculate(data[:9]) == data[-1] # Sees if check digit is valid


# p = position of ?
# total_sum = 10a + 9b + ... j (excluding p?)
# For ISBN-10: ? = (121 - total_sum - (11 x check digit)) / p


def calculate_missing(data):
char_pos = 10 - data.index("?") # What ? is multiplied by
total_sum = 0
multiply_counter = 10
for item in data:
if item == "X":
total_sum += 10
elif item != "?":
total_sum += int(item) * multiply_counter
multiply_counter -= 1
poss_output = 0
while True:
final_value = ((11 * poss_output) - total_sum) / char_pos
if int(final_value) == final_value and final_value >= 0:
if final_value == 10:
return "X"
return str(int(final_value)) # Better to not have .0
poss_output += 1
14 changes: 7 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@
packages=find_packages(exclude=("tests",)),
author_email="[email protected]",
url="https://harens.github.io",
download_url='https://github.com/harens/checkdigit/tarball/0.0.1',
download_url="https://github.com/harens/checkdigit/tarball/0.0.1",
classifiers=[
"Development Status :: 3 - Alpha",
'Operating System :: OS Independent',
'Intended Audience :: Information Technology',
'Intended Audience :: Education',
"Operating System :: OS Independent",
"Intended Audience :: Information Technology",
"Intended Audience :: Education",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
'Programming Language :: Python',
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
],
project_urls={
'Source': 'https://github.com/harens/checkdigit',
'Tracker': 'https://github.com/harens/checkdigit/issues',
"Source": "https://github.com/harens/checkdigit",
"Tracker": "https://github.com/harens/checkdigit/issues",
},
keywords="Check Digits, Validation, ISBN",
)
31 changes: 25 additions & 6 deletions tests/test_isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

import sys
import time

sys.path.append('../') # Go back a directory
sys.path.append("../") # Go back a directory
from checkdigit import isbn

number_success = 0
number_failed = 0
total_tests = 0


# Test function
# Test function (Credit to spscah)
def test(function, value):
global total_tests
total_tests += 1
Expand All @@ -41,6 +42,8 @@ def test(function, value):
print("")


start_time = time.time()

test(isbn.evenparity("0110"), "01100")
test(isbn.evenparity("0"), "00")
test(isbn.evenparity("01101"), "011011")
Expand All @@ -53,16 +56,32 @@ def test(function, value):

test(isbn.isbn13calculate("012345678912"), "8")

# For ISBN-10
test(isbn.calculate_missing("15688?111X"), "1")
test(isbn.calculate_missing("812071988?"), "3")
test(isbn.calculate_missing("020161586?"), "X")
test(isbn.calculate_missing("?131103628"), "0")
test(isbn.calculate_missing("?86046324X"), "1")
test(isbn.calculate_missing("1?68811306"), "5")
test(isbn.calculate_missing("951?451570"), "4")
test(isbn.calculate_missing("0393020?31"), "2")
test(isbn.calculate_missing("01367440?5"), "9")

finish_time = time.time()
final_time = round(finish_time - start_time, 4)

print("")
if number_failed == 0:
print("Out of {0} tests, all succeeded".format(total_tests))
print(
"Out of {0} tests, all succeeded in {1} seconds".format(total_tests, final_time)
)
elif number_success == 0:
print("Out of {0} tests, all failed".format(total_tests))
print("Out of {0} tests, all failed in {1} seconds".format(total_tests, final_time))
exit(1)
else:
print(
"Out of {0} tests, {1} succeeded and {2} failed".format(
total_tests, number_success, number_failed
"Out of {0} tests, {1} succeeded and {2} failed in {3} seconds".format(
total_tests, number_success, number_failed, final_time
)
)
# Success rate rounded to 2 d.p.
Expand Down

0 comments on commit 9f601aa

Please sign in to comment.