Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Zambia TIN #382

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions stdnum/zm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# __init__.py - collection of Zambia numbers
# coding: utf-8
#
# Copyright (C) 2023 Leandro Regueiro
#
# 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 Zambia numbers."""

# provide aliases
from stdnum.zm import tpin as vat # noqa: F401
76 changes: 76 additions & 0 deletions stdnum/zm/tpin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# tpin.py - functions for handling Zambia TPIN numbers
# coding: utf-8
#
# Copyright (C) 2023 Leandro Regueiro
#
# 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

"""TPIN (Taxpayer Identification Number, Zambia tax number).

This number consists of 10 digits.

More information:

* https://www.zra.org.zm/faq/
* http://www.businesslicenses.gov.zm/business-procedures/details/tpin-registration

>>> validate('1001788854')
'1001788854'
>>> validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> format('1001788854')
'1001788854'
""" # noqa: E501

from stdnum.exceptions import *
from stdnum.util import clean, isdigits


def compact(number):
"""Convert the number to the minimal representation.

This strips the number of any valid separators and removes surrounding
whitespace.
"""
return clean(number, ' -').strip()


def validate(number):
"""Check if the number is a valid Zambia TPIN number.

This checks the length and formatting.
"""
number = compact(number)
if len(number) != 10:
raise InvalidLength()
if not isdigits(number):
raise InvalidFormat()
return number


def is_valid(number):
"""Check if the number is a valid Zambia TPIN number."""
try:
return bool(validate(number))
except ValidationError:
return False


def format(number):
"""Reformat the number to the standard presentation format."""
return compact(number)
63 changes: 63 additions & 0 deletions tests/test_zm_tpin.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
test_zm_tpin.doctest - more detailed doctests for stdnum.zm.tpin module

Copyright (C) 2023 Leandro Regueiro

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.zm.tpin module. It
tries to test more corner cases and detailed functionality that is not really
useful as module documentation.

>>> from stdnum.zm import tpin


Tests for some corner cases.

>>> tpin.validate('1001788854')
'1001788854'
>>> tpin.validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> tpin.validate('VV34567890')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> tpin.format('1001788854')
'1001788854'


These have been found online and should all be valid numbers.

>>> numbers = '''
...
... 1001788854
... 1002198166
... 1001998819
... 2991788603
... 1002188228
... 1006160651
... 1002327663
... 1002503851
... 1003661603
... 1001710408
... 1002169843
... 1001218594
...
... '''
>>> [x for x in numbers.splitlines() if x and not tpin.is_valid(x)]
[]