Skip to content

Instantly share code, notes, and snippets.

@vstoykov
Created December 20, 2012 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vstoykov/4344506 to your computer and use it in GitHub Desktop.
Save vstoykov/4344506 to your computer and use it in GitHub Desktop.
Method for checking validity of bulgarian EIK/BULSTAT codes.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# eik_checker.py
#
# Copyright 2012 Venelin Stoykov <venelin@magicbg.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
def eik_checker(eik):
"""
Check Bulgarian EIK/BULSTAT codes for validity
full information about algoritm is available here
http://bulstat.registryagency.bg/About.html
"""
def get_checksum(weights, digits):
checksum = sum([weight * digit for weight, digit in zip(weights, digits)])
return checksum % 11
def check_eik_base(eik):
checksum = get_checksum(range(1, 9), eik)
if checksum == 10:
checksum = get_checksum(range(3, 11), eik)
return eik[-1] == checksum % 10
def check_eik_extra(eik):
digits = eik[9:13]
checksum = get_checksum([2, 7, 3, 5], digits)
if checksum == 10:
checksum = get_checksum([4, 9, 5, 7], digits)
return digits[-1] == checksum % 10
try:
eik = map(int, eik)
except ValueError:
return False
if not (len(eik) in [9, 13] and check_eik_base(eik)):
return False
if len(eik) == 13 and not check_eik_extra(eik):
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment