Skip to content

Commit 2a35fc9

Browse files
committed
Add support for Togo TIN
Fixes arthurdejong#374
1 parent 6d366e3 commit 2a35fc9

File tree

3 files changed

+279
-0
lines changed

3 files changed

+279
-0
lines changed

Diff for: stdnum/tg/__init__.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# __init__.py - collection of Togo 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 Togo numbers."""
22+
23+
# provide aliases
24+
from stdnum.tg import nif as vat # noqa: F401

Diff for: stdnum/tg/nif.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# nif.py - functions for handling Togo 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, Togo tax number).
22+
23+
This number consists of 10 digits.
24+
25+
More information:
26+
27+
* https://www.otr.tg/index.php/fr/documentation/sur-les-impots/6-liste-des-nouveaux-nif-des-contribuables/file.html
28+
* https://www.otr.tg/repertoire/index.php/contribuables-actifs.html
29+
30+
>>> validate('1000174915')
31+
'1000174915'
32+
>>> validate('1000 292 023')
33+
'1000292023'
34+
>>> validate('12345')
35+
Traceback (most recent call last):
36+
...
37+
InvalidLength: ...
38+
>>> format('1000 292 023')
39+
'1000292023'
40+
""" # noqa: E501
41+
42+
from stdnum.exceptions import *
43+
from stdnum.util import clean, isdigits
44+
45+
46+
def compact(number):
47+
"""Convert the number to the minimal representation.
48+
49+
This strips the number of any valid separators and removes surrounding
50+
whitespace.
51+
"""
52+
return clean(number, ' ').strip()
53+
54+
55+
def validate(number):
56+
"""Check if the number is a valid Togo NIF number.
57+
58+
This checks the length and formatting.
59+
"""
60+
number = compact(number)
61+
if len(number) != 10:
62+
raise InvalidLength()
63+
if not isdigits(number):
64+
raise InvalidFormat()
65+
return number
66+
67+
68+
def is_valid(number):
69+
"""Check if the number is a valid Togo NIF number."""
70+
try:
71+
return bool(validate(number))
72+
except ValidationError:
73+
return False
74+
75+
76+
def format(number):
77+
"""Reformat the number to the standard presentation format."""
78+
return compact(number)

Diff for: tests/test_tg_nif.doctest

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
test_tg_nif.doctest - more detailed doctests for stdnum.tg.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.tg.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.tg import nif
26+
27+
28+
Tests for some corner cases.
29+
30+
>>> nif.validate('1000174915')
31+
'1000174915'
32+
>>> nif.validate('1000 292 023')
33+
'1000292023'
34+
>>> nif.validate('12345')
35+
Traceback (most recent call last):
36+
...
37+
InvalidLength: ...
38+
>>> nif.validate('VV34567890')
39+
Traceback (most recent call last):
40+
...
41+
InvalidFormat: ...
42+
>>> nif.format('1000 292 023')
43+
'1000292023'
44+
45+
46+
These have been found online and should all be valid numbers.
47+
48+
>>> numbers = '''
49+
...
50+
... 1000174915
51+
... 1000167004
52+
... 1000166131
53+
... 1000174339
54+
... 1000160992
55+
... 1000145206
56+
... 1000161073
57+
... 1000175320
58+
... 1000166365
59+
... 1000173844
60+
... 1000166185
61+
... 1000166419
62+
... 1000166455
63+
... 1000175248
64+
... 1000175680
65+
... 1000175698
66+
... 1000174969
67+
... 1000166338
68+
... 1000174213
69+
... 1000343454
70+
... 1000166374
71+
... 1000175860
72+
... 1001694618
73+
... 1000034173
74+
... 1000166401
75+
... 1000210995
76+
... 1000166689
77+
... 1000160785
78+
... 1000166608
79+
... 1000166158
80+
... 1000175068
81+
... 1000175869
82+
... 1000145062
83+
... 1000166743
84+
... 1000165159
85+
... 1000116001
86+
... 1000174735
87+
... 1000166716
88+
... 1000161442
89+
... 1000145044
90+
... 1000116343
91+
... 1000166977
92+
... 1000166293
93+
... 1000175788
94+
... 1000089811
95+
... 1000145152
96+
... 1000166536
97+
... 1000116325
98+
... 1000160974
99+
... 1000160911
100+
... 1000175419
101+
... 1000160380
102+
... 1000166671
103+
... 1000173970
104+
... 1000174807
105+
... 1000144405
106+
... 1000166554
107+
... 1000166203
108+
... 1000166464
109+
... 1000161343
110+
... 1000167076
111+
... 1000145035
112+
... 1000166491
113+
... 1000116748
114+
... 1000033084
115+
... 1000144576
116+
... 1000385632
117+
... 1000160416
118+
... 1000160326
119+
... 1000166500
120+
... 1000160425
121+
... 1000211141
122+
... 1000166347
123+
... 1000174249
124+
... 1000144801
125+
... 1000174816
126+
... 1000166842
127+
... 1000116172
128+
... 1000166356
129+
... 1000144927
130+
... 1000166995
131+
... 1000166644
132+
... 1000166680
133+
... 1000145071
134+
... 1000166410
135+
... 1000166167
136+
... 1000174276
137+
... 1000166221
138+
... 1000144774
139+
... 1000116568
140+
... 1000166194
141+
... 1000175662
142+
... 1000166329
143+
... 1000174006
144+
... 1000173808
145+
... 1000175698
146+
... 1000093762
147+
... 1000 292 023
148+
... 1000002610
149+
... 1000002619
150+
... 1000002628
151+
... 1000002637
152+
... 1000002646
153+
... 1000002655
154+
... 1000002664
155+
... 1000002673
156+
... 1000009189
157+
... 1000009198
158+
... 1000116496
159+
... 1001585478
160+
... 1000087624
161+
... 1000286204
162+
... 1000822999
163+
... 1001662737
164+
... 1000819204
165+
... 1000129582
166+
... 1000176698
167+
... 1000 2686 18
168+
... 1000145251
169+
... 1001527008
170+
... 1001090612
171+
... 1001808335
172+
... 1000165159
173+
... 1001405758
174+
...
175+
... '''
176+
>>> [x for x in numbers.splitlines() if x and not nif.is_valid(x)]
177+
[]

0 commit comments

Comments
 (0)