Skip to content

Commit

Permalink
Support for Integers
Browse files Browse the repository at this point in the history
With the exception of ints beginning with 0, due to PEP 3127
  • Loading branch information
harens committed Jan 25, 2019
1 parent ccd63c7 commit 87f5898
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
6 changes: 6 additions & 0 deletions checkdigit/isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

# 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:
Expand All @@ -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:
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions checkdigit/luhn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.


# 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
Expand All @@ -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
4 changes: 4 additions & 0 deletions checkdigit/parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.


# 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"
5 changes: 5 additions & 0 deletions checkdigit/upc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions tests/test_isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

# WARNING: Data beginning with 0 must be as a string due to PEP 3127

import sys
import time
from config import test
Expand All @@ -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
Expand All @@ -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)")
Expand Down
12 changes: 7 additions & 5 deletions tests/test_luhn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

# WARNING: Data beginning with 0 must be as a string due to PEP 3127

import sys
import time
from config import test
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions tests/test_parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

# WARNING: Data beginning with 0 must be as a string due to PEP 3127

import sys
import time
from config import test
Expand Down
2 changes: 2 additions & 0 deletions tests/test_upc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU General Public License
# along with checkdigit. If not, see <http://www.gnu.org/licenses/>.

# WARNING: Data beginning with 0 must be as a string due to PEP 3127

import sys
import time
from config import test
Expand Down

0 comments on commit 87f5898

Please sign in to comment.