Server IP : 127.0.0.2 / Your IP : 13.58.199.13 Web Server : Apache/2.4.18 (Ubuntu) System : User : www-data ( ) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : disk_free_space,disk_total_space,diskfreespace,dl,exec,fpaththru,getmyuid,getmypid,highlight_file,ignore_user_abord,leak,listen,link,opcache_get_configuration,opcache_get_status,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,php_uname,phpinfo,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,pclose,popen,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,shell_exec,source,show_source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/lib/python2.7/dist-packages/stdnum/es/ |
Upload File : |
# 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:]