strings.barcode_validator

https://en.wikipedia.org/wiki/Check_digit#Algorithms

Attributes

barcode

Functions

get_barcode(→ int)

Returns the barcode as an integer

get_check_digit(→ int)

Returns the last digit of barcode by excluding the last digit first

is_valid(→ bool)

Checks for length of barcode and last-digit

Module Contents

strings.barcode_validator.get_barcode(barcode: str) int

Returns the barcode as an integer

>>> get_barcode("8718452538119")
8718452538119
>>> get_barcode("dwefgiweuf")
Traceback (most recent call last):
    ...
ValueError: Barcode 'dwefgiweuf' has alphabetic characters.
strings.barcode_validator.get_check_digit(barcode: int) int

Returns the last digit of barcode by excluding the last digit first and then computing to reach the actual last digit from the remaining 12 digits.

>>> get_check_digit(8718452538119)
9
>>> get_check_digit(87184523)
5
>>> get_check_digit(87193425381086)
9
>>> [get_check_digit(x) for x in range(0, 100, 10)]
[0, 7, 4, 1, 8, 5, 2, 9, 6, 3]
strings.barcode_validator.is_valid(barcode: int) bool

Checks for length of barcode and last-digit Returns boolean value of validity of barcode

>>> is_valid(8718452538119)
True
>>> is_valid(87184525)
False
>>> is_valid(87193425381089)
False
>>> is_valid(0)
False
>>> is_valid(dwefgiweuf)
Traceback (most recent call last):
    ...
NameError: name 'dwefgiweuf' is not defined
strings.barcode_validator.barcode