lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.11-21-g1e814ce

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

python-stdnum branch master updated. 1.11-21-g1e814ce



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  1e814cea8d86d0b69a5fbb5b5107036e4fd6b119 (commit)
      from  8c1015a3623762375e5231df84af68ac6a21e837 (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=1e814cea8d86d0b69a5fbb5b5107036e4fd6b119

commit 1e814cea8d86d0b69a5fbb5b5107036e4fd6b119
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Mon Jun 10 15:12:04 2019 +0200

    Add South Africa TIN number
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/129
    Closes https://github.com/arthurdejong/python-stdnum/issues/108

diff --git a/stdnum/za/__init__.py b/stdnum/za/__init__.py
new file mode 100644
index 0000000..2424e9c
--- /dev/null
+++ b/stdnum/za/__init__.py
@@ -0,0 +1,21 @@
+# __init__.py - collection of South Africa numbers
+# coding: utf-8
+#
+# Copyright (C) 2019 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 South Africa numbers."""
diff --git a/stdnum/za/tin.py b/stdnum/za/tin.py
new file mode 100644
index 0000000..cde0fdf
--- /dev/null
+++ b/stdnum/za/tin.py
@@ -0,0 +1,85 @@
+# tin.py - functions for handling South Africa Tax Reference Number
+# coding: utf-8
+#
+# Copyright (C) 2019 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
+
+"""TIN (South African Tax Identification Number).
+
+The South African Tax Identification Number (TIN or Tax Reference Number) is
+issued to individuals and legal entities for tax purposes. The number
+consists of 10 digits.
+
+More information:
+
+* 
https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/South-Africa-TIN.pdf
+* https://www.sars.gov.za/
+
+>>> validate('0001339050')
+'0001339050'
+>>> validate('2449/494/16/0')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('9125568')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> format('084308984-8')
+'0843089848'
+"""
+
+from stdnum import luhn
+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, ' -/').upper().strip()
+
+
+def validate(number):
+    """Check if the number is a valid South Africa Tax Reference Number.
+
+    This checks the length, formatting and check digit.
+    """
+    number = compact(number)
+    if len(number) != 10:
+        raise InvalidLength()
+    if not isdigits(number):
+        raise InvalidFormat()
+    if number[0] not in '01239':
+        raise InvalidComponent()
+    return luhn.validate(number)
+
+
+def is_valid(number):
+    """Check if the number is a valid South Africa Tax Reference 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_za_tin.doctest b/tests/test_za_tin.doctest
new file mode 100644
index 0000000..917a699
--- /dev/null
+++ b/tests/test_za_tin.doctest
@@ -0,0 +1,65 @@
+test_za_tin.doctest - more detailed doctests for stdnum.za.tin module
+
+Copyright (C) 2019 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.za.tin module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.za import tin
+
+
+Tests for some corner cases.
+
+>>> tin.validate('0001339050')
+'0001339050'
+>>> tin.validate('0843089848')
+'0843089848'
+>>> tin.validate('0843/089/84/8')
+'0843089848'
+>>> tin.format('084308984-8')
+'0843089848'
+>>> tin.validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> tin.validate('FF01339050')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> tin.validate('5001339050')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> tin.validate('2449/494/16/0')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 0001339050
+... 0843089848
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not tin.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/{au => za}/__init__.py                      |  6 +--
 stdnum/{py/ruc.py => za/tin.py}                    | 60 ++++++++--------------
 tests/{test_nz_ird.doctest => test_za_tin.doctest} | 51 +++++++++---------
 3 files changed, 49 insertions(+), 68 deletions(-)
 copy stdnum/{au => za}/__init__.py (85%)
 copy stdnum/{py/ruc.py => za/tin.py} (55%)
 copy tests/{test_nz_ird.doctest => test_za_tin.doctest} (60%)


hooks/post-receive
-- 
python-stdnum