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/fi/ |
Upload File : |
# associationid.py - functions for handling Finnish association registry id # coding: utf-8 # # Copyright (C) 2015 Holvi Payment Services Oy # Copyright (C) 2015 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 """Finnish Association Identifier. The number consists of 1 to 6 digits that are normally separated with a dot in groups of 0-3 and 0-3 numbers. E.g. 123.123, 12.123, 1.123, 123 or 1. >>> validate('123.123') '123123' >>> validate('1123') '1123' >>> validate('123123123') Traceback (most recent call last): ... stdnum.exceptions.InvalidLength: The number has an invalid length. >>> validate('12df') Traceback (most recent call last): ... stdnum.exceptions.InvalidFormat: The number has an invalid format. >>> format('123') '123' >>> format('1234') '1.234' """ from stdnum.exceptions import * from stdnum.util import clean # a collection of all registered numbers with 2 or less digits _lownumbers = set(( 1, 6, 7, 9, 12, 14, 15, 16, 18, 22, 23, 24, 27, 28, 29, 35, 36, 38, 40, 41, 42, 43, 45, 46, 50, 52, 55, 58, 60, 64, 65, 68, 72, 75, 76, 77, 78, 83, 84, 85, 89, 92)) 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, ' -._+').strip() def validate(number): """ Validate the format of a Finnish association register number. First strip all separators and spaces from the number and then checks that it has a correct length and is only numeric. """ number = compact(number) if not number.isdigit(): raise InvalidFormat() if len(number) < 1 or len(number) > 6: raise InvalidLength() if len(number) < 3 and int(number) not in _lownumbers: raise InvalidComponent() return number def is_valid(number): """Checks to see if the number provided is a valid association register number. This checks that the format is correct.""" try: return bool(validate(number)) except ValidationError: return False def format(number): """Reformat the passed number to the standard format.""" number = compact(number) if len(number) <= 3: return number else: return number[:-3] + '.' + number[-3:]