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.
2020-10-11 22:33:18 +03:00
You can also import egn_check from this file.
2011-12-02 06:26:12 +02:00
"""
2020-10-11 22:33:18 +03:00
__version__ = "0.0.2"
__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
2011-12-02 06:26:12 +02:00
def egn_check(egn):
'''Check if the given EGN (Bulgarian Universal Citizen Number) is valid.'''
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
2020-10-11 22:33:18 +03:00
print("EGN check version " + __version__ + ", by Mr.700")
2011-12-02 06:26:12 +02:00
if len(sys.argv) < 2:
2020-10-11 22:33:18 +03:00
print("\n" + __doc__.strip())
2011-12-02 06:26:12 +02:00
else:
for arg in sys.argv[1:]:
if egn_check(arg):
2020-10-11 22:33:18 +03:00
print(arg + " - OK")
2011-12-02 06:26:12 +02:00
else:
2020-10-11 22:33:18 +03:00
print(arg + " - BAD")