-
-
Notifications
You must be signed in to change notification settings - Fork 46k
/
barcode_validator.py
89 lines (73 loc) · 2.13 KB
/
barcode_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
https://en.wikipedia.org/wiki/Check_digit#Algorithms
"""
def 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]
"""
barcode //= 10 # exclude the last digit
checker = False
s = 0
# extract and check each digit
while barcode != 0:
mult = 1 if checker else 3
s += mult * (barcode % 10)
barcode //= 10
checker = not checker
return (10 - (s % 10)) % 10
def 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
"""
return len(str(barcode)) == 13 and get_check_digit(barcode) == barcode % 10
def 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.
"""
if str(barcode).isalpha():
msg = f"Barcode '{barcode}' has alphabetic characters."
raise ValueError(msg)
elif int(barcode) < 0:
raise ValueError("The entered barcode has a negative value. Try again.")
else:
return int(barcode)
if __name__ == "__main__":
import doctest
doctest.testmod()
"""
Enter a barcode.
"""
barcode = get_barcode(input("Barcode: ").strip())
if is_valid(barcode):
print(f"'{barcode}' is a valid barcode.")
else:
print(f"'{barcode}' is NOT a valid barcode.")