python-stdnum branch master updated. 1.17-10-gbda2a9c
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.17-10-gbda2a9c
- From: Commits of the python-stdnum project <python-stdnum-commits [at] lists.arthurdejong.org>
- To: python-stdnum-commits [at] lists.arthurdejong.org
- Reply-to: python-stdnum-users [at] lists.arthurdejong.org, python-stdnum-commits [at] lists.arthurdejong.org
- Subject: python-stdnum branch master updated. 1.17-10-gbda2a9c
- Date: Mon, 21 Mar 2022 14:55:32 +0100 (CET)
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 bda2a9c8ac0646ca4661e2d85020ba6e60dbf0b9 (commit)
from e2a27743b3f8d57a819d8507573660fd5e0162b3 (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=bda2a9c8ac0646ca4661e2d85020ba6e60dbf0b9
commit bda2a9c8ac0646ca4661e2d85020ba6e60dbf0b9
Author: Cédric Krier <ced@b2ck.com>
Date: Sat Feb 5 18:30:48 2022 +0100
Compute birth date from Belgian National Number
Closes https://github.com/arthurdejong/python-stdnum/pull/288
diff --git a/stdnum/be/nn.py b/stdnum/be/nn.py
index 1c000c8..bada9b9 100644
--- a/stdnum/be/nn.py
+++ b/stdnum/be/nn.py
@@ -1,7 +1,7 @@
# coding=utf-8
# nn.py - function for handling Belgian national numbers
#
-# Copyright (C) 2021 Cédric Krier
+# Copyright (C) 2021-2022 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
@@ -39,6 +39,8 @@ Traceback (most recent call last):
InvalidChecksum: ...
>>> format('85073003328')
'85.07.30-033.28'
+>>> get_birth_date('85.07.30-033 28')
+datetime.date(1985, 7, 30)
"""
import datetime
@@ -55,13 +57,13 @@ def compact(number):
def _checksum(number):
- """Calculate the checksum."""
+ """Calculate the checksum and return the detected century."""
numbers = [number]
if int(number[:2]) + 2000 <= datetime.date.today().year:
numbers.append('2' + number)
- for n in numbers:
+ for century, n in zip((19, 20), numbers):
if 97 - (int(n[:-2]) % 97) == int(n[-2:]):
- return True
+ return century
return False
@@ -91,3 +93,16 @@ def format(number):
return (
'.'.join(number[i:i + 2] for i in range(0, 6, 2)) +
'-' + '.'.join([number[6:9], number[9:11]]))
+
+
+def get_birth_date(number):
+ """Return the date of birth"""
+ number = compact(number)
+ century = _checksum(number)
+ if not century:
+ raise InvalidChecksum()
+ try:
+ return datetime.datetime.strptime(
+ str(century) + number[:6], '%Y%m%d').date()
+ except ValueError:
+ raise InvalidComponent()
diff --git a/tests/test_be_nn.doctest b/tests/test_be_nn.doctest
new file mode 100644
index 0000000..287ba05
--- /dev/null
+++ b/tests/test_be_nn.doctest
@@ -0,0 +1,42 @@
+test_be_nn.doctest - more detailed doctests for stdnum.be.nn module
+
+Copyright (C) 2022 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
+
+
+This file contains more detailed doctests for the stdnum.be.nn module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.be import nn
+
+
+Extra tests for getting birth date
+
+
+>>> nn.get_birth_date('85.07.30-033 28')
+datetime.date(1985, 7, 30)
+>>> nn.get_birth_date('17 07 30 033 84')
+datetime.date(2017, 7, 30)
+>>> nn.get_birth_date('12345678901')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> nn.get_birth_date('00 00 01 003-64') # 2000-00-00 is not a valid date
+Traceback (most recent call last):
+ ...
+InvalidComponent: ...
-----------------------------------------------------------------------
Summary of changes:
stdnum/be/nn.py | 23 ++++++++++++++----
tests/{test_ean.doctest => test_be_nn.doctest} | 33 +++++++++++++-------------
2 files changed, 35 insertions(+), 21 deletions(-)
copy tests/{test_ean.doctest => test_be_nn.doctest} (63%)
hooks/post-receive
--
python-stdnum
- python-stdnum branch master updated. 1.17-10-gbda2a9c,
Commits of the python-stdnum project