8000 Add support for Mauritania TIN · unho/python-stdnum@7add9ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 7add9ee

Browse files
committed
Add support for Mauritania TIN
Fixes arthurdejong#350
1 parent 6d366e3 commit 7add9ee

File tree

3 files changed

+300
-0
lines changed

3 files changed

+300
-0
lines changed

stdnum/mr/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# __init__.py - collection of Mauritania numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2023 Leandro Regueiro
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
"""Collection of Mauritania numbers."""
22+
23+
# provide aliases
24+
from stdnum.mr import nif as vat # noqa: F401

stdnum/mr/nif.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# nit.py - functions for handling Mauritania NIF numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2023 Leandro Regueiro
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
"""NIF (Numéro d'Identification Fiscale, Mauritania tax number).
22+
23+
This number consists of 8 digits.
24+
25+
More information:
26+
27+
* https://impots.gov.mr:8080/DGI/
28+
29+
>>> validate('20300059')
30+
'20300059'
31+
>>> validate('70 401 906')
32+
'70401906'
33+
>>> validate('12345')
34+
Traceback (most recent call last):
35+
...
36+
InvalidLength: ...
37+
>>> format('70 401 906')
38+
'70401906'
39+
"""
40+
41+
from stdnum.exceptions import *
42+
from stdnum.util import clean, isdigits
43+
44+
45+
def compact(number):
46+
"""Convert the number to the minimal representation.
47+
48+
This strips the number of any valid separators and removes surrounding
49+
whitespace.
50+
"""
51+
return clean(number, ' -').strip()
52+
53+
54+
def validate(number):
55+
"""Check if the number is a valid Mauritania NIF number.
56+
57+
This checks the length and formatting.
58+
"""
59+
number = compact(number)
60+
if len(number) != 8:
61+
raise InvalidLength()
62+
if not isdigits(number):
63+
raise InvalidFormat()
64+
return number
65+
66+
67+
def is_valid(number):
68+
"""Check if the number is a valid Mauritania NIF number."""
69+
try:
70+
return bool(validate(number))
71+
except ValidationError:
72+
return False
73+
74+
75+
def format(number):
76+
"""Reformat the number to the standard presentation format."""
77+
return compact(number)

tests/test_mr_nif.doctest

Lines changed: 199 additions & 0 deletions
2851
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
test_mr_nif.doctest - more detailed doctests for stdnum.mr.nif module
2+
3+
Copyright (C) 2023 Leandro Regueiro
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.mr.nif module. It
22+
tries to test more corner cases and detailed functionality that is not really
23+
useful as module documentation.
24+
25+
>>> from stdnum.mr import nif
26+
27+
28+
Tests for some corner cases.
29+
30+
>>> nif.validate('20300059')
31+
'20300059'
32+
>>> nif.validate('70 401 906')
33+
'70401906'
34+
>>> nif.validate('12345')
35+
Traceback (most recent call last):
36+
...
37+
InvalidLength: ...
38+
>>> nif.validate('XX345678')
39+
Traceback (most recent call last):
40+
...
41+
InvalidFormat: ...
42+
>>> nif.format('70 401 906')
43+
'70401906'
44+
45+
46+
These have been found online and should all be valid numbers.
47+
48+
>>> numbers = '''
49+
...
50+
... 20300059
51+
... 00409136
52+
... 90300075
53+
... 00455386
54+
... 00510669
55+
... 00510651
56+
... 00510677
57+
... 20300059
58+
... 00409136
59+
... 90300075
60+
... 00455386
61+
... 00510669
62+
... 00510651
63+
... 00510677
64+
... 20700455
65+
... 00042218
66+
... 11100448
67+
... 00520510
68+
... 10600166
69+
... 00066845
70+
... 30500075
71+
... 00117549
72+
... 70 401 906
73+
... 30600091
74+
... 11100778
75+
... 10701982
76+
... 20400034
77+
... 20705520
78+
... 00554352
79+
... 00550608
80+
... 01196641
81+
... 00060707
82+
... 00127456
83+
... 00407239
84+
... 00091769
85+
... 00055152
86+
... 10701354
87+
... 10701339
88+
... 10707336
89+
... 10713326
90+
... 10701495
91+
... 00194480
92+
... 00076299
93+
... 00516534
94+
... 10800042
95+
... 10706114
96+
... 00066845
97+
... 10709316
98+
... 30200208
99+
... 10709951
100+
... 30200174
101+
... 30700067
102+
... 30700113
103+
... 20600117
104+
... 10717012
105+
... 10707955
106+
... 20705397
107+
... 10702865
108+
... 20602626
109+
... 10703574
110+
... 10713896
111+
... 10713201
112+
... 21103616
113+
... 21100901
114+
... 10600158
115+
... 20901545
116+
... 10708862
117+
... 20703590
118+
... 11100075
119+
... 10713102
120+
... 10707476
121+
... 20600125
122+
... 20601099
123+
... 10706544
124+
... 10712070
125+
... 10600174
126+
... 20702154
127+
... 10718358
128+
... 10715883
129+
... 10700513
130+
... 10602709
131+
... 20602808
132+
... 10721188
133+
... 10703103
134+
... 20601206
135+
... 20600083
136+
... 20600760
137+
... 20704853
138+
... 10708987
139+
... 20701594
140+
... 10603400
141+
... 20704432
142+
... 20302865
143+
... 20600620
144+
... 10701776
145+
... 10704762
146+
... 10718820
147+
... 11101016
148+
... 20602782
149+
... 11108235
150+
... 30600323
151+
... 10700885
152+
... 20705470
153+
... 41100018
154+
... 10602410
155+
... 20602121
156+
... 11104234
157+
... 10718655
158+
... 40700018
159+
... 10720354
160+
... 21106429
161+
... 10600042
162+
... 20602477
163+
... 10718200
164+
... 11100935
165+
... 21100760
166+
... 20703400
167+
... 20704572
168+
... 10600620
169+
... 10701479
170+
... 20601081
171+
... 10721147
172+
... 10700604
173+
... 10601305
174+
... 10600166
175+
... 10723903
176+
... 10601933
177+
... 11107690
178+
... 11100182
179+
... 20602683
180+
... 20600141
181+
... 10700893
182+
... 10715974
183+
... 21101248
184+
... 20705181
185+
... 10701065
186+
... 20704176
187+
... 10600299
188+
... 20701560
189+
... 10715853
190+
... 11107518
191+
... 10722269
192+
... 10707708
193+
... 10900828
194+
... 10603079
195+
... 20704218
196+
...
197+
... '''
198+
>>> [x for x in numbers.splitlines() if x and not nif.is_valid(x)]
199+
[]

0 commit comments

Comments
 (0)
0