Skip to content

Commit 5ee78a0

Browse files
committed
Add support for Uganda TIN
Fixes arthurdejong#194
1 parent 6d366e3 commit 5ee78a0

File tree

3 files changed

+362
-0
lines changed

3 files changed

+362
-0
lines changed

stdnum/ug/__init__.py

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

stdnum/ug/tin.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# tin.py - functions for handling Uganda TIN 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+
"""TIN (Tax identification number, Uganda tax number).
22+
23+
This number consists of 10 digits.
24+
25+
More information:
26+
27+
* https://thetaxman.ura.go.ug/understanding-a-tax-identification-number-tin/
28+
* https://businessfocus.co.ug/how-to-get-taxpayer-identification-number-from-ura/
29+
30+
>>> validate('1000957588')
31+
'1000957588'
32+
>>> validate('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> format('1000957588')
37+
'1000957588'
38+
""" # noqa: E501
39+
40+
from stdnum.exceptions import *
41+
from stdnum.util import clean, isdigits
42+
43+
44+
def compact(number):
45+
"""Convert the number to the minimal representation.
46+
47+
This strips the number of any valid separators and removes surrounding
48+
whitespace.
49+
"""
50+
return clean(number, ' -').strip()
51+
52+
53+
def validate(number):
54+
"""Check if the number is a valid Uganda TIN number.
55+
56+
This checks the length and formatting.
57+
"""
58+
number = compact(number)
59+
if len(number) != 10:
60+
raise InvalidLength()
61+
if not isdigits(number):
62+
raise InvalidFormat()
63+
return number
64+
65+
66+
def is_valid(number):
67+
"""Check if the number is a valid Uganda TIN number."""
68+
try:
69+
return bool(validate(number))
70+
except ValidationError:
71+
return False
72+
73+
74+
def format(number):
75+
"""Reformat the number to the standard presentation format."""
76+
return compact(number)

tests/test_ug_tin.doctest

