home_bin/misc/egn.py

42 lines
1.1 KiB
Python
Raw Normal View History

2011-12-02 06:26:12 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This program check if the given EGN (Universal Citizen Number)
is valid in Bulgaria. All EGNs are accepted as arguments.
2023-01-11 00:08:38 +02:00
You can also import and use the egn_check function.
2011-12-02 06:26:12 +02:00
"""
2023-01-11 00:08:38 +02:00
__version__ = "0.0.3"
2020-10-11 22:33:18 +03:00
__author__ = "Doncho Gunchev <gunchev@gmail.com>"
__depends__ = ["Python-3"]
2011-12-02 06:26:12 +02:00
__copyright__ = "GPLv2+/BSD"
2020-10-11 22:33:18 +03:00
2023-01-11 00:08:38 +02:00
def egn_check(egn) -> bool:
"""Check if the given EGN (Bulgarian Universal Citizen Number) is valid."""
2011-12-02 06:26:12 +02:00
if len(egn) != 10:
return False
multipliers = (2, 4, 8, 5, 10, 9, 7, 3, 6)
check_sum = 0
for i in range(9):
check_sum += int(egn[i]) * multipliers[i]
check_sum %= 11
if check_sum > 9:
2020-10-11 22:33:18 +03:00
check_sum = 0 # hmm? should it not just subtract 10?
2011-12-02 06:26:12 +02:00
return int(egn[9]) == check_sum
if __name__ == '__main__':
import sys
2023-01-11 00:08:38 +02:00
print(f"EGN check version {__version__}, by {__author__}")
2011-12-02 06:26:12 +02:00
if len(sys.argv) < 2:
2023-01-11 00:08:38 +02:00
print(f"\n{__doc__.strip()}")
2011-12-02 06:26:12 +02:00
else:
for arg in sys.argv[1:]:
if egn_check(arg):
2023-01-11 00:08:38 +02:00
print(f'{arg} - OK')
2011-12-02 06:26:12 +02:00
else:
2023-01-11 00:08:38 +02:00
print(f'{arg} - BAD')