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,24 @@
# __init__.py - collection of Spanish 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 Spanish numbers."""
# provide vat as an alias
from stdnum.es import nif as vat

View file

@ -0,0 +1,108 @@
# cif.py - functions for handling Spanish fiscal 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
"""CIF (Certificado de Identificación Fiscal, Spanish company tax number).
The CIF is a tax identification number for legal entities. It has 9 digits
where the first digit is a letter (denoting the type of entity) and the
last is a check digit (which may also be a letter).
>>> validate('J99216582')
'J99216582'
>>> validate('J99216583') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('J992165831') # too long
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('M-1234567-L')
'M1234567L'
>>> validate('O-1234567-L') # invalid first character
Traceback (most recent call last):
...
InvalidFormat: ...
>>> split('A13 585 625')
('A', '13', '58562', '5')
"""
from stdnum import luhn
from stdnum.es import dni
from stdnum.exceptions import *
__all__ = ['compact', 'validate', 'is_valid', 'split']
# use the same compact function as DNI
compact = dni.compact
def calc_check_digits(number):
"""Calculate the check digits for the specified number. The number
passed should not have the check digit included. This function returns
both the number and character check digit candidates."""
check = luhn.calc_check_digit(number[1:])
return check + 'JABCDEFGHI'[int(check)]
def validate(number):
"""Checks to see if the number provided is a valid DNI number. This
checks the length, formatting and check digit."""
number = compact(number)
if not number[1:-1].isdigit():
raise InvalidFormat()
if len(number) != 9:
raise InvalidLength()
if number[0] in 'KLM':
# K: Spanish younger than 14 year old
# L: Spanish living outside Spain without DNI
# M: granted the tax to foreigners who have no NIE
# these use the old checkdigit algorithm (the DNI one)
if number[-1] != dni.calc_check_digit(number[1:-1]):
raise InvalidChecksum()
elif number[0] in 'ABCDEFGHJNPQRSUVW':
# there seems to be conflicting information on which organisation types
# should have which type of check digit (alphabetic or numeric) so
# we support either here
if number[-1] not in calc_check_digits(number[:-1]):
raise InvalidChecksum()
else:
# anything else is invalid
raise InvalidFormat()
return number
def is_valid(number):
"""Checks to see if the number provided is a valid DNI number. This
checks the length, formatting and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False
def split(number):
"""Split the provided number into a letter to define the type of
organisation, two digits that specify a province, a 5 digit sequence
number within the province and a check digit."""
number = compact(number)
return number[0], number[1:3], number[3:8], number[8:]

View file

@ -0,0 +1,76 @@
# dni.py - functions for handling Spanish personal identity codes
# 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
"""DNI (Documento nacional de identidad, Spanish personal identity codes).
The DNI is a 9 digit number used to identify Spanish citizens. The last
digit is a checksum letter.
Foreign nationals, since 2010 are issued an NIE (Número de Identificación
de Extranjeros, Foreigner's Identity Number) instead.
>>> validate('54362315-K')
'54362315K'
>>> validate('54362315Z') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('54362315') # digit missing
Traceback (most recent call last):
...
InvalidLength: ...
"""
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."""
return 'TRWAGMYFPDXBNJZSQVHLCKE'[int(number) % 23]
def validate(number):
"""Checks to see if the number provided is a valid DNI number. This
checks the length, formatting and check digit."""
number = compact(number)
if not number[:-1].isdigit():
raise InvalidFormat()
if len(number) != 9:
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 DNI number. This
checks the length, formatting and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,77 @@
# nie.py - functions for handling Spanish foreigner identity codes
# 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
"""NIE (Número de Identificación de Extranjeros, Spanish foreigner number).
The NIE is an identification number for foreigners. It is a 9 digit number
where the first digit is either X, Y or Z and last digit is a checksum
letter.
>>> validate('x-2482300w')
'X2482300W'
>>> validate('x-2482300a') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('X2482300') # digit missing
Traceback (most recent call last):
...
InvalidLength: ...
"""
from stdnum.es import dni
from stdnum.exceptions import *
__all__ = ['compact', 'is_valid']
# use the same compact function as DNI
compact = dni.compact
def calc_check_digit(number):
"""Calculate the check digit. The number passed should not have the
check digit included."""
# replace XYZ with 012
number = str('XYZ'.index(number[0])) + number[1:]
return dni.calc_check_digit(number)
def validate(number):
"""Checks to see if the number provided is a valid NIE. This checks
the length, formatting and check digit."""
number = compact(number)
if not number[1:-1].isdigit() or number[:1] not in 'XYZ':
raise InvalidFormat()
if len(number) != 9:
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 NIE. This checks
the length, formatting and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,85 @@
# nif.py - functions for handling Spanish NIF (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
"""NIF (Número de Identificación Fiscal, Spanish VAT number).
The Spanish VAT number is a 9-digit number where either the first, last
digits or both can be letters.
The number is either a DNI (Documento nacional de identidad, for
Spaniards), a NIE (Número de Identificación de Extranjeros, for
foreigners) or a CIF (Certificado de Identificación Fiscal, for legal
entities and others).
>>> compact('ES B-58378431')
'B58378431'
>>> validate('B64717838')
'B64717838'
>>> validate('B64717839') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('54362315K') # resident
'54362315K'
>>> validate('X-5253868-R') # foreign person
'X5253868R'
"""
from stdnum.es import dni, nie, cif
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('ES'):
number = number[2:]
return number
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[1:-1].isdigit():
raise InvalidFormat()
if len(number) != 9:
raise InvalidLength()
if number[0].isdigit():
# natural resident
dni.validate(number)
elif number[0] in 'XYZ':
# foreign natural person
nie.validate(number)
else:
# otherwise it has to be a valid CIF
cif.validate(number)
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