python-stdnum branch master updated. 1.11-39-g37e6032
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.11-39-g37e6032
- 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-39-g37e6032
- Date: Sun, 13 Oct 2019 17:05:47 +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 37e603204b0a43bc1debb421ffd2da280be4343c (commit)
from 5441ffada1a8ff6e860f5675264d40bfbb470820 (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=37e603204b0a43bc1debb421ffd2da280be4343c
commit 37e603204b0a43bc1debb421ffd2da280be4343c
Author: Alan Hettinger <ahettinger@gmail.com>
Date: Wed Sep 18 11:03:49 2019 -0400
Add Japan Corporate Number
Closes https://github.com/arthurdejong/python-stdnum/pull/157
diff --git a/stdnum/jp/__init__.py b/stdnum/jp/__init__.py
new file mode 100644
index 0000000..12751aa
--- /dev/null
+++ b/stdnum/jp/__init__.py
@@ -0,0 +1,21 @@
+# __init__.py - collection of Japanese numbers
+# coding: utf-8
+#
+# Copyright (C) 2019 Alan Hettinger
+#
+# 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 Japanese numbers."""
diff --git a/stdnum/jp/cn.py b/stdnum/jp/cn.py
new file mode 100644
index 0000000..ff31e35
--- /dev/null
+++ b/stdnum/jp/cn.py
@@ -0,0 +1,86 @@
+# cn.py - functions for handling Japanese Corporate Number (CN)
+# coding: utf-8
+#
+# Copyright (C) 2019 Alan Hettinger
+#
+# 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
+
+"""CN (法人番号, hōjin bangō, Japanese Corporate Number).
+
+The Corporate Number is assigned by the Japanese National Tax Agency to
+identify government organs, public entities, registered corporations and
+other organisations. The number consists of 13 digits where the first digit
+is a non-zero check digit.
+
+More information:
+
+ * https://en.wikipedia.org/wiki/Corporate_Number
+ * https://www.nta.go.jp/taxes/tetsuzuki/mynumberinfo/houjinbangou/
+
+>>> validate('5-8356-7825-6246')
+'5835678256246'
+>>> validate('2-8356-7825-6246')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> format('5835678256246')
+'5-8356-7825-6246'
+"""
+
+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_check_digit(number):
+ """Calculate the check digit. The number passed should not have
+ the check digit included."""
+ weights = (1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
+ s = sum(w * int(n) for w, n in zip(weights, reversed(number))) % 9
+ return str(9 - s)
+
+
+def validate(number):
+ """Check if the number is valid. This checks the length and check
+ digit."""
+ number = compact(number)
+ if len(number) != 13:
+ raise InvalidLength()
+ if not isdigits(number):
+ raise InvalidFormat()
+ if calc_check_digit(number[1:]) != number[0]:
+ raise InvalidChecksum()
+ return number
+
+
+def is_valid(number):
+ """Check if the number is a valid CN."""
+ 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[0], number[1:5], number[5:9], number[9:13]))
diff --git a/tests/test_jp_cn.doctest b/tests/test_jp_cn.doctest
new file mode 100644
index 0000000..e3bf0fe
--- /dev/null
+++ b/tests/test_jp_cn.doctest
@@ -0,0 +1,92 @@
+test_jp_cn.doctest - more detailed doctests for stdnum.jp.cn module
+
+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.jp.cn module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.jp import cn
+
+
+Basic tests
+
+>>> cn.validate('5-8356-7825-6246')
+'5835678256246'
+>>> cn.validate('2-8356-7825-6246')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
+>>> cn.validate('2-aaaa-7825-6246')
+Traceback (most recent call last):
+ ...
+InvalidFormat: ...
+>>> cn.format('5835678256246')
+'5-8356-7825-6246'
+
+
+These numbers should be valid and were randomly generated on
+https://my-number-kun.herokuapp.com/
+
+>>> numbers = '''
+...
+... 1020281079130
+... 1092280840134
+... 1878613755182
+... 2014614971152
+... 2154272766021
+... 2156020645218
+... 2190501102051
+... 2506450764947
+... 2877943595076
+... 3102603003473
+... 3677889020488
+... 3932580304272
+... 4133937187596
+... 4322035819249
+... 4422212699405
+... 4434215752536
+... 4894094830853
+... 5385009332457
+... 5703079264765
+... 5864353627704
+... 6402078819006
+... 6457924394338
+... 6671470548996
+... 6821550923715
+... 6862173718594
+... 7030204178342
+... 7044157771432
+... 7179487563592
+... 7360581582900
+... 7631172336579
+... 7896877317434
+... 8085647298491
+... 8166975505797
+... 8451632537189
+... 8455957905624
+... 8865890851034
+... 8911879336082
+... 9196934167858
+... 9680537896732
+... 9863806292730
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not cn.is_valid(x)]
+[]
-----------------------------------------------------------------------
Summary of changes:
stdnum/{ca => jp}/__init__.py | 6 +--
stdnum/{co/nit.py => jp/cn.py} | 62 +++++++++++++++-------------
tests/test_jp_cn.doctest | 92 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 128 insertions(+), 32 deletions(-)
copy stdnum/{ca => jp}/__init__.py (86%)
copy stdnum/{co/nit.py => jp/cn.py} (56%)
create mode 100644 tests/test_jp_cn.doctest
hooks/post-receive
--
python-stdnum
- python-stdnum branch master updated. 1.11-39-g37e6032,
Commits of the python-stdnum project