python-stdnum branch master updated. 1.13-40-g94e2c63
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.13-40-g94e2c63
- 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.13-40-g94e2c63
- Date: Sun, 2 Aug 2020 18:14:03 +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 94e2c6373e191e26beee6153ef2f99adcf31cf45 (commit)
from 505521e2341306700958d698b8f73a608e687191 (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=94e2c6373e191e26beee6153ef2f99adcf31cf45
commit 94e2c6373e191e26beee6153ef2f99adcf31cf45
Author: Mohammed Salman <mohammed@holvi.com>
Date: Tue Jun 2 15:11:05 2020 +0300
Add English Unique Tax Reference
Closes https://github.com/arthurdejong/python-stdnum/pull/227
diff --git a/stdnum/gb/utr.py b/stdnum/gb/utr.py
new file mode 100644
index 0000000..958c723
--- /dev/null
+++ b/stdnum/gb/utr.py
@@ -0,0 +1,72 @@
+# upn.py - functions for handling English UTRs
+#
+# Copyright (C) 2020 Holvi Payment Services
+# Copyright (C) 2020 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
+
+"""UTR (United Kingdom Unique Taxpayer Reference).
+
+A UTR (unique taxpayer reference) is a 10 digit number used to identify UK
+taxpayers who have to submit a tax return.
+
+More information:
+
+*
https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/UK-TIN.pdf
+
+>>> validate('1955839661')
+'1955839661'
+>>> validate('2955839661')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ..
+"""
+
+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().lstrip('K')
+
+
+def calc_check_digit(number):
+ """Calculate the check digit for the number. The passed number should not
+ have the check digit (the first one) included."""
+ weights = (6, 7, 8, 9, 10, 5, 4, 3, 2)
+ return '21987654321'[sum(int(n) * w for n, w in zip(number, weights)) % 11]
+
+
+def validate(number):
+ """Check if the number is a valid UTR."""
+ number = compact(number)
+ if not isdigits(number):
+ raise InvalidFormat()
+ if not len(number) == 10:
+ raise InvalidLength()
+ if number[0] != calc_check_digit(number[1:]):
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Check if the number is a valid UTR."""
+ try:
+ return bool(validate(number))
+ except ValidationError:
+ return False
diff --git a/tests/test_gb_utr.doctest b/tests/test_gb_utr.doctest
new file mode 100644
index 0000000..93637cc
--- /dev/null
+++ b/tests/test_gb_utr.doctest
@@ -0,0 +1,46 @@
+test_gb_utr.doctest - more detailed doctests for stdnum.gb.utr module
+
+Copyright (C) 2020 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.gb.utr module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.gb import utr
+
+
+These numbers have been generated but have been confirmed to be correct using
+a different validator (they have not been confirmed to be assigned).
+
+>>> numbers = '''
+...
+... 1234567895
+... 1478641088
+... 1955839661
+... 2172858530
+... 2234567890
+... 4748949890
+... 5179265754
+... 5816619590
+... 8258106771
+... 9624088948
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not utr.is_valid(x)]
+[]
-----------------------------------------------------------------------
Summary of changes:
stdnum/{md/idno.py => gb/utr.py} | 43 ++++++++++----------
tests/{test_ean.doctest => test_gb_utr.doctest} | 53 +++++++++++++------------
2 files changed, 48 insertions(+), 48 deletions(-)
copy stdnum/{md/idno.py => gb/utr.py} (57%)
copy tests/{test_ean.doctest => test_gb_utr.doctest} (54%)
hooks/post-receive
--
python-stdnum
- python-stdnum branch master updated. 1.13-40-g94e2c63,
Commits of the python-stdnum project