|
| 1 | +# fodselsnummer.py - functions for handling Norwegian birth numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2018 Ilya Vihtinsky |
| 5 | +# Copyright (C) 2018 Arthur de Jong |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | +# 02110-1301 USA |
| 21 | + |
| 22 | +"""Fødselsnummer (Norwegian birth number, the national identity number). |
| 23 | +
|
| 24 | +The Fødselsnummer is an eleven-digit number that is built up of the date of |
| 25 | +birth of the person, a serial number and two check digits. |
| 26 | +
|
| 27 | +More information: |
| 28 | +
|
| 29 | +* https://no.wikipedia.org/wiki/F%C3%B8dselsnummer |
| 30 | +* https://en.wikipedia.org/wiki/National_identification_number#Norway |
| 31 | +
|
| 32 | +>>> validate('684131 52112') |
| 33 | +'68413152112' |
| 34 | +>>> get_gender('684131 52112') |
| 35 | +'M' |
| 36 | +>>> validate('684131 52123') |
| 37 | +Traceback (most recent call last): |
| 38 | + ... |
| 39 | +InvalidChecksum: ... |
| 40 | +>>> format('68413152112') |
| 41 | +'684131 52112' |
| 42 | +""" |
| 43 | + |
| 44 | +from stdnum.exceptions import * |
| 45 | +from stdnum.util import clean |
| 46 | + |
| 47 | + |
| 48 | +def compact(number): |
| 49 | + """Convert the number to the minimal representation. This strips the |
| 50 | + number of any valid separators and removes surrounding whitespace.""" |
| 51 | + return clean(number, ' -:') |
| 52 | + |
| 53 | + |
| 54 | +def calc_check_digit1(number): |
| 55 | + """Calculate the first check digit for the number.""" |
| 56 | + weights = (3, 7, 6, 1, 8, 9, 4, 5, 2) |
| 57 | + return str((11 - sum(w * int(n) for w, n in zip(weights, number))) % 11) |
| 58 | + |
| 59 | + |
| 60 | +def calc_check_digit2(number): |
| 61 | + """Calculate the second check digit for the number.""" |
| 62 | + weights = (5, 4, 3, 2, 7, 6, 5, 4, 3, 2) |
| 63 | + return str((11 - sum(w * int(n) for w, n in zip(weights, number))) % 11) |
| 64 | + |
| 65 | + |
| 66 | +def get_gender(number): |
| 67 | + """Get the person's birth gender ('M' or 'F').""" |
| 68 | + number = compact(number) |
| 69 | + if int(number[8]) % 2: |
| 70 | + return 'M' |
| 71 | + else: |
| 72 | + return 'F' |
| 73 | + |
| 74 | + |
| 75 | +def validate(number): |
| 76 | + """Check if the number is a valid birth number.""" |
| 77 | + number = compact(number) |
| 78 | + if len(number) != 11: |
| 79 | + raise InvalidLength() |
| 80 | + if not number.isdigit(): |
| 81 | + raise InvalidFormat() |
| 82 | + if number[-2] != calc_check_digit1(number): |
| 83 | + raise InvalidChecksum() |
| 84 | + if number[-1] != calc_check_digit2(number): |
| 85 | + raise InvalidChecksum() |
| 86 | + return number |
| 87 | + |
| 88 | + |
| 89 | +def is_valid(number): |
| 90 | + """Check if the number is a valid birth number.""" |
| 91 | + try: |
| 92 | + return bool(validate(number)) |
| 93 | + except ValidationError: |
| 94 | + return False |
| 95 | + |
| 96 | + |
| 97 | +def format(number): |
| 98 | + """Reformat the number to the standard presentation format.""" |
| 99 | + number = compact(number) |
| 100 | + return number[:6] + ' ' + number[6:] |
0 commit comments