lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.17-33-gd70549a

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.17-33-gd70549a



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  d70549a36bc6f13c21dc1b4b6042d3532f84d2e8 (commit)
      from  dd70cd5a1d9b934e532ed96fe775a5e9d282723b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=d70549a36bc6f13c21dc1b4b6042d3532f84d2e8

commit d70549a36bc6f13c21dc1b4b6042d3532f84d2e8
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Mon Aug 8 22:18:40 2022 +0200

    Add Kenyan TIN
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/300
    Closes https://github.com/arthurdejong/python-stdnum/pull/310

diff --git a/stdnum/ke/__init__.py b/stdnum/ke/__init__.py
new file mode 100644
index 0000000..1b33710
--- /dev/null
+++ b/stdnum/ke/__init__.py
@@ -0,0 +1,24 @@
+# __init__.py - collection of Kenyan numbers
+# coding: utf-8
+#
+# Copyright (C) 2022 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 Kenyan numbers."""
+
+# provide aliases
+from stdnum.ke import pin as vat  # noqa: F401
diff --git a/stdnum/ke/pin.py b/stdnum/ke/pin.py
new file mode 100644
index 0000000..82e4918
--- /dev/null
+++ b/stdnum/ke/pin.py
@@ -0,0 +1,95 @@
+# pin.py - functions for handling Kenya PIN numbers
+# coding: utf-8
+#
+# Copyright (C) 2022 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
+
+"""PIN (Personal Identification Number, Kenya tax number).
+
+The Personal Identification Number (KRA PIN) is an 11 digit unique number that
+is issued by Kenya Revenue Authority (KRA) for purposes of transacting business
+with KRA, other Government agencies and service providers. It can be issued for
+individuals and non-individuals like companies, schools, organisations, etc.
+
+The number consists of 11 characters, where the first one is an A (for
+individuals) or a P (for non-individuals), the last one is a letter, and the
+rest are digits.
+
+More information:
+
+* 
https://www.kra.go.ke/individual/individual-pin-registration/learn-about-pin/about-pin
+* https://itax.kra.go.ke/KRA-Portal/pinChecker.htm
+
+>>> validate('P051365947M')
+'P051365947M'
+>>> validate('A004416331M')
+'A004416331M'
+>>> validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> validate('V1234567890')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> format('a004416331m')
+'A004416331M'
+"""
+
+import re
+
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+# The number consists of 11 characters, where the first one is an A (for
+# individuals) or a P (for non-individuals), the last one is a letter, and the
+# rest are digits.
+_pin_re = re.compile(r'^[A|P]{1}[0-9]{9}[A-Z]{1}$')
+
+
+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().upper()
+
+
+def validate(number):
+    """Check if the number is a valid Kenya PIN number.
+
+    This checks the length and formatting.
+    """
+    number = compact(number)
+    if len(number) != 11:
+        raise InvalidLength()
+    match = _pin_re.search(number)
+    if not match:
+        raise InvalidFormat()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid Kenya PIN 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_ke_pin.doctest b/tests/test_ke_pin.doctest
new file mode 100644
index 0000000..1b730ec
--- /dev/null
+++ b/tests/test_ke_pin.doctest
@@ -0,0 +1,153 @@
+test_ke_pin.doctest - more detailed doctests for stdnum.ke.pin module
+
+Copyright (C) 2022 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.ke.pin module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.ke import pin
+
+
+Tests for some corner cases.
+
+>>> pin.validate('A006665913Y')
+'A006665913Y'
+>>> pin.validate('P 051193029-r')
+'P051193029R'
+>>> pin.format('P 051193029-r')
+'P051193029R'
+>>> pin.validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> pin.validate('X1234567897')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... A001906275H
+... A003364162P
+... A004690554X
+... A004764821W
+... A005171423w
+... A008965081L
+... A010165600X
+... A014678229G
+... A015002408V
+... P000591575X
+... P000592118K
+... P000592265X
+... P000592366Z
+... P000592851X
+... P000593471Z
+... P000593916S
+... P000595037X
+... P000595362D
+... P000595438C
+... P000595467J
+... P000596373I
+... P000596967Q
+... P000597143A
+... P000597694S
+... P000598224A
+... P000598330Z
+... P000598522B
+... P000599642I
+... P000600314X
+... P000602904I
+... P000602920I
+... P000603130E
+... P000603639S
+... P000604053O
+... P000605255W
+... P000605674D
+... P000607839E
+... P000608167F
+... P000609340W
+... P000609345B
+... P000609346C
+... P000609356G
+... P000609360C
+... P000609361D
+... P000609362E
+... P000609364G
+... P000609365H
+... P000609367J
+... P000609370E
+... P000609661G
+... P000609671I
+... P000611951N
+... P000611975V
+... P000612067R
+... P000612260M
+... P000612829R
+... P000613824O
+... P000615058Y
+... P000619379O
+... P000619594P
+... P051092778I
+... P051092962C
+... P051094112J
+... P051094227U
+... P051094522S
+... P051100248H
+... P051100289S
+... P051102426H
+... P051102575T
+... P051104242J
+... P051106014G
+... P051112430E
+... P051116151S
+... P051117306P
+... P051123493Y
+... P051124237Z
+... P051124533N
+... P051141061H
+... P051149832D
+... P051151506E
+... P051153177Y
+... P051153543Q
+... P051155350V
+... P051160236J
+... P051170839T
+... P051172781A
+... P051332840T
+... P051351120F
+... P051365947M
+... P051391353w
+... P051474338E
+... P051603577M
+... P051647363S
+... P051675891A
+... P051828116I
+... P051855612G
+... P051909085B
+... P051912053E
+... p051606652M
+... p051896085c
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not pin.is_valid(x)]
+[]

-----------------------------------------------------------------------

Summary of changes:
 stdnum/{tn => ke}/__init__.py |   6 +-
 stdnum/ke/pin.py              |  95 ++++++++++++++++++++++++++
 tests/test_ke_pin.doctest     | 153 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 251 insertions(+), 3 deletions(-)
 copy stdnum/{tn => ke}/__init__.py (86%)
 create mode 100644 stdnum/ke/pin.py
 create mode 100644 tests/test_ke_pin.doctest


hooks/post-receive
-- 
python-stdnum