Example #1
0
def validate(number, strip_check_digits=False, add_check_digits=False):
    """Check if the number provided is a valid ISAN. If check digits are
    present in the number they are validated. If strip_check_digits is True
    any existing check digits will be removed (after checking). If
    add_check_digits is True the check digit will be added if they are not
    present yet."""
    (root, episode, check1, version, check2) = split(number)
    # check digits used
    for x in root + episode + version:
        if x not in '0123456789ABCDEF':
            raise InvalidFormat()
    # check length of all components
    if len(root) != 12 or len(episode) != 4 or len(check1) not in (0, 1) or \
       len(version) not in (0, 8) or len(check1) not in (0, 1):
        raise InvalidLength()
    # allow removing check digits
    if strip_check_digits:
        check1 = check2 = ''
    # check check digits
    if check1:
        mod_37_36.validate(root + episode + check1)
    if check2:
        mod_37_36.validate(root + episode + version + check2)
    # add check digits
    if add_check_digits and not check1:
        check1 = mod_37_36.calc_check_digit(root + episode)
    if add_check_digits and not check2 and version:
        check2 = mod_37_36.calc_check_digit(root + episode + version)
    return root + episode + check1 + version + check2
Example #2
0
def validate(number, strip_check_digits=False, add_check_digits=False):
    """Checks to see if the number provided is a valid ISAN. If check digits
    are present in the number they are validated. If strip_check_digits is
    True any existing check digits will be removed (after checking). If
    add_check_digits is True the check digit will be added if they are not
    present yet."""
    (root, episode, check1, version, check2) = split(number)
    # check digits used
    for x in root + episode + version:
        if x not in '0123456789ABCDEF':
            raise InvalidFormat()
    # check length of all components
    if len(root) != 12 or len(episode) != 4 or len(check1) not in (0, 1) or \
       len(version) not in (0, 8) or len(check1) not in (0, 1):
        raise InvalidLength()
    # check check digits
    if check1:
        mod_37_36.validate(root + episode + check1)
    if check2:
        mod_37_36.validate(root + episode + version + check2)
    # remove or add check digits
    if strip_check_digits:
        check1 = check2 = ''
    if add_check_digits and not check1:
        check1 = mod_37_36.calc_check_digit(root + episode)
    if add_check_digits and not check2 and version:
        check2 = mod_37_36.calc_check_digit(root + episode + version)
    return root + episode + check1 + version + check2
Example #3
0
def validate(number):
    """Check if the number is a valid GRid."""
    from stdnum.iso7064 import mod_37_36
    number = compact(number)
    if len(number) != 18:
        raise InvalidLength()
    return mod_37_36.validate(number)
 def test_mod37_36(self):
     sequence = self.get_sequence("ISO7064_37_36")
     self.assertTrue(mod_37_36.validate(sequence.next_by_id()))