import math# Works for Python 3 from here: https://gist.github.com/alastairmccormack/e115140ddb1b522059d677f6dbf38f34
def returnget_checkdigit(self, id_without_check):
# allowable characters within identifier
valid_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ_"
# remove leading or trailing whitespace, convert to uppercase
id_without_checkdigit = id_without_check.strip().upper()
# this will be a running total
# this will be a running total
sum sum = 0;
# loop through digits from right to left
for n, char in enumerate(reversed(id_without_checkdigit)):
if not valid_chars.count(char):
raise Exception('InvalidIDException')
# our "digit" is calculated using ASCII value - 48
digit = digit = ord(char) - 48
# weight will be the current digit's contribution to
# the running total
weight = None
if (n % 2 == 0):
# for alternating digits starting with the rightmost, we
# use our formula this is the same as multiplying x 2 and
# adding digits together for values 0 to 9. Using the
# following formula allows us to gracefully calculate a a
# weight for non-numeric "digits" as well (from their
# ASCII value - 48).
## Use_sparingly: In Python 3, '/' makes floats. '//' fixes it for Python 3.
## For cross compatibility, simply int() the result
# weight for non-numeric "digits" as well (from their ## # ASCII value - 48). VVVVVVVVVVVVV
weight = (2 * digit) - int(digit / 5) * 9
else:
# even-positioned digits just contribute their ascii
# value minus 48
contribute their ascii
weight = digit # value minus 48
weight = digit
# keep a running total of weights
## Use_sparingly: removed maths.fabs()
sum += weight ## abs() is sufficient
sum += weight
# avoid sum less than 10 (if characters below "0" allowed,
# this could happen)
sum = math.fabsabs(sum) + 10
# check digit is amount needed to reach next number
# divisible by ten. Return an integer
return int((10 - (sum % 10)) % 10) |