lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.3-17-g43b58d3

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.3-17-g43b58d3



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  43b58d331a8e61694a5a9b77fa0968fa326f52cc (commit)
       via  879f2d3c8a777b4ae8a1b97fdd0629e90d7bc871 (commit)
      from  fd9f9538c362aacf50e4ae32ae51b15ecaf79184 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://arthurdejong.org/git/python-stdnum/commit/?id=43b58d331a8e61694a5a9b77fa0968fa326f52cc

commit 43b58d331a8e61694a5a9b77fa0968fa326f52cc
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Tue Jul 26 02:33:16 2016 +0200

    Move NIR check digit calculation to function
    
    This also fixes a number of formatting issues, improves the module
    description and adds tests for the 2A and 2B departments.

diff --git a/stdnum/fr/nir.py b/stdnum/fr/nir.py
index d8c26ee..bf1206a 100644
--- a/stdnum/fr/nir.py
+++ b/stdnum/fr/nir.py
@@ -1,6 +1,8 @@
 # nir.py - functions for handling French NIR numbers
+# coding: utf-8
 #
 # Copyright (C) 2016 Dimitri Papadopoulos
+# Copyright (C) 2016 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
@@ -19,17 +21,23 @@
 
 """NIR (French personal identification number).
 
-The NIR (Numero d'Inscription au Repertoire national d'identification
-des personnes physiques) is a 15 digit number used to identify persons
-in France. All persons born in France are registered in the Repertoire
-national d'identification des personnes physiques (RNIPP) and assigned
-a NIR which consists of 15 digits where the two final digits are check
-digits. The NIR is used by French social security and is popularly known
-as the "social security number".
+The NIR (Numero d'Inscription au Repertoire national d'identification des
+personnes physiques) is used to identify persons in France. It is popularly
+known as the "social security number" and sometimes referred to as an INSEE
+number. All persons born in France are registered in the Repertoire national
+d'identification des personnes physiques (RNIPP) and assigned a NIR.
+
+The number consists of 15 digits: the first digit indicates the gender,
+followed by 2 digits for the year or birth, 2 for the month of birth, 5 for
+the location of birth (COG), 3 for a serial and 2 check digits.
 
 More information:
 
 * http://www.insee.fr/en/methodes/default.asp?page=definitions/nir.htm
+* https://en.wikipedia.org/wiki/INSEE_code
+* http://resoo.org/docs/_docs/regles-numero-insee.pdf
+* https://fr.wikipedia.org/wiki/Numéro_de_sécurité_sociale_en_France
+* http://xml.insee.fr/schema/nir.html
 
 >>> validate('2 95 10 99 126 111 93')
 '295109912611193'
@@ -37,6 +45,10 @@ More information:
 Traceback (most recent call last):
     ...
 InvalidChecksum: ...
+>>> validate('253072B07300470')
+'253072B07300470'
+>>> validate('253072A07300443')
+'253072A07300443'
 >>> format('295109912611193')
 '2 95 10 99 126 111 93'
 """
@@ -51,6 +63,16 @@ def compact(number):
     return clean(number, ' .').strip().upper()
 
 
+def calc_check_digits(number):
+    """Calculate the check digits for the number."""
+    department = number[5:7]
+    if department == '2A':
+        number = number[:5] + '19' + number[7:]
+    elif department == '2B':
+        number = number[:5] + '18' + number[7:]
+    return '%02d' % (97 - (int(number[:13]) % 97))
+
+
 def validate(number):
     """Checks to see if the number provided is valid. This checks the length
     and check digits."""
@@ -61,14 +83,7 @@ def validate(number):
         raise InvalidFormat()
     if len(number) != 15:
         raise InvalidLength()
-    department = number[5:7]
-    if department == '2A':
-        s = number[:5] + '19' + number[7:13]
-    elif department == '2B':
-        s = number[:5] + '18' + number[7:13]
-    else:
-        s = number[:13]
-    if (97 - (int(s) % 97)) != int(number[13:]):
+    if calc_check_digits(number) != number[13:]:
         raise InvalidChecksum()
     return number
 

http://arthurdejong.org/git/python-stdnum/commit/?id=879f2d3c8a777b4ae8a1b97fdd0629e90d7bc871

commit 879f2d3c8a777b4ae8a1b97fdd0629e90d7bc871
Author: Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
Date:   Sun May 29 17:57:05 2016 +0200

    Improve French NIR validation
    
    Please note that the 7th character of the NIR might be 'A' or 'B'. Other
    than that the NIR contains digits only.

diff --git a/stdnum/fr/nir.py b/stdnum/fr/nir.py
index bd9ce80..d8c26ee 100644
--- a/stdnum/fr/nir.py
+++ b/stdnum/fr/nir.py
@@ -48,25 +48,34 @@ 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()
+    return clean(number, ' .').strip().upper()
 
 
 def validate(number):
     """Checks to see if the number provided is valid. This checks the length
-    and check digit."""
+    and check digits."""
     number = compact(number)
-    if not number.isdigit():
+    if not (number.isdigit() or (
+                number[:5].isdigit() and number[7:].isdigit() and
+                number[5:7] in ('2A', '2B'))):
         raise InvalidFormat()
     if len(number) != 15:
         raise InvalidLength()
-    if (97 - (int(number[:13]) % 97)) != int(number[13:]):
+    department = number[5:7]
+    if department == '2A':
+        s = number[:5] + '19' + number[7:13]
+    elif department == '2B':
+        s = number[:5] + '18' + number[7:13]
+    else:
+        s = number[:13]
+    if (97 - (int(s) % 97)) != int(number[13:]):
         raise InvalidChecksum()
     return number
 
 
 def is_valid(number):
     """Checks to see if the number provided is valid. This checks the length
-    and check digit."""
+    and check digits."""
     try:
         return bool(validate(number))
     except ValidationError:

-----------------------------------------------------------------------

Summary of changes:
 stdnum/fr/nir.py | 48 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 12 deletions(-)


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/