python-stdnum branch master updated. 1.9-7-gec39d86
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.9-7-gec39d86
- 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
- Subject: python-stdnum branch master updated. 1.9-7-gec39d86
- Date: Wed, 8 Aug 2018 14:29:44 +0200 (CEST)
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 ec39d86d1d6a7f47d4c0d9dc70089bdc48e5e7ab (commit)
from 676d62c307e1456763b0dcd8e9149a8eaee7c3d5 (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=ec39d86d1d6a7f47d4c0d9dc70089bdc48e5e7ab
commit ec39d86d1d6a7f47d4c0d9dc70089bdc48e5e7ab
Author: Arthur de Jong <arthur@arthurdejong.org>
Date: Wed Aug 8 14:19:13 2018 +0200
Add Mauritian national ID number
Thans to Bradley Smith for providing the needed information to implement
this.
See https://lists.arthurdejong.org/python-stdnum-users/2018/msg00003.html
diff --git a/stdnum/mu/__init__.py b/stdnum/mu/__init__.py
new file mode 100644
index 0000000..1fff166
--- /dev/null
+++ b/stdnum/mu/__init__.py
@@ -0,0 +1,21 @@
+# __init__.py - collection of Mauritian numbers
+# coding: utf-8
+#
+# Copyright (C) 2018 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
+
+"""Collection of Mauritian numbers."""
diff --git a/stdnum/mu/nid.py b/stdnum/mu/nid.py
new file mode 100644
index 0000000..354de4a
--- /dev/null
+++ b/stdnum/mu/nid.py
@@ -0,0 +1,94 @@
+# nid.py - functions for handling Mauritian national ID numbers
+# coding: utf-8
+#
+# Copyright (C) 2018 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
+
+"""ID number (Mauritian national identifier).
+
+The Mauritian national ID number is a unique 14 alphanumeric identifier
+assigned at birth to identify individuals. It is displayed on the National
+Identity Card.
+
+The number consists of one alphabetic character and thirteen digits:
+
+* the first character of the person's surname at birth
+* 2 digits for day of birth
+* 2 digits for month of birth
+* 2 digits for year of birth
+* 6 digit unique id
+* a check digit
+
+More information:
+
+* http://mnis.govmu.org/English/ID%20Card/Pages/default.aspx
+"""
+
+import datetime
+
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+# characters used for checksum calculation
+_alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+
+
+def compact(number):
+ """Convert the number to the minimal representation. This strips
+ surrounding whitespace and separation dash."""
+ return clean(number, ' ').upper().strip()
+
+
+def calc_check_digit(number):
+ """Calculate the check digit for the number."""
+ check = sum((14 - i) * _alphabet.index(n)
+ for i, n in enumerate(number[:13]))
+ return _alphabet[(17 - check) % 17]
+
+
+def _get_date(number):
+ """Convert the part of the number that represents a date into a
+ datetime. Note that the century may be incorrect."""
+ day = int(number[1:3])
+ month = int(number[3:5])
+ year = int(number[5:7])
+ try:
+ return datetime.date(year + 2000, month, day)
+ except ValueError:
+ raise InvalidComponent()
+
+
+def validate(number):
+ """Check if the number is a valid ID number."""
+ number = compact(number)
+ if len(number) != 14:
+ raise InvalidLength()
+ if not number[0].isalpha() or not number[1:-1].isdigit():
+ raise InvalidFormat()
+ if calc_check_digit(number) != number[-1]:
+ raise InvalidChecksum()
+ _get_date(number)
+ return number
+
+
+def is_valid(number):
+ """Check if the number provided is a valid RFC."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
diff --git a/tests/test_mu_nid.doctest b/tests/test_mu_nid.doctest
new file mode 100644
index 0000000..93f6adc
--- /dev/null
+++ b/tests/test_mu_nid.doctest
@@ -0,0 +1,94 @@
+test_mu_nid.doctest - more detailed doctests for the stdnum.mu.nid module
+
+Copyright (C) 2018 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.mu.nid module. It
+tries handle more corner cases than are useful as module documentation.
+
+>>> from stdnum.mu import nid
+
+
+Some simple tests.
+
+>>> nid.validate('12345678901234') # all-digits
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> nid.validate('ABCDEFGHIJKLMN') # all-alhpa
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> nid.validate('A311299123456') # missing check digit
+Traceback (most recent call last):
+ ...
+InvalidLength: ...
+>>> nid.validate('A999999123456F') # invalid date
+Traceback (most recent call last):
+ ...
+InvalidComponent: ...
+>>> nid.validate('A3112991234565') # invalid check digit
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> nid.validate('A3112991234567')
+'A3112991234567'
+
+These have been randomly generated and tested against the validator at
+https://eservices.mra.mu/apptan/feedpdfapptan
+
+>>> numbers = '''
+...
+... A0503022303817
+... A2103451905713
+... B120106274060E
+... B2209251149773
+... C0302977326799
+... C090939529731G
+... D0906040423734
+... D110163477627F
+... G270634609988D
+... H070461221669C
+... H100257938348B
+... I050125386993D
+... J1205062398729
+... K210220118460G
+... L2410001918056
+... M2206357474780
+... N160734180180A
+... N1806401273261
+... O190339068436F
+... P0301682918358
+... Q300706590045E
+... R2307179551405
+... S0408513643074
+... S180622940994C
+... T1801758951565
+... U1010291604172
+... U1507158217746
+... U1605075318231
+... V180331350210A
+... W2304253291007
+... X1111599499508
+... X2402942866912
+... Y2504945824300
+... Z170971799359B
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not nid.is_valid(x)]
+[]
-----------------------------------------------------------------------
Summary of changes:
stdnum/{me => mu}/__init__.py | 4 +-
stdnum/mu/nid.py | 94 +++++++++++++++++++++++++++++++++++++++++++
tests/test_mu_nid.doctest | 94 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 190 insertions(+), 2 deletions(-)
copy stdnum/{me => mu}/__init__.py (89%)
create mode 100644 stdnum/mu/nid.py
create mode 100644 tests/test_mu_nid.doctest
hooks/post-receive
--
python-stdnum
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
https://lists.arthurdejong.org/python-stdnum-commits/
- python-stdnum branch master updated. 1.9-7-gec39d86,
Commits of the python-stdnum project