python-stdnum branch master updated. 1.11-14-g7211ccb
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.11-14-g7211ccb
- From: Commits of the python-stdnum project <python-stdnum-commits [at] lists.arthurdejong.org>
- To: python-stdnum-commits [at] lists.arthurdejong.org
- Reply-to: python-stdnum-users [at] lists.arthurdejong.org, python-stdnum-commits [at] lists.arthurdejong.org
- Subject: python-stdnum branch master updated. 1.11-14-g7211ccb
- Date: Sun, 9 Jun 2019 19:44:32 +0200 (CEST)
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 7211ccbc3f4f16e4069bb95aad308e17ef256623 (commit)
from c969fc8ac83a42f5d7acbd2d0e1a32f619a33ce8 (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=7211ccbc3f4f16e4069bb95aad308e17ef256623
commit 7211ccbc3f4f16e4069bb95aad308e17ef256623
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date: Sun Jun 2 15:52:13 2019 +0200
Add New Zealand IRD number
Closes https://github.com/arthurdejong/python-stdnum/pull/112
Closes https://github.com/arthurdejong/python-stdnum/issues/104
diff --git a/stdnum/nz/ird.py b/stdnum/nz/ird.py
new file mode 100644
index 0000000..50b6d01
--- /dev/null
+++ b/stdnum/nz/ird.py
@@ -0,0 +1,102 @@
+# ird.py - functions for handling New Zealand IRD 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
+
+"""IRD number (New Zealand Inland Revenue Department (Te Tari Tāke) number).
+
+The IRD number is used by the New Zealand Inland Revenue Department (Te Tari
+Tāke in Māori) to identify businesses and individuals for tax purposes. The
+number consists of 8 or 9 digits where the last digit is a check digit.
+
+More information:
+
+* https://www.ird.govt.nz/
+*
https://www.ird.govt.nz/-/media/Project/IR/PDF/2020RWTNRWTSpecificationDocumentv10.pdf
+*
https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/New%20Zealand-TIN.pdf
+
+>>> validate('4909185-0')
+'49091850'
+>>> validate('NZ 49-098-576')
+'49098576'
+>>> validate('136410133')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> validate('9125568')
+Traceback (most recent call last):
+ ...
+InvalidLength: ...
+>>> format('49098576')
+'49-098-576'
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits
+
+
+def compact(number):
+ """Convert the number to the minimal representation."""
+ number = clean(number, ' -').upper().strip()
+ if number.startswith('NZ'):
+ return number[2:]
+ return number
+
+
+def calc_check_digit(number):
+ """Calculate the check digit.
+
+ The number passed should not have the check digit included.
+ """
+ primary_weights = (3, 2, 7, 6, 5, 4, 3, 2)
+ secondary_weights = (7, 4, 3, 2, 5, 2, 7, 6)
+ # pad with leading zeros
+ number = (8 - len(number)) * '0' + number
+ s = -sum(w * int(n) for w, n in zip(primary_weights, number)) % 11
+ if s != 10:
+ return str(s)
+ s = -sum(w * int(n) for w, n in zip(secondary_weights, number)) % 11
+ return str(s)
+
+
+def validate(number):
+ """Check if the number is a valid IRD number."""
+ number = compact(number)
+ if len(number) not in (8, 9):
+ raise InvalidLength()
+ if not isdigits(number):
+ raise InvalidFormat()
+ if not 10000000 < int(number) < 150000000:
+ raise InvalidComponent()
+ if number[-1] != calc_check_digit(number[:-1]):
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Check if the number is a valid IRD number."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
+
+
+def format(number):
+ """Reformat the number to the standard presentation format."""
+ number = compact(number)
+ return '-'.join([number[:-6], number[-6:-3], number[-3:]])
diff --git a/tests/test_nz_ird.doctest b/tests/test_nz_ird.doctest
new file mode 100644
index 0000000..aa0dff3
--- /dev/null
+++ b/tests/test_nz_ird.doctest
@@ -0,0 +1,68 @@
+test_nz_ird.doctest - more detailed tests for stdnum.nz.ird
+
+Copyright (C) 2019 Leandro Regueiro
+Copyright (C) 2019 Arthur de Jong
+
+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.nz.ird module.
+
+>>> from stdnum.nz import ird
+>>> from stdnum.exceptions import *
+
+
+This is a selection of numbers (which should be valid) found at
+https://www.ird.govt.nz/-/media/Project/IR/PDF/2020RWTNRWTSpecificationDocumentv10.pdf
+
+>>> ird.validate('49091850')
+'49091850'
+>>> ird.validate('136410133')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> ird.validate('9125568')
+Traceback (most recent call last):
+ ...
+InvalidLength: ...
+
+
+Test for corner cases.
+
+>>> ird.validate('1234567A')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> ird.validate('150000001')
+Traceback (most recent call last):
+ ...
+InvalidComponent: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 047-179-025
+... 136410132
+... 35901981
+... 49098576
+... 53-710-220
+... 61-631-852
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not ird.is_valid(x)]
+[]
-----------------------------------------------------------------------
Summary of changes:
stdnum/nz/ird.py | 102 +++++++++++++++++++++
...est_eu_banknote.doctest => test_nz_ird.doctest} | 61 ++++++------
2 files changed, 130 insertions(+), 33 deletions(-)
create mode 100644 stdnum/nz/ird.py
copy tests/{test_eu_banknote.doctest => test_nz_ird.doctest} (53%)
hooks/post-receive
--
python-stdnum
- python-stdnum branch master updated. 1.11-14-g7211ccb,
Commits of the python-stdnum project