Skip to content

Commit 8cd966e

Browse files
committed
Add support for Zambia TIN
Fixes arthurdejong#368
1 parent 6d366e3 commit 8cd966e

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

stdnum/zm/__init__.py

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

stdnum/zm/tpin.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# tpin.py - functions for handling Zambia TPIN 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+
"""TPIN (Taxpayer Identification Number, Zambia tax number).
22+
23+
This number consists of 10 digits.
24+
25+
More information:
26+
27+
* https://www.zra.org.zm/faq/
28+
* http://www.businesslicenses.gov.zm/business-procedures/details/tpin-registration
29+
30+
>>> validate('1001788854')
31+
'1001788854'
32+
>>> validate('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> format('1001788854')
37+
'1001788854'
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 Zambia TPIN 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 Zambia TPIN 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_zm_tpin.doctest

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
test_zm_tpin.doctest - more detailed doctests for stdnum.zm.tpin 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.zm.tpin 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.zm import tpin
26+
27+
28+
Tests for some corner cases.
29+
30+
>>> tpin.validate('1001788854')
31+
'1001788854'
32+
>>> tpin.validate('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> tpin.validate('VV34567890')
37+
Traceback (most recent call last):
38+
...
39+
InvalidFormat: ...
40+
>>> tpin.format('1001788854')
41+
'1001788854'
42+
43+
44+
These have been found online and should all be valid numbers.
45+
46+
>>> numbers = '''
47+
...
48+
... 1001788854
49+
... 1002198166
50+
... 1001998819
51+
... 2991788603
52+
... 1002188228
53+
... 1006160651
54+
... 1002327663
55+
... 1002503851
56+
... 1003661603
57+
... 1001710408
58+
... 1002169843
59+
... 1001218594
60+
...
61+
... '''
62+
>>> [x for x in numbers.splitlines() if x and not tpin.is_valid(x)]
63+
[]

0 commit comments

Comments
 (0)