Skip to content

biip.checksums

Checksum algorithms used by GS1 standards.

gs1_price_weight_check_digit

gs1_price_weight_check_digit(value: str) -> int

Get GS1 check digit for a price or weight field.

Parameters:

  • value (str) –

    The numeric string to calculate the check digit for.

Returns:

  • int

    The check digit.

Raises:

References

GS1 General Specification, section 7.9.2-7.9.4

Examples:

>>> from biip.checksums import gs1_price_weight_check_digit
>>> gs1_price_weight_check_digit("2875")
9
>>> gs1_price_weight_check_digit("14685")
6
Source code in src/biip/checksums.py
def gs1_price_weight_check_digit(value: str) -> int:
    """Get GS1 check digit for a price or weight field.

    Args:
        value: The numeric string to calculate the check digit for.

    Returns:
        The check digit.

    Raises:
        ValueError: If the value isn't numeric.

    References:
        GS1 General Specification, section 7.9.2-7.9.4

    Examples:
        >>> from biip.checksums import gs1_price_weight_check_digit
        >>> gs1_price_weight_check_digit("2875")
        9
        >>> gs1_price_weight_check_digit("14685")
        6
    """
    if not value.isdecimal():
        msg = f"Expected numeric value, got {value!r}."
        raise ValueError(msg)

    if len(value) == 4:
        return _four_digit_price_weight_check_digit(value)

    if len(value) == 5:
        return _five_digit_price_weight_check_digit(value)

    msg = f"Expected input of length 4 or 5, got {value!r}."
    raise ValueError(msg)

gs1_standard_check_digit

gs1_standard_check_digit(value: str) -> int

Get GS1 check digit for numeric string.

Parameters:

  • value (str) –

    The numeric string to calculate the check digit for.

Returns:

  • int

    The check digit.

Raises:

References

GS1 General Specification, section 7.9.1

Examples:

>>> from biip.checksums import gs1_standard_check_digit
>>> gs1_standard_check_digit("950110153100")  # GTIN-13
0
>>> gs1_standard_check_digit("9501234")  # GTIN-8
6
Source code in src/biip/checksums.py
def gs1_standard_check_digit(value: str) -> int:
    """Get GS1 check digit for numeric string.

    Args:
        value: The numeric string to calculate the check digit for.

    Returns:
        The check digit.

    Raises:
        ValueError: If the value isn't numeric.

    References:
        GS1 General Specification, section 7.9.1

    Examples:
        >>> from biip.checksums import gs1_standard_check_digit
        >>> gs1_standard_check_digit("950110153100")  # GTIN-13
        0
        >>> gs1_standard_check_digit("9501234")  # GTIN-8
        6
    """
    if not value.isdecimal():
        msg = f"Expected numeric value, got {value!r}."
        raise ValueError(msg)

    digits = list(map(int, list(value)))
    reversed_digits = reversed(digits)

    weighted_sum = 0
    for digit, weight in zip(reversed_digits, itertools.cycle([3, 1])):
        weighted_sum += digit * weight

    return (10 - weighted_sum % 10) % 10