From 87f589883357e189daa51b98f13dabada25f77cb Mon Sep 17 00:00:00 2001 From: Haren Date: Fri, 25 Jan 2019 13:15:45 +0000 Subject: [PATCH] Support for Integers With the exception of ints beginning with 0, due to PEP 3127 --- checkdigit/isbn.py | 6 ++++++ checkdigit/luhn.py | 6 ++++++ checkdigit/parity.py | 4 ++++ checkdigit/upc.py | 5 +++++ tests/test_isbn.py | 7 +++++-- tests/test_luhn.py | 12 +++++++----- tests/test_parity.py | 2 ++ tests/test_upc.py | 2 ++ 8 files changed, 37 insertions(+), 7 deletions(-) diff --git a/checkdigit/isbn.py b/checkdigit/isbn.py index 226ba8a..1ce3479 100644 --- a/checkdigit/isbn.py +++ b/checkdigit/isbn.py @@ -13,9 +13,11 @@ # You should have received a copy of the GNU General Public License # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 # Calculates ISBN-10 Check Digits def isbn10calculate(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces total_sum = 0 multiply_counter = 10 for item in data: @@ -31,12 +33,14 @@ def isbn10calculate(data): # Validates ISBN-10 def isbn10check(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces return ( isbn10calculate(data[:9]) == data[-1] ) # Determines if calculated Check Digit of the data is the last digit given def isbn13calculate(data, function_name="isbn"): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces if function_name == "isbn": mod_number = 0 else: @@ -59,12 +63,14 @@ def isbn13calculate(data, function_name="isbn"): def isbn13check(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces return ( isbn13calculate(data[:12]) == data[-1] ) # Determines if calculated Check Digit of the data is the last digit given def calculate_missing(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces for poss_digit in range(0, 11): # Brute Force the 11 options if poss_digit == 10: poss_digit = "X" # '10' as a single digit is X diff --git a/checkdigit/luhn.py b/checkdigit/luhn.py index 81a8fae..f428c93 100644 --- a/checkdigit/luhn.py +++ b/checkdigit/luhn.py @@ -15,7 +15,11 @@ # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + +# Calculates Check Digits def luhn_calculate(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces position_counter = 1 # 1-based indexing total_sum = 0 for item in data[::-1]: # Reverses String @@ -37,6 +41,8 @@ def luhn_calculate(data): def luhn_validate(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces + data = (str(data)).replace('-', '') # Removes Hyphens return ( luhn_calculate(data[:-1]) == data[-1] ) # Determines if calculated Check Digit of the data is the last digit given diff --git a/checkdigit/parity.py b/checkdigit/parity.py index a97bf75..03b5db4 100644 --- a/checkdigit/parity.py +++ b/checkdigit/parity.py @@ -14,13 +14,17 @@ # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + def evenparity(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces if data.count("1") % 2 == 0: return data + "0" return data + "1" def oddparity(data): + data = str(data).replace('-', '').replace(' ', '') # Removes Hyphens and Spaces if data.count("1") % 2 == 0: return data + "1" return data + "0" diff --git a/checkdigit/upc.py b/checkdigit/upc.py index 8c5fee1..71fd3e0 100644 --- a/checkdigit/upc.py +++ b/checkdigit/upc.py @@ -15,9 +15,14 @@ from checkdigit import isbn +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + # ISBN calculations are very similar to that of UPC # The only major difference is that the ODD instead of even placed digits are multiplied by 3 +# Spaces and hyphens do not need to be removed +# This is since it's removed by the ISBN-13 Function + def upc_calculate(data): return isbn.isbn13calculate(data, "upc") diff --git a/tests/test_isbn.py b/tests/test_isbn.py index 675ee5c..705657e 100644 --- a/tests/test_isbn.py +++ b/tests/test_isbn.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + import sys import time from config import test @@ -30,7 +32,7 @@ # ISBN-10 Check digit test(isbn.isbn10calculate("006196436"), "0", "isbn10calculate (1)") -test(isbn.isbn10calculate("190592105"), "5", "isbn10calculate (2)") +test(isbn.isbn10calculate(190592105), "5", "isbn10calculate (2)") test(isbn.isbn10calculate("043942089"), "X", "isbn10calculate (3)") # Validate ISBN-10 @@ -41,9 +43,10 @@ # Validate ISBN-13 test(isbn.isbn13check("0123456789128"), True, "isbn13check (1)") -test(isbn.isbn13check("9781861978769"), True, "isbn13check (2)") +test(isbn.isbn13check(9781861978769), True, "isbn13check (2)") # ISBN-10 Missing Digit +# Must be string due to ? and/or X test(isbn.calculate_missing("15688?111X"), "1", "calculate_missing (10a)") test(isbn.calculate_missing("812071988?"), "3", "calculate_missing (10b)") test(isbn.calculate_missing("020161586?"), "X", "calculate_missing (10c)") diff --git a/tests/test_luhn.py b/tests/test_luhn.py index 07a4140..9c5716a 100644 --- a/tests/test_luhn.py +++ b/tests/test_luhn.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + import sys import time from config import test @@ -31,23 +33,23 @@ # Determine Check Digit test(luhn.luhn_calculate("53251309870224"), "3", "luhn_calculate(1)") test(luhn.luhn_calculate("601195843045086"), "9", "luhn_calculate(2)") -test(luhn.luhn_calculate("543669577125419"), "0", "luhn_calculate(3)") +test(luhn.luhn_calculate(543669577125419), "0", "luhn_calculate(3)") test(luhn.luhn_calculate("436076111511668"), "6", "luhn_calculate(4)") test(luhn.luhn_calculate("37266630277430"), "0", "luhn_calculate(5)") -test(luhn.luhn_calculate("91497796683515"), "3", "luhn_calculate(6)") +test(luhn.luhn_calculate(91497796683515), "3", "luhn_calculate(6)") test(luhn.luhn_calculate("10408772972296"), "9", "luhn_calculate(7)") # Validate Data -test(luhn.luhn_validate("541756116585277"), True, "luhn_validate(1)") +test(luhn.luhn_validate(541756116585277), True, "luhn_validate(1)") test(luhn.luhn_validate("526012730485489"), True, "luhn_validate(2)") test(luhn.luhn_validate("515853022176176"), True, "luhn_validate(3)") test(luhn.luhn_validate("6011365035868968"), True, "luhn_validate(4)") -test(luhn.luhn_validate("372098369216316"), True, "luhn_validate(5)") +test(luhn.luhn_validate(372098369216316), True, "luhn_validate(5)") test(luhn.luhn_validate("4556098986775827"), True, "luhn_validate(6)") test(luhn.luhn_validate("49927398717"), False, "luhn_validate(7)") test(luhn.luhn_validate("1234567812345678"), False, "luhn_validate(8)") test(luhn.luhn_validate("2222222222222222"), False, "luhn_validate(9)") -test(luhn.luhn_validate("111111111111111"), False, "luhn_validate(10)") +test(luhn.luhn_validate(111111111111111), False, "luhn_validate(10)") test(luhn.luhn_validate("33333333333333"), False, "luhn_validate(11)") finish_time = time.time() diff --git a/tests/test_parity.py b/tests/test_parity.py index 01efd68..e931d0e 100644 --- a/tests/test_parity.py +++ b/tests/test_parity.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + import sys import time from config import test diff --git a/tests/test_upc.py b/tests/test_upc.py index b2b10ee..89666f6 100644 --- a/tests/test_upc.py +++ b/tests/test_upc.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with checkdigit. If not, see . +# WARNING: Data beginning with 0 must be as a string due to PEP 3127 + import sys import time from config import test