Switch to python3

This commit is contained in:
j 2014-09-30 18:15:32 +02:00
commit 9ba4b6a91a
5286 changed files with 677347 additions and 576888 deletions

View file

@ -0,0 +1,21 @@
# __init__.py - collection of Bulgarian numbers
# coding: utf-8
#
# Copyright (C) 2012 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""Collection of Bulgarian numbers."""

View file

@ -0,0 +1,105 @@
# egn.py - functions for handling Bulgarian national identification numbers
# coding: utf-8
#
# Copyright (C) 2012, 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""EGN (ЕГН, Единен граждански номер, Bulgarian personal identity codes).
It is a 10-digit number of which the first 6 digits denote the person's
birth date, the next three digits represent a birth order number from
which the person's gender can be determined and the last digit is a check
digit.
>>> compact('752316 926 3')
'7523169263'
>>> validate('8032056031')
'8032056031'
>>> get_birth_date('7542011030')
datetime.date(2075, 2, 1)
>>> validate('7552A10004') # invalid digit
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('8019010008') # invalid date
Traceback (most recent call last):
...
InvalidComponent: ...
"""
import datetime
from stdnum.exceptions import *
from stdnum.util import clean
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, ' -.').upper().strip()
def calc_check_digit(number):
"""Calculate the check digit. The number passed should not have the
check digit included."""
weights = (2, 4, 8, 5, 10, 9, 7, 3, 6)
return str(sum(weights[i] * int(n) for i, n in enumerate(number)) % 11 % 10)
def get_birth_date(number):
"""Split the date parts from the number and return the birth date."""
year = int(number[0:2]) + 1900
month = int(number[2:4])
day = int(number[4:6])
if month > 40:
year += 100
month -= 40
elif month > 20:
year -= 100
month -= 20
try:
return datetime.date(year, month, day)
except ValueError:
raise InvalidComponent()
def validate(number):
"""Checks to see if the number provided is a valid national
identification number. This checks the length, formatting, embedded
date and check digit."""
number = compact(number)
if not number.isdigit():
raise InvalidFormat()
if len(number) != 10:
raise InvalidLength()
# check if birth date is valid
birth_date = get_birth_date(number)
# TODO: check that the birth date is not in the future
# check the check digit
if calc_check_digit(number[:-1]) != number[-1]:
raise InvalidChecksum()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid national
identification number. This checks the length, formatting, embedded
date and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,76 @@
# pnf.py - functions for handling Bulgarian personal number of a foreigner
# coding: utf-8
#
# Copyright (C) 2012, 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""PNF (ЛНЧ, Личен номер на чужденец, Bulgarian number of a foreigner).
The personal number of a foreigner is a 10-digit number where the last digit
is the result of a weighted checksum.
>>> validate('7111 042 925')
'7111042925'
>>> validate('7111042922') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('71110A2922') # invalid digit
Traceback (most recent call last):
...
InvalidFormat: ...
"""
from stdnum.exceptions import *
from stdnum.util import clean
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, ' -.').upper().strip()
def calc_check_digit(number):
"""Calculate the check digit. The number passed should not have the
check digit included."""
weights = (21, 19, 17, 13, 11, 9, 7, 3, 1)
return str(sum(weights[i] * int(n) for i, n in enumerate(number)) % 10)
def validate(number):
"""Checks to see if the number provided is a valid national
identification number. This checks the length, formatting, embedded
date and check digit."""
number = compact(number)
if not number.isdigit():
raise InvalidFormat()
if len(number) != 10:
raise InvalidLength()
if calc_check_digit(number[:-1]) != number[-1]:
raise InvalidChecksum()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid national
identification number. This checks the length, formatting, embedded
date and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,93 @@
# vat.py - functions for handling Bulgarian VAT numbers
# coding: utf-8
#
# Copyright (C) 2012, 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""VAT (Идентификационен номер по ДДС, Bulgarian VAT number).
The Bulgarian VAT (Данък върху добавената стойност) number is either 9
(for legal entities) or 10 digits (for physical persons, foreigners and
others) long. Each type of number has it's own check digit algorithm.
>>> compact('BG 175 074 752')
'175074752'
>>> validate('175074752')
'175074752'
>>> validate('175074751') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
"""
from stdnum.bg import egn, pnf
from stdnum.exceptions import *
from stdnum.util import clean
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
number = clean(number, ' -.').upper().strip()
if number.startswith('BG'):
number = number[2:]
return number
def calc_check_digit_legal(number):
"""Calculate the check digit for legal entities. The number passed
should not have the check digit included."""
check = sum((i + 1) * int(n) for i, n in enumerate(number)) % 11
if check == 10:
check = sum((i + 3) * int(n) for i, n in enumerate(number)) % 11
return str(check % 10)
def calc_check_digit_other(number):
"""Calculate the check digit for others. The number passed should not
have the check digit included."""
weights = (4, 3, 2, 7, 6, 5, 4, 3, 2)
return str((11 - sum(weights[i] * int(n) for i, n in enumerate(number))) % 11)
def validate(number):
"""Checks to see if the number provided is a valid VAT number. This
checks the length, formatting and check digit."""
number = compact(number)
if not number.isdigit():
raise InvalidFormat()
if len(number) == 9:
# 9 digit numbers are for legal entities
if number[-1] != calc_check_digit_legal(number[:-1]):
raise InvalidChecksum()
elif len(number) == 10:
# 10 digit numbers are for physical persons, foreigners and others
if not egn.is_valid(number) and not pnf.is_valid(number) and \
number[-1] != calc_check_digit_other(number[:-1]):
raise InvalidChecksum()
else:
raise InvalidLength()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid VAT number. This
checks the length, formatting and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False