lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.13-36-gdab926c

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

python-stdnum branch master updated. 1.13-36-gdab926c



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  dab926cfd86b6dc85f688de59b398ca98320b577 (commit)
      from  291b8311898b103d5d6c0e38fd2ae3dcde48c4c1 (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=dab926cfd86b6dc85f688de59b398ca98320b577

commit dab926cfd86b6dc85f688de59b398ca98320b577
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Sun Apr 5 20:30:43 2020 +0200

    Add support for Taiwan TIN number
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/214
    Closes https://github.com/arthurdejong/python-stdnum/issues/209

diff --git a/stdnum/tw/__init__.py b/stdnum/tw/__init__.py
new file mode 100644
index 0000000..d79cc3d
--- /dev/null
+++ b/stdnum/tw/__init__.py
@@ -0,0 +1,24 @@
+# __init__.py - collection of Taiwanese numbers
+# coding: utf-8
+#
+# Copyright (C) 2020 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 Taiwanese numbers."""
+
+# provide aliases
+from stdnum.tw import ubn as vat  # noqa: F401
diff --git a/stdnum/tw/ubn.py b/stdnum/tw/ubn.py
new file mode 100644
index 0000000..efae740
--- /dev/null
+++ b/stdnum/tw/ubn.py
@@ -0,0 +1,93 @@
+# ubn.py - functions for handling Taiwanese UBN numbers
+# coding: utf-8
+#
+# Copyright (C) 2020 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
+
+"""UBN (Unified Business Number, 統一編號, Taiwanese tax number).
+
+The Unified Business Number (UBN, 統一編號) is the number assigned to businesses
+within Taiwan for tax (VAT) purposes. The number consists of 8 digits, the
+last being a check digit.
+
+More information:
+
+* https://zh.wikipedia.org/wiki/統一編號
+* https://findbiz.nat.gov.tw/fts/query/QueryBar/queryInit.do?request_locale=en
+
+>>> validate('00501503')
+'00501503'
+>>> validate('00501502')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> format(' 0050150 3 ')
+'00501503'
+"""
+
+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 calc_checksum(number):
+    """Calculate the checksum over the number."""
+    # convert to numeric first, then sum individual digits
+    weights = (1, 2, 1, 2, 1, 2, 4, 1)
+    number = ''.join(str(w * int(n)) for w, n in zip(weights, number))
+    return sum(int(n) for n in number) % 10
+
+
+def validate(number):
+    """Check if the number is a valid Taiwan UBN number.
+
+    This checks the length, formatting and check digit.
+    """
+    number = compact(number)
+    if len(number) != 8:
+        raise InvalidLength()
+    if not isdigits(number):
+        raise InvalidFormat()
+    checksum = calc_checksum(number)
+    if not (checksum == 0 or (checksum == 9 and number[6] == '7')):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid Taiwan UBN 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_tw_ubn.doctest b/tests/test_tw_ubn.doctest
new file mode 100644
index 0000000..127f636
--- /dev/null
+++ b/tests/test_tw_ubn.doctest
@@ -0,0 +1,208 @@
+test_tw_ubn.doctest - more detailed doctests for stdnum.tw.ubn module
+
+Copyright (C) 2020 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.tw.ubn module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.tw import ubn
+
+
+Tests for some corner cases.
+
+>>> ubn.validate('00277104')
+'00277104'
+>>> ubn.validate(' 0027710-4 ')
+'00277104'
+>>> ubn.format(' 0027710-4 ')
+'00277104'
+>>> ubn.validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> ubn.validate('FF277104')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> ubn.validate('00277103')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 00501503
+... 00508203
+... 00520500
+... 00965067
+... 00992156
+... 01006018
+... 01012715
+... 01013257
+... 01031918
+... 01671049
+... 01819129
+... 02255149
+... 02612809
+... 02695934
+... 02810218
+... 02810370
+... 02810478
+... 02811286
+... 02811574
+... 03729709
+... 03731705
+... 03734203
+... 03734246
+... 03734301
+... 03735202
+... 03737200
+... 03753600
+... 03787502
+... 03807654
+... 04173812
+... 05600361
+... 06313774
+... 06476734
+... 08149429
+... 08551977
+... 09561093
+... 11337775
+... 12800766
+... 14293457
+... 21100531
+... 22822281
+... 24718587
+... 24932469
+... 25329369
+... 27940723
+... 28148905
+... 28674499
+... 29116418
+... 29137568
+... 40407602
+... 45000103
+... 45888690
+... 46804706
+... 46804804
+... 50872698
+... 52011008
+... 52020800
+... 52697649
+... 53020997
+... 53084078
+... 53235414
+... 53412564
+... 54381049
+... 54663032
+... 54664996
+... 55508115
+... 57301337
+... 57302906
+... 58815405
+... 59372104
+... 60004108
+... 60004304
+... 61611307
+... 61611405
+... 61611502
+... 64004306
+... 64967512
+... 66018608
+... 66019206
+... 66019304
+... 69115908
+... 69116202
+... 69116408
+... 69116701
+... 71802305
+... 71803607
+... 71804508
+... 73761420
+... 76001900
+... 77495542
+... 78962740
+... 80158777
+... 84846045
+... 86521248
+... 87402608
+... 87814088
+... 88504104
+... 88507003
+... 89398405
+... 91003907
+... 91004005
+... 91605801
+... 91746936
+... 93504202
+... 93504408
+... 95927022
+... 99033073
+... 99326850
+... 99595290
+... 99595989
+... 99596220
+... 99596468
+... 99651948
+... 99653306
+... 99663965
+... 99665979
+... 99667982
+... 99668027
+... 99668336
+... 99669193
+... 99685399
+... 99688662
+... 99696641
+... 99736030
+... 99745468
+... 99749204
+... 99756176
+... 99757338
+... 99786158
+... 99809976
+... 99830984
+... 99833899
+... 99841053
+... 99842031
+... 99848387
+... 99849512
+... 99853521
+... 99855648
+... 99856230
+... 99861923
+... 99879748
+... 99884812
+... 99892600
+... 99894425
+... 99904177
+... 99924856
+... 99931596
+... 99936918
+... 99937037
+... 99940188
+... 99963479
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not ubn.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/{sg => tw}/__init__.py   |   6 +-
 stdnum/{za/tin.py => tw/ubn.py} |  52 +++++-----
 tests/test_tw_ubn.doctest       | 208 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 241 insertions(+), 25 deletions(-)
 copy stdnum/{sg => tw}/__init__.py (85%)
 copy stdnum/{za/tin.py => tw/ubn.py} (56%)
 create mode 100644 tests/test_tw_ubn.doctest


hooks/post-receive
-- 
python-stdnum