예제 #1
0
def num_to_code(user_number, part_number):
    """
    Convert a user/part number combo to an alphanumeric asset code.

    :param user_number int: The user number.
    :param part_number int: The part number.
    :returns: An asset code string.
    :rtype: str
    """
    if user_number < 0 or part_number < 0:
        raise ValueError('User ({0}) or part ({1}) number cannot be '
                         'negative. '.format(user_number, part_number))

    assetno = ''
    for num in (user_number, part_number):
        while True:
            if num > 15:
                r = num % 16
                assetno = assetno + ALPHABET[r + 16]
                num = num // 16
            else:
                assetno = assetno + ALPHABET[num]
                break

    checkdigit = luhn.calc_check_digit(assetno, ALPHABET)

    assetno = assetno + checkdigit
    assert luhn.is_valid(assetno, ALPHABET)
    return assetno
예제 #2
0
def num_to_code(user_number, part_number):
    """
    Convert a user/part number combo to an alphanumeric asset code.

    :param user_number int: The user number.
    :param part_number int: The part number.
    :returns: An asset code string.
    :rtype: str
    """
    if user_number < 0 or part_number < 0:
        raise ValueError('User ({0}) or part ({1}) number cannot be '
                         'negative. '.format(user_number, part_number))

    assetno = ''
    for num in (user_number, part_number):
        while True:
            if num > 15:
                r = num % 16
                assetno = assetno + ALPHABET[r + 16]
                num = num // 16
            else:
                assetno = assetno + ALPHABET[num]
                break

    checkdigit = luhn.calc_check_digit(assetno, ALPHABET)

    assetno = assetno + checkdigit
    assert luhn.is_valid(assetno, ALPHABET)
    return assetno
예제 #3
0
파일: assetcode.py 프로젝트: srobo/tools
def code_to_num(asset_code):
    """
    Convert an alphanumeric asset code to a user/part number combo.

    :param str asset_code: The asset code to convert.s
    :returns: A tuple consisting of the user and part number.
    :rtype: pair of ints
    """
    asset_code = normalise(asset_code)
    if not luhn.is_valid(asset_code, ALPHABET):
        raise ValueError("Asset code '{}' is not valid".format(asset_code))

    # Remove checkdigit
    asset_code = asset_code[:-1]

    field = [0, 0]
    fieldno = 0
    i = 0
    for c in asset_code:
        if fieldno == 2:
            raise ValueError("Error in asset code '{}', too many fields".format(asset_code))
        num = ALPHABET.index(c)
        if num > 15:
            field[fieldno] = field[fieldno] + (num - 16) * (16 ** i)
        else:
            field[fieldno] = field[fieldno] + num * (16 ** i)
            fieldno += 1
            i = -1
        i += 1

    return (field[0], field[1])
예제 #4
0
파일: assetcode.py 프로젝트: srobo/tools
def is_valid(asset_code):
    """
    Check if an asset code is valid.

    :param str asset_code: The asset code to check.
    :returns: True if valid, else False.
    :rtype: bool
    """
    asset_code = normalise(asset_code)
    return luhn.is_valid(asset_code, ALPHABET)
예제 #5
0
def is_valid(asset_code):
    """
    Check if an asset code is valid.

    :param str asset_code: The asset code to check.
    :returns: True if valid, else False.
    :rtype: bool
    """
    asset_code = normalise(asset_code)

    invalid_characters = set(asset_code) - ALPHABET_SET
    if invalid_characters:
        return False

    return luhn.is_valid(asset_code, ALPHABET)
예제 #6
0
def is_valid(asset_code):
    """
    Check if an asset code is valid.

    :param str asset_code: The asset code to check.
    :returns: True if valid, else False.
    :rtype: bool
    """
    asset_code = normalise(asset_code)

    invalid_characters = set(asset_code) - ALPHABET_SET
    if invalid_characters:
        return False

    return luhn.is_valid(asset_code, ALPHABET)
예제 #7
0
 def test_invalid(self):
     for code in ['7894']:
         self.assertFalse(luhn.is_valid(code))
예제 #8
0
 def test_valid(self):
     for code in ['78949']:
         self.assertTrue(luhn.is_valid(code))