diff --git a/stdnum/zm/__init__.py b/stdnum/zm/__init__.py new file mode 100644 index 00000000..f71f24bb --- /dev/null +++ b/stdnum/zm/__init__.py @@ -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 diff --git a/stdnum/zm/tpin.py b/stdnum/zm/tpin.py new file mode 100644 index 00000000..7b59ea72 --- /dev/null +++ b/stdnum/zm/tpin.py @@ -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) diff --git a/tests/test_zm_tpin.doctest b/tests/test_zm_tpin.doctest new file mode 100644 index 00000000..592ba05a --- /dev/null +++ b/tests/test_zm_tpin.doctest @@ -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)] +[]