lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.11-20-g8c1015a

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

python-stdnum branch master updated. 1.11-20-g8c1015a



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  8c1015a3623762375e5231df84af68ac6a21e837 (commit)
      from  e1ea8db04fb9754b9fc65a7432d088a892110a94 (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=8c1015a3623762375e5231df84af68ac6a21e837

commit 8c1015a3623762375e5231df84af68ac6a21e837
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Sun Jun 9 19:09:37 2019 +0200

    Add Paraguay RUC number
    
    This supports RUC number validation of rphysical persons, non-juridical
    persons and foreigners.
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/122
    Closes https://github.com/arthurdejong/python-stdnum/pull/123

diff --git a/stdnum/py/__init__.py b/stdnum/py/__init__.py
new file mode 100644
index 0000000..b7916f7
--- /dev/null
+++ b/stdnum/py/__init__.py
@@ -0,0 +1,24 @@
+# __init__.py - collection of Paraguayan 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
+
+"""Collection of Paraguayan numbers."""
+
+# provide aliases
+from stdnum.py import ruc as vat  # noqa: F401
diff --git a/stdnum/py/ruc.py b/stdnum/py/ruc.py
new file mode 100644
index 0000000..0870b66
--- /dev/null
+++ b/stdnum/py/ruc.py
@@ -0,0 +1,101 @@
+# rut.py - functions for handling Paraguay RUC 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
+
+"""RUC number (Registro Único de Contribuyentes, Paraguay tax number).
+
+The Registro Único del Contribuyente (RUC) is the unique taxpayer registry
+that maintains identification numbers for all persons (national or foreign)
+and legal entities in Paraguay.
+
+The RUC number for legal entities consists of 8 digits starting after
+80000000. Number for residents and foreigners are up to 9 digits. The last
+digit is a check digit.
+
+More information:
+
+* https://www.ruc.com.py/
+
+>>> validate('80028061-0')
+'800280610'
+>>> validate('9991603')
+'9991603'
+>>> validate('2660-3')
+'26603'
+>>> validate('800532492')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('80123456789')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> format('800000358')
+'80000035-8'
+"""
+
+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()
+
+
+def calc_check_digit(number):
+    """Calculate the check digit.
+
+    The number passed should not have the check digit included.
+    """
+    s = sum((i + 2) * int(n) for i, n in enumerate(reversed(number)))
+    return str((-s % 11) % 10)
+
+
+def validate(number):
+    """Check if the number is a valid Paraguay RUC number.
+
+    This checks the length, formatting and check digit.
+    """
+    number = compact(number)
+    if len(number) > 9:
+        raise InvalidLength()
+    if not isdigits(number):
+        raise InvalidFormat()
+    if number[-1] != calc_check_digit(number[:-1]):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid Paraguay RUC 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[:-1], number[-1]])
diff --git a/tests/test_py_ruc.doctest b/tests/test_py_ruc.doctest
new file mode 100644
index 0000000..baa0843
--- /dev/null
+++ b/tests/test_py_ruc.doctest
@@ -0,0 +1,261 @@
+test_py_ruc.doctest - more detailed doctests for stdnum.py.ruc module
+
+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
+
+
+This file contains more detailed doctests for the stdnum.py.ruc module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.py import ruc
+
+
+Tests for some corner cases.
+
+>>> ruc.validate('800280610')
+'800280610'
+>>> ruc.validate('80000020-0')
+'800000200'
+>>> ruc.format('800001907')
+'80000190-7'
+>>> ruc.validate('9991603')
+'9991603'
+>>> ruc.validate('2660-3')
+'26603'
+>>> ruc.validate('80123456785')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> ruc.validate('FF8002121')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> ruc.validate('80021744-8')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 1068460-3
+... 1070600-3
+... 1075150-5
+... 1086170-0
+... 1120880-5
+... 1152390-5
+... 116840-1
+... 1206910-8
+... 1218000-9
+... 1218400-4
+... 1238600-6
+... 1249160-8
+... 1267110-0
+... 1280950-0
+... 1371610-7
+... 1381630-6
+... 1393200-4
+... 1420870-9
+... 1449540-6
+... 1494600-9
+... 1572440-9
+... 1588230-6
+... 1616440-7
+... 176330-0
+... 1820310-8
+... 1820920-3
+... 1839520-1
+... 1848060-8
+... 1934740-5
+... 2007510-3
+... 2012640-9
+... 2042780-8
+... 2048860-2
+... 2049750-4
+... 2059680-4
+... 2157230-5
+... 2195990-0
+... 2218790-1
+... 2224970-2
+... 2299400-9
+... 2345940-9
+... 2356810-0
+... 2366360-0
+... 2414460-6
+... 244860-2
+... 2498810-3
+... 2513340-3
+... 2634810-1
+... 2863640-6
+... 2874790-9
+... 2889810-9
+... 2908190-4
+... 2951250-6
+... 2997610-3
+... 3196710-8
+... 3206530-2
+... 3330830-6
+... 3387260-0
+... 341160-5
+... 3412130-7
+... 3417980-1
+... 3446170-1
+... 3464340-0
+... 3500120-8
+... 3526270-2
+... 3527940-0
+... 3538680-0
+... 3598580-1
+... 3616260-4
+... 3617350-9
+... 3632060-9
+... 3649750-9
+... 3676210-5
+... 3715270-0
+... 3724170-2
+... 3785560-3
+... 3804540-0
+... 3811470-4
+... 3896270-5
+... 3942870-2
+... 398020-0
+... 3980620-0
+... 3981320-7
+... 3998290-4
+... 4012150-0
+... 4050010-1
+... 410020-4
+... 4107340-1
+... 4116920-4
+... 4179170-3
+... 428770-3
+... 4288350-4
+... 432180-4
+... 4345560-3
+... 4366430-0
+... 440010-0
+... 4416740-7
+... 4421940-7
+... 4507560-3
+... 46470-8
+... 4651020-6
+... 4692880-4
+... 4716240-6
+... 4730250-0
+... 4732250-0
+... 4845310-2
+... 4941300-7
+... 50001080-3
+... 50002520-7
+... 50017800-3
+... 50028750-3
+... 50030670-2
+... 50032280-5
+... 50033340-8
+... 50034850-2
+... 50035860-5
+... 50036990-9
+... 50037400-7
+... 50037650-6
+... 50038920-9
+... 50047020-0
+... 50060170-4
+... 50061570-5
+... 50062160-8
+... 50066800-0
+... 50078920-7
+... 50082020-1
+... 50083320-6
+... 5025370-0
+... 5070050-2
+... 5107680-2
+... 5155780-0
+... 5183950-4
+... 5282210-9
+... 5351470-0
+... 5357880-5
+... 5365190-1
+... 539800-2
+... 545520-0
+... 5621980-6
+... 5632950-4
+... 5714430-3
+... 572080-0
+... 5751190-0
+... 579240-1
+... 579970-8
+... 6232700-3
+... 624910-8
+... 628910-0
+... 6331180-1
+... 685060-0
+... 6914900-3
+... 716120-4
+... 7167830-1
+... 717240-0
+... 786480-9
+... 80005820-8
+... 80006669-3
+... 80006750-9
+... 80011350-0
+... 80017690-1
+... 80019740-2
+... 80020720-3
+... 80022460-4
+... 80027090-8
+... 80031640-1
+... 80034800-1
+... 80034917-2
+... 80040580-3
+... 80048500-9
+... 80050224-8
+... 80051740-7
+... 80056320-4
+... 80056920-2
+... 80060240-4
+... 80060340-0
+... 80061853-0
+... 80061890-4
+... 80065130-8
+... 80065230-4
+... 80065872-8
+... 80071980-8
+... 80072700-2
+... 80075200-7
+... 80078770-6
+... 80084300-2
+... 80084780-6
+... 80086010-1
+... 80088870-7
+... 80097050-0
+... 80101690-8
+... 80102430-7
+... 881220-9
+... 891380-3
+... 897820-4
+... 929240-3
+... 964900-0
+... 989120-0
+... 992100-1
+... 998620-0
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not ruc.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/{uy => py}/__init__.py   |   6 +-
 stdnum/{nz/ird.py => py/ruc.py} |  71 ++++++-----
 tests/test_py_ruc.doctest       | 261 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 299 insertions(+), 39 deletions(-)
 copy stdnum/{uy => py}/__init__.py (85%)
 copy stdnum/{nz/ird.py => py/ruc.py} (50%)
 create mode 100644 tests/test_py_ruc.doctest


hooks/post-receive
-- 
python-stdnum