diff options
author | Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr> | 2016-05-22 20:09:21 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2016-07-26 14:39:22 +0200 |
commit | 2409ee943859b11539be67b11da078e15e2bdc0d (patch) | |
tree | 33b0fb357d969fd9cf4545ffe22aaf58ac48add8 /stdnum/fr/nif.py | |
parent | 43b58d331a8e61694a5a9b77fa0968fa326f52cc (diff) |
Add NIF - French tax identification number
Add module for NIF also known as SPI number.
Diffstat (limited to 'stdnum/fr/nif.py')
-rw-r--r-- | stdnum/fr/nif.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/stdnum/fr/nif.py b/stdnum/fr/nif.py new file mode 100644 index 0000000..7d5a802 --- /dev/null +++ b/stdnum/fr/nif.py @@ -0,0 +1,75 @@ +# nif.py - functions for handling French tax identification numbers +# coding: utf-8 +# +# Copyright (C) 2016 Dimitri Papadopoulos +# +# 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 (Numéro d'Immatriculation Fiscale, French tax identification number). + +The NIF (Numéro d'Immatriculation Fiscale, Numéro d'Identité Fiscale or +Numéro d'Identification Fiscale) also known as numéro fiscal de référence or +SPI (Simplification des Procédures d'Identification) is a 13-digit number +issued by the French tax authorities to people for tax reporting purposes. + +More information: + +* https://ec.europa.eu/taxation_customs/tin/tinByCountry.html +* https://fr.wikipedia.org/wiki/Num%C3%A9ro_d%27Immatriculation_Fiscale#France + +>>> validate('0701987765432') +'0701987765432' +>>> validate('070198776543') +Traceback (most recent call last): + ... +InvalidLength: ... +>>> format('295109912611193') +'2 95 10 99 126 111 93' +""" + +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, ' ').strip() + + +def validate(number): + """Checks the length to see if the number provided is valid.""" + number = compact(number) + if not number.isdigit(): + raise InvalidFormat() + if len(number) != 13: + raise InvalidLength() + return number + + +def is_valid(number): + """Checks the length to see if the number provided is valid.""" + try: + return bool(validate(number)) + except ValidationError: + return False + + +def format(number): + """Reformat the passed number to the standard format.""" + number = compact(number) + return ' '.join((number[:2], number[2:4], number[4:7], + number[7:10], number[10:])) |