+262
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
test_ug_tin.doctest - more detailed doctests for stdnum.ug.tin 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.ug.tin 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.ug import tin
26+
27+
28+
Tests for some corner cases.
29+
30+
>>> tin.validate('1000957588')
31+
'1000957588'
32+
>>> tin.validate('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> tin.validate('10009575XX')
37+
Traceback (most recent call last):
38+
...
39+
InvalidFormat: ...
40+
>>> tin.format('1000957588')
41+
'1000957588'
42+
43+
44+
These have been found online and should all be valid numbers.
45+
46+
>>> numbers = '''
47+
...
48+
... 1000957588
49+
... 1000071933
50+
... 1008904187
51+
... 1024060758
52+
... 1008799009
53+
... 1014870350
54+
... 1000185668
55+
... 1000171284
56+
... 1000459992
57+
... 1017875693
58+
... 1001251612
59+
... 1006801945
60+
... 1000412258
61+
... 1000047445
62+
... 1019689817
63+
... 1000028435
64+
... 1002408394
65+
... 1009431677
66+
... 1000866974
67+
... 1000134869
68+
... 1000049868
69+
... 1000042267
70+
... 1000084312
71+
... 1000566633
72+
... 1000134272
73+
... 1000212673
74+
... 1000785042
75+
... 1000035743
76+
... 1000021193
77+
... 1000020447
78+
... 1000724864
79+
... 1000039236
80+
... 1000028435
81+
... 1000098610
82+
... 1000039754
83+
... 1000158605
84+
... 1000432017
85+
... 1000363499
86+
... 1000113650
87+
... 1000023585
88+
... 1000583175
89+
... 1000054556
90+
... 1000026312
91+
... 1000491360
92+
... 1000573706
93+
... 1000036164
94+
... 1000482371
95+
... 1000287073
96+
... 1000054984
97+
... 1000039364
98+
... 1000025311
99+
... 1007345542
100+
... 1000028459
101+
... 1000131286
102+
... 1006695918
103+
... 1000031221
104+
... 1000026692
105+
... 1000134517
106+
... 1000094357
107+
... 1000026640
108+
... 1000033720
109+
... 1000032087
110+
... 1000041342
111+
... 1000096342
112+
... 1000142650
113+
... 1000030513
114+
... 1000049844
115+
... 1000807252
116+
... 1000123395
117+
... 1000061408
118+
... 1000538227
119+
... 1000027848
120+
... 1000051722
121+
... 1000866974
122+
... 1000103360
123+
... 1000028311
124+
... 1000025080
125+
... 1000030537
126+
... 1007361580
127+
... 1000034745
128+
... 1000979287
129+
... 1000025739
130+
... 1000395040
131+
... 1000071398
132+
... 1000024679
133+
... 1000547002
134+
... 1000040558
135+
... 1000029757
136+
... 1001251277
137+
... 1006994240
138+
... 1000054563
139+
... 1000212642
140+
... 1000027589
141+
... 1009286336
142+
... 1000028725
143+
... 1000101316
144+
... 1000217913
145+
... 1000232097
146+
... 1000131714
147+
... 1000899143
148+
... 1000126305
149+
... 1000486398
150+
... 1000485398
151+
... 1000098116
152+
... 1000161646
153+
... 1003157831
154+
... 1000039778
155+
... 1000112373
156+
... 1000042764
157+
... 1002263840
158+
... 1000024517
159+
... 1000046996
160+
... 1000027144
161+
... 1000041214
162+
... 1000061415
163+
... 1001073346
164+
... 1000123274
165+
... 1007698660
166+
... 1000408392
167+
... 1010664806
168+
... 1000024790
169+
... 1000171284
170+
... 1000175899
171+
... 1000225635
172+
... 1000028428
173+
... 1000055626
174+
... 1000400663
175+
... 1000023944
176+
... 1000443250
177+
... 1009135185
178+
... 1000594518
179+
... 1000967830
180+
... 1000062775
181+
... 1008139706
182+
... 1000132401
183+
... 1000078012
184+
... 1004849404
185+
... 1000029771
186+
... 1000472771
187+
... 1000460461
188+
... 1000099925
189+
... 1000126688
190+
... 1000243257
191+
... 1000028818
192+
... 1000394477
193+
... 1003357368
194+
... 1009975388
195+
... 1000055115
196+
... 1000872860
197+
... 1010783208
198+
... 1012498175
199+
... 1002736330
200+
... 1007088485
201+
... 1001194444
202+
... 1008291934
203+
... 1000935599
204+
... 1000990088
205+
... 1000061201
206+
... 1000186445
207+
... 1013963845
208+
... 1003408785
209+
... 1013958964
210+
... 1012318235
211+
... 1012997342
212+
... 1000056565
213+
... 1003067438
214+
... 1000383555
215+
... 1000382561
216+
... 1000051622
217+
... 1000467745
218+
... 1002665506
219+
... 1008565695
220+
... 1000519607
221+
... 1002665720
222+
... 1002753555
223+
... 1010898089
224+
... 1013779859
225+
... 1001119029
226+
... 1007765966
227+
... 1000061584
228+
... 1014184753
229+
... 1000187094
230+
... 1007071087
231+
... 1014112010
232+
... 1000860008
233+
... 1007075485
234+
... 1000042719
235+
... 1000892736
236+
... 1000048515
237+
... 1000648662
238+
... 1001102936
239+
... 1000094364
240+
... 1011242712
241+
... 1001461384
242+
... 1000773861
243+
... 1001171140
244+
... 1010042518
245+
... 1010449314
246+
... 1011417274
247+
... 1003214205
248+
... 1006980615
249+
... 1009612881
250+
... 1009331018
251+
... 1007230785
252+
... 1000058160
253+
... 1001976219
254+
... 1002965640
255+
... 1003795342
256+
... 1002595231
257+
... 1002155752
258+
... 1000545200
259+
...
260+
... '''
261+
>>> [x for x in numbers.splitlines() if x and not tin.is_valid(x)]
262+
[]

0 commit comments

Comments
 (0)