lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.6-12-gb8389eb

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

python-stdnum branch master updated. 1.6-12-gb8389eb



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  b8389ebd405f74bdbaa1c09c91b4a816fcd0873b (commit)
       via  efd2eb98ed51c0866b4f206e93cbc1bc1691ee5a (commit)
       via  b8e12d631eec0b000f74d813f6a7d570187c67f5 (commit)
      from  0c91b4302ca4019ab1ea7c023b592c177dddc4fe (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=b8389ebd405f74bdbaa1c09c91b4a816fcd0873b

commit b8389ebd405f74bdbaa1c09c91b4a816fcd0873b
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat Aug 26 23:44:00 2017 +0200

    Add Canadian Business Number (BN)

diff --git a/stdnum/ca/bn.py b/stdnum/ca/bn.py
new file mode 100644
index 0000000..37d44d9
--- /dev/null
+++ b/stdnum/ca/bn.py
@@ -0,0 +1,80 @@
+# bn.py - functions for handling Canadian Business Numbers (BNs)
+#
+# Copyright (C) 2017 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
+
+"""BN (Canadian Business Number).
+
+A Business Number (BN) is a 9-digit identification number for businesses
+issued by the Canada Revenue Agency for tax purposes. The 9-digit number can
+be followed by two letters (program identifier) and 4 digits (reference
+number) to form a program account (or BN15).
+
+More information:
+
+* https://www.canada.ca/en/services/taxes/business-number.html
+* 
https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/fdrlCrpSrch.html?locale=en_CA/
+
+>>> validate('12302 6635')
+'123026635'
+>>> validate('12302 6635 RC 0001')
+'123026635RC0001'
+>>> validate('123456783')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('12345678Z')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+"""
+
+from stdnum import luhn
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+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 validate(number):
+    """Checks to see if the number provided is a valid BN or BN15. This
+    checks the length, formatting and check digit."""
+    number = compact(number)
+    if len(number) not in (9, 15):
+        raise InvalidLength()
+    if not number[:9].isdigit():
+        raise InvalidFormat()
+    luhn.validate(number[:9])
+    if len(number) == 15:
+        if number[9:11] not in ('RC', 'RM', 'RP', 'RT'):
+            raise InvalidComponent()
+        if not number[11:].isdigit():
+            raise InvalidFormat()
+    return number
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid BN or BN15. This
+    checks the length, formatting and check digit."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_ca_bn.doctest b/tests/test_ca_bn.doctest
new file mode 100644
index 0000000..c28a627
--- /dev/null
+++ b/tests/test_ca_bn.doctest
@@ -0,0 +1,151 @@
+test_ca_bn.doctest - more detailed doctests for the stdnum.ca.bn module
+
+Copyright (C) 2017 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.ca.bn module.
+
+>>> from stdnum.ca import bn
+>>> from stdnum.exceptions import *
+
+
+The number can also be a 15-digit account number (Business Number followed by
+program identifier and reference number).
+
+>>> bn.validate('12302 6635')
+'123026635'
+>>> bn.validate('12302 6635 RC 0001')
+'123026635RC0001'
+>>> bn.validate('12302 6635 AA 0001')  # only some values allowed
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> bn.validate('12302 6635 RC OOO1')  # only digits allowed in ref nr
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 000089987RC1818
+... 100007780RC0001
+... 100026723RC0001
+... 100044833RC0001
+... 100060474RC0001
+... 100074699RC0001
+... 100078294RC0002
+... 100080571RC0001
+... 100287119RC0001
+... 100457266RC0001
+... 101007961RC0001
+... 101060390RC0001
+... 123025645RC0001
+... 123026635RC0001
+... 123222911RC0001
+... 123828493RC0001
+... 700706898RC0001
+... 700783491RC0001
+... 700784499RC0001
+... 700803299RC0001
+... 700859895RC0001
+... 702520891RC0001
+... 702521097RC0001
+... 702529496RC0001
+... 702667122RC0001
+... 751055724RC0001
+... 751119520RC0001
+... 751207697RC0001
+... 751369729RC0001
+... 751446725RC0001
+... 751542895RC0001
+... 751551490RC0001
+... 751756529RC0001
+... 752573725RC0001
+... 752574525RC0001
+... 752628297RC0001
+... 752767327RC0001
+... 752768127RC0001
+... 752805291RC0001
+... 752828293RC0001
+... 752860320RC0001
+... 752944892RC0001
+... 753000298RC0001
+... 753371897RC0001
+... 753372291RC0001
+... 753447127RC0001
+... 753461292RC0001
+... 753946698RC0001
+... 789422128RC0001
+... 789482494RC0001
+... 789538923RC0001
+... 789634326RC0001
+... 795578699RC0001
+... 795606490RC0001
+... 795676121RC0001
+... 795710920RC0001
+... 795848126RC0001
+... 795855527RC0001
+... 795930726RC0001
+... 800500001RC0001
+... 800810657RC0001
+... 800812885RC0001
+... 800826489RC0001
+... 800958118RC0001
+... 800973406RC0001
+... 852093749RC0001
+... 852470673RC0001
+... 852581149RC0001
+... 852615772RC0001
+... 852646900RC0001
+... 852694231RC0001
+... 852750546RC0001
+... 852988633RC0001
+... 855065504RC0001
+... 855102976RC0001
+... 855582995RC0001
+... 855643086RC0001
+... 855696373RC0001
+... 855789004RC0001
+... 855957882RC0001
+... 859043580RC0001
+... 859098337RC0001
+... 859144438RC0001
+... 859363848RC0001
+... 859395162RC0001
+... 859457681RC0001
+... 859620973RC0001
+... 892190364RC0001
+... 892220393RC0002
+... 892462672RC0001
+... 892476565RC0001
+... 892552035RC0001
+... 892737149RC0001
+... 892738741RC0001
+... 892807983RC0001
+... 899099733RC0001
+... 899429443RC0001
+... 899549042RC0001
+... 899558340RC0001
+... 899927438RC0001
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not bn.is_valid(x)]
+[]

https://arthurdejong.org/git/python-stdnum/commit/?id=efd2eb98ed51c0866b4f206e93cbc1bc1691ee5a

commit efd2eb98ed51c0866b4f206e93cbc1bc1691ee5a
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sat Aug 26 22:37:17 2017 +0200

    Add Canadian Social Insurance Number (SIN)

diff --git a/stdnum/ca/__init__.py b/stdnum/ca/__init__.py
new file mode 100644
index 0000000..bb4db36
--- /dev/null
+++ b/stdnum/ca/__init__.py
@@ -0,0 +1,21 @@
+# __init__.py - collection of Canadian numbers
+# coding: utf-8
+#
+# Copyright (C) 2017 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
+
+"""Collection of Canadian numbers."""
diff --git a/stdnum/ca/sin.py b/stdnum/ca/sin.py
new file mode 100644
index 0000000..59e8713
--- /dev/null
+++ b/stdnum/ca/sin.py
@@ -0,0 +1,80 @@
+# sin.py - functions for handling Canadian Social Insurance Numbers (SINs)
+#
+# Copyright (C) 2017 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
+
+"""SIN (Canadian Social Insurance Number).
+
+The Social Insurance Number (SIN) is a 9-digit identifier issued to
+individuals for various government programs. SINs that begin with a 9 are
+issued to temporary workers who are neither Canadian citizens nor permanent
+residents.
+
+More information:
+
+* https://www.canada.ca/en/employment-social-development/services/sin.html
+* https://en.wikipedia.org/wiki/Social_Insurance_Number
+
+>>> validate('123-456-782')
+'123456782'
+>>> validate('999-999-999')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('12345678Z')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> format('123456782')
+'123-456-782'
+"""
+
+from stdnum import luhn
+from stdnum.exceptions import *
+from stdnum.util import clean
+
+
+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 validate(number):
+    """Checks to see if the number provided is a valid SIN. This checks the
+    length, formatting and check digit."""
+    number = compact(number)
+    if len(number) != 9:
+        raise InvalidLength()
+    if not number.isdigit():
+        raise InvalidFormat()
+    return luhn.validate(number)
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid SIN. This checks the
+    length, formatting and check digit."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number):
+    """Reformat the passed number to the standard format."""
+    number = compact(number)
+    return '-'.join((number[0:3], number[3:6], number[6:]))

https://arthurdejong.org/git/python-stdnum/commit/?id=b8e12d631eec0b000f74d813f6a7d570187c67f5

commit b8e12d631eec0b000f74d813f6a7d570187c67f5
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Thu Aug 24 23:39:29 2017 +0200

    Ensure all Python files are in coverage report

diff --git a/setup.cfg b/setup.cfg
index 106690c..501bdbc 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -9,6 +9,7 @@ doctest-options=+IGNORE_EXCEPTION_DETAIL
 with-coverage=true
 cover-branches=true
 cover-package=stdnum
+cover-inclusive=true
 cover-erase=true
 cover-html=true
 cover-html-dir=coverage

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

Summary of changes:
 setup.cfg                       |   1 +
 stdnum/{gr => ca}/__init__.py   |   6 +-
 stdnum/{ec/ci.py => ca/bn.py}   |  60 ++++++++--------
 stdnum/{do/rnc.py => ca/sin.py} |  55 ++++++++-------
 tests/test_ca_bn.doctest        | 151 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 214 insertions(+), 59 deletions(-)
 copy stdnum/{gr => ca}/__init__.py (86%)
 copy stdnum/{ec/ci.py => ca/bn.py} (54%)
 copy stdnum/{do/rnc.py => ca/sin.py} (58%)
 create mode 100644 tests/test_ca_bn.doctest


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
https://lists.arthurdejong.org/python-stdnum-commits/