lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.17-1-gdcf4730

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

python-stdnum branch master updated. 1.17-1-gdcf4730



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  dcf47300aa522ad4a76c9062e10f7ef4e627e204 (commit)
      from  50650a94bba1a5175607003bccc91c69ad9645c3 (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 -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=dcf47300aa522ad4a76c9062e10f7ef4e627e204

commit dcf47300aa522ad4a76c9062e10f7ef4e627e204
Author: Cédric Krier <ced@b2ck.com>
Date:   Mon Nov 29 10:46:19 2021 +0100

    Add Belgian National Number
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/284

diff --git a/stdnum/be/__init__.py b/stdnum/be/__init__.py
index 83af30b..d131345 100644
--- a/stdnum/be/__init__.py
+++ b/stdnum/be/__init__.py
@@ -21,4 +21,5 @@
 """Collection of Belgian numbers."""
 
 # provide businessid as an alias
+from stdnum.be import nn as personalid  # noqa: F401
 from stdnum.be import vat as businessid  # noqa: F401
diff --git a/stdnum/be/nn.py b/stdnum/be/nn.py
new file mode 100644
index 0000000..1c000c8
--- /dev/null
+++ b/stdnum/be/nn.py
@@ -0,0 +1,93 @@
+# coding=utf-8
+# nn.py - function for handling Belgian national numbers
+#
+# Copyright (C) 2021 Cédric Krier
+#
+# 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
+
+"""NN, NISS (Belgian national number).
+
+The national number is a unique identifier of Belgian. The number consists of
+11 digits.
+
+More information:
+
+* https://fr.wikipedia.org/wiki/Numéro_de_registre_national
+
+>>> compact('85.07.30-033 28')
+'85073003328'
+>>> validate('85 07 30 033 28')
+'85073003328'
+>>> validate('17 07 30 033 84')
+'17073003384'
+>>> validate('12345678901')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> format('85073003328')
+'85.07.30-033.28'
+"""
+
+import datetime
+
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits
+
+
+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, ' -.').strip()
+    return number
+
+
+def _checksum(number):
+    """Calculate the checksum."""
+    numbers = [number]
+    if int(number[:2]) + 2000 <= datetime.date.today().year:
+        numbers.append('2' + number)
+    for n in numbers:
+        if 97 - (int(n[:-2]) % 97) == int(n[-2:]):
+            return True
+    return False
+
+
+def validate(number):
+    """Check if the number if a valid National Number."""
+    number = compact(number)
+    if not isdigits(number) or int(number) <= 0:
+        raise InvalidFormat()
+    if len(number) != 11:
+        raise InvalidLength()
+    if not _checksum(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid National Number."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number):
+    """Reformat the number to the standard presentation format."""
+    number = compact(number)
+    return (
+        '.'.join(number[i:i + 2] for i in range(0, 6, 2)) +
+        '-' + '.'.join([number[6:9], number[9:11]]))

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

Summary of changes:
 stdnum/be/__init__.py          |  1 +
 stdnum/{au/tfn.py => be/nn.py} | 68 +++++++++++++++++++++++-------------------
 2 files changed, 39 insertions(+), 30 deletions(-)
 copy stdnum/{au/tfn.py => be/nn.py} (52%)


hooks/post-receive
-- 
python-stdnum