diff options
author | Jeff Horemans <jeff.horemans@vortex-financials.be> | 2023-01-05 13:27:29 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2023-04-29 15:32:36 +0200 |
commit | 42d2792bcac8692b2c081dead7a5061a4540abe6 (patch) | |
tree | 88ee5de249d35d020a9a7cc9f825ce796761e83a | |
parent | cf14a9ff079b53da57c7e17e7a5686734795285e (diff) |
Add functionality to get gender from Belgian National Number
This also extends the documentation for the number.
Closes https://github.com/arthurdejong/python-stdnum/pull/347/files
-rw-r--r-- | stdnum/be/nn.py | 28 | ||||
-rw-r--r-- | tests/test_be_nn.doctest | 6 |
2 files changed, 29 insertions, 5 deletions
diff --git a/stdnum/be/nn.py b/stdnum/be/nn.py index bada9b9..fd6c627 100644 --- a/stdnum/be/nn.py +++ b/stdnum/be/nn.py @@ -18,13 +18,21 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 USA -"""NN, NISS (Belgian national number). +"""NN, NISS, RRN (Belgian national number). -The national number is a unique identifier of Belgian. The number consists of -11 digits. +The national registration number (Rijksregisternummer, Numéro de registre +national, Nationalregisternummer) is a unique identification number of +natural persons who are registered in Belgium. + +The number consists of 11 digits and includes the person's date of birth and +gender. It encodes the date of birth in the first 6 digits in the format +YYMMDD. The following 3 digits represent a counter of people born on the same +date, seperated by sex (odd for male and even for females respectively). The +final 2 digits form a check number based on the 9 preceding digits. More information: +* https://nl.wikipedia.org/wiki/Rijksregisternummer * https://fr.wikipedia.org/wiki/Numéro_de_registre_national >>> compact('85.07.30-033 28') @@ -41,6 +49,8 @@ InvalidChecksum: ... '85.07.30-033.28' >>> get_birth_date('85.07.30-033 28') datetime.date(1985, 7, 30) +>>> get_gender('85.07.30-033 28') +'M' """ import datetime @@ -68,7 +78,7 @@ def _checksum(number): def validate(number): - """Check if the number if a valid National Number.""" + """Check if the number is a valid National Number.""" number = compact(number) if not isdigits(number) or int(number) <= 0: raise InvalidFormat() @@ -96,7 +106,7 @@ def format(number): def get_birth_date(number): - """Return the date of birth""" + """Return the date of birth.""" number = compact(number) century = _checksum(number) if not century: @@ -106,3 +116,11 @@ def get_birth_date(number): str(century) + number[:6], '%Y%m%d').date() except ValueError: raise InvalidComponent() + + +def get_gender(number): + """Get the person's gender ('M' or 'F').""" + number = compact(number) + if int(number[6:9]) % 2: + return 'M' + return 'F' diff --git a/tests/test_be_nn.doctest b/tests/test_be_nn.doctest index 287ba05..492218d 100644 --- a/tests/test_be_nn.doctest +++ b/tests/test_be_nn.doctest @@ -40,3 +40,9 @@ InvalidChecksum: ... Traceback (most recent call last): ... InvalidComponent: ... + + +Extra tests for getting gender + +>>> nn.get_gender('75.06.08-980.09') +'F' |