Switch to python3
This commit is contained in:
parent
531041e89a
commit
9ba4b6a91a
5286 changed files with 677347 additions and 576888 deletions
|
|
@ -0,0 +1,30 @@
|
|||
# __init__.py - functions for performing the ISO 7064 algorithms
|
||||
#
|
||||
# Copyright (C) 2010, 2012 Arthur de Jong
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library 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
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
|
||||
"""Collection of the ISO 7064 algorithms.
|
||||
|
||||
This package provides a number of modules for calculation and verification
|
||||
of numbers using one of the ISO 7064 algorithms.
|
||||
|
||||
Note that these functions were not implemented using the ISO text itself
|
||||
because the text is not available for free. These functions were
|
||||
implemented based on information on the algorithms found online and some
|
||||
reverse engineering. If anyone can provide a legal copy of the ISO/IEC
|
||||
7064 standard these functions can be validated and perhaps improved.
|
||||
"""
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
# mod_11_10.py - functions for performing the ISO 7064 Mod 11, 10 algorithm
|
||||
#
|
||||
# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library 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
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
|
||||
"""The ISO 7064 Mod 11, 10 algorithm.
|
||||
|
||||
The Mod 11, 10 algorithm uses a number of calculations modulo 11 and 10 to
|
||||
determine a checksum.
|
||||
|
||||
For a module that can do generic Mod x+1, x calculations see the
|
||||
:mod:`stdnum.iso7064.mod_37_36` module.
|
||||
|
||||
>>> calc_check_digit('79462')
|
||||
'3'
|
||||
>>> validate('794623')
|
||||
'794623'
|
||||
>>> calc_check_digit('00200667308')
|
||||
'5'
|
||||
>>> validate('002006673085')
|
||||
'002006673085'
|
||||
"""
|
||||
|
||||
from stdnum.exceptions import *
|
||||
|
||||
|
||||
def checksum(number):
|
||||
"""Calculate the checksum. A valid number should have a checksum of 1."""
|
||||
check = 5
|
||||
for n in number:
|
||||
check = (((check or 10) * 2) % 11 + int(n)) % 10
|
||||
return check
|
||||
|
||||
|
||||
def calc_check_digit(number):
|
||||
"""With the provided number, calculate the extra digit that should be
|
||||
appended to make it a valid number."""
|
||||
return str((1 - ((checksum(number) or 10) * 2) % 11) % 10)
|
||||
|
||||
|
||||
def validate(number):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
valid = checksum(number) == 1
|
||||
except:
|
||||
raise InvalidFormat()
|
||||
if not valid:
|
||||
raise InvalidChecksum()
|
||||
return number
|
||||
|
||||
|
||||
def is_valid(number):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
return bool(validate(number))
|
||||
except ValidationError:
|
||||
return False
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
# mod_11_2.py - functions for performing the ISO 7064 Mod 11, 2 algorithm
|
||||
#
|
||||
# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library 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
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
|
||||
"""The ISO 7064 Mod 11, 2 algorithm.
|
||||
|
||||
The Mod 11, 2 algorithm is a simple module 11 checksum where the check
|
||||
digit can be an X to make the number valid.
|
||||
|
||||
For a module that can do generic Mod x, 2 calculations see the
|
||||
:mod:`stdnum.iso7064.mod_37_2` module.
|
||||
|
||||
>>> calc_check_digit('0794')
|
||||
'0'
|
||||
>>> validate('07940')
|
||||
'07940'
|
||||
>>> calc_check_digit('079')
|
||||
'X'
|
||||
>>> validate('079X')
|
||||
'079X'
|
||||
>>> checksum('079X')
|
||||
1
|
||||
"""
|
||||
|
||||
from stdnum.exceptions import *
|
||||
|
||||
|
||||
def checksum(number):
|
||||
"""Calculate the checksum. A valid number should have a checksum of 1."""
|
||||
check = 0
|
||||
for n in number:
|
||||
check = (2 * check + int(10 if n == 'X' else n)) % 11
|
||||
return check
|
||||
|
||||
|
||||
def calc_check_digit(number):
|
||||
"""With the provided number, calculate the extra digit that should be
|
||||
appended to make it a valid number."""
|
||||
c = (1 - 2 * checksum(number)) % 11
|
||||
return 'X' if c == 10 else str(c)
|
||||
|
||||
|
||||
def validate(number):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
valid = checksum(number) == 1
|
||||
except:
|
||||
raise InvalidFormat()
|
||||
if not valid:
|
||||
raise InvalidChecksum()
|
||||
return number
|
||||
|
||||
|
||||
def is_valid(number):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
return bool(validate(number))
|
||||
except ValidationError:
|
||||
return False
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
# mod_37_2.py - functions for performing the ISO 7064 Mod 37, 2 algorithm
|
||||
#
|
||||
# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library 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
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
|
||||
"""The ISO 7064 Mod 37, 2 algorithm.
|
||||
|
||||
The Mod 37, 2 checksum can be used for alphanumeric numbers and the check
|
||||
digit may also be numeric, a letter or '*'.
|
||||
|
||||
>>> calc_check_digit('G123489654321')
|
||||
'Y'
|
||||
>>> validate('G123489654321Y')
|
||||
'G123489654321Y'
|
||||
>>> checksum('G123489654321Y')
|
||||
1
|
||||
|
||||
By changing the alphabet this can be turned into any Mod x, 2
|
||||
algorithm. For example Mod 11, 2:
|
||||
|
||||
>>> calc_check_digit('079', alphabet='0123456789X')
|
||||
'X'
|
||||
>>> validate('079X', alphabet='0123456789X')
|
||||
'079X'
|
||||
>>> checksum('079X', alphabet='0123456789X')
|
||||
1
|
||||
"""
|
||||
|
||||
from stdnum.exceptions import *
|
||||
|
||||
|
||||
def checksum(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*'):
|
||||
"""Calculate the checksum. A valid number should have a checksum of 1."""
|
||||
modulus = len(alphabet)
|
||||
check = 0
|
||||
for n in number:
|
||||
check = (2 * check + alphabet.index(n)) % modulus
|
||||
return check
|
||||
|
||||
|
||||
def calc_check_digit(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*'):
|
||||
"""With the provided number, calculate the extra digit that should be
|
||||
appended to make it a valid number."""
|
||||
modulus = len(alphabet)
|
||||
return alphabet[(1 - 2 * checksum(number, alphabet)) % modulus]
|
||||
|
||||
|
||||
def validate(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*'):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
valid = checksum(number, alphabet) == 1
|
||||
except:
|
||||
raise InvalidFormat()
|
||||
if not valid:
|
||||
raise InvalidChecksum()
|
||||
return number
|
||||
|
||||
|
||||
def is_valid(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*'):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
return bool(validate(number, alphabet))
|
||||
except ValidationError:
|
||||
return False
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
# mod_37_36.py - functions for performing the ISO 7064 Mod 37, 36 algorithm
|
||||
#
|
||||
# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library 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
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
|
||||
"""The ISO 7064 Mod 37, 36 algorithm.
|
||||
|
||||
The Mod 37, 36 algorithm uses an alphanumeric check digit and the number
|
||||
itself may also contain letters.
|
||||
|
||||
>>> checksum('A12425GABC1234002M')
|
||||
1
|
||||
>>> calc_check_digit('A12425GABC1234002')
|
||||
'M'
|
||||
>>> validate('A12425GABC1234002M')
|
||||
'A12425GABC1234002M'
|
||||
|
||||
By changing the alphabet this can be turned into any Mod x+1, x
|
||||
algorithm. For example Mod 11, 10:
|
||||
|
||||
>>> calc_check_digit('00200667308', alphabet='0123456789')
|
||||
'5'
|
||||
>>> validate('002006673085', alphabet='0123456789')
|
||||
'002006673085'
|
||||
"""
|
||||
|
||||
from stdnum.exceptions import *
|
||||
|
||||
|
||||
def checksum(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
|
||||
"""Calculate the checksum. A valid number should have a checksum of 1."""
|
||||
modulus = len(alphabet)
|
||||
check = modulus // 2
|
||||
for n in number:
|
||||
check = (((check or modulus) * 2) % (modulus + 1) + alphabet.index(n)) % modulus
|
||||
return check
|
||||
|
||||
|
||||
def calc_check_digit(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
|
||||
"""With the provided number, calculate the extra digit that should be
|
||||
appended to make it a valid number."""
|
||||
modulus = len(alphabet)
|
||||
return alphabet[(1 - ((checksum(number, alphabet) or modulus) * 2) % (modulus + 1)) % modulus]
|
||||
|
||||
|
||||
def validate(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
valid = checksum(number, alphabet) == 1
|
||||
except:
|
||||
raise InvalidFormat()
|
||||
if not valid:
|
||||
raise InvalidChecksum()
|
||||
return number
|
||||
|
||||
|
||||
def is_valid(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
return bool(validate(number, alphabet))
|
||||
except ValidationError:
|
||||
return False
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
# mod_97_10.py - functions for performing the ISO 7064 Mod 97, 10 algorithm
|
||||
#
|
||||
# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library 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
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
|
||||
"""The ISO 7064 Mod 97, 10 algorithm.
|
||||
|
||||
The Mod 97, 10 algorithm evaluates the whole number as an integer which is
|
||||
valid if the number modulo 97 is 1. As such it has two check digits.
|
||||
|
||||
>>> calc_check_digits('99991234567890121414')
|
||||
'90'
|
||||
>>> validate('9999123456789012141490')
|
||||
'9999123456789012141490'
|
||||
>>> calc_check_digits('4354111611551114')
|
||||
'31'
|
||||
>>> validate('08686001256515001121751')
|
||||
'08686001256515001121751'
|
||||
>>> calc_check_digits('22181321402534321446701611')
|
||||
'35'
|
||||
"""
|
||||
|
||||
from stdnum.exceptions import *
|
||||
|
||||
|
||||
def checksum(number):
|
||||
"""Calculate the checksum. A valid number should have a checksum of 1."""
|
||||
return int(number) % 97
|
||||
|
||||
|
||||
def calc_check_digits(number):
|
||||
"""With the provided number, calculate the extra digits that should be
|
||||
appended to make it a valid number."""
|
||||
return '%02d' % ((98 - 100 * checksum(number)) % 97)
|
||||
|
||||
|
||||
def validate(number):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
valid = checksum(number) == 1
|
||||
except:
|
||||
raise InvalidFormat()
|
||||
if not valid:
|
||||
raise InvalidChecksum()
|
||||
return number
|
||||
|
||||
|
||||
def is_valid(number):
|
||||
"""Checks whether the check digit is valid."""
|
||||
try:
|
||||
return bool(validate(number))
|
||||
except ValidationError:
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue