lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.16-20-g1a0e613

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

python-stdnum branch master updated. 1.16-20-g1a0e613



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  1a0e61332c5bd8c517f868f68050bbc416051abf (commit)
      from  80714444f92f7c0a80118b7fdf64537c5b9975b7 (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=1a0e61332c5bd8c517f868f68050bbc416051abf

commit 1a0e61332c5bd8c517f868f68050bbc416051abf
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun Sep 19 17:21:44 2021 +0200

    Fix detection of natural RUC values
    
    A natural RUC is the CI plus an establishment number. Both the natural
    RUC and the public RUC can have a third digit with the value 6.
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/267

diff --git a/stdnum/ec/ruc.py b/stdnum/ec/ruc.py
index 6d0c961..c68bf0f 100644
--- a/stdnum/ec/ruc.py
+++ b/stdnum/ec/ruc.py
@@ -2,7 +2,7 @@
 # coding: utf-8
 #
 # Copyright (C) 2014 Jonathan Finlay
-# Copyright (C) 2014-2015 Arthur de Jong
+# Copyright (C) 2014-2021 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
@@ -53,6 +53,32 @@ def _checksum(number, weights):
     return sum(w * int(n) for w, n in zip(weights, number)) % 11
 
 
+def _validate_natural(number):
+    """Check if the number is a valid natural RUC (CI plus establishment)."""
+    if number[-3:] == '000':
+        raise InvalidComponent()  # establishment number wrong
+    ci.validate(number[:10])
+    return number
+
+
+def _validate_public(number):
+    """Check if the number is a valid public RUC."""
+    if number[-4:] == '0000':
+        raise InvalidComponent()  # establishment number wrong
+    if _checksum(number[:9], (3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
+        raise InvalidChecksum()
+    return number
+
+
+def _validate_juridical(number):
+    """Check if the number is a valid juridical RUC."""
+    if number[-3:] == '000':
+        raise InvalidComponent()  # establishment number wrong
+    if _checksum(number[:10], (4, 3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
+        raise InvalidChecksum()
+    return number
+
+
 def validate(number):
     """Check if the number provided is a valid RUC number. This checks the
     length, formatting, check digit and check sum."""
@@ -65,21 +91,16 @@ def validate(number):
         raise InvalidComponent()  # invalid province code
     if number[2] < '6':
         # 0..5 = natural RUC: CI plus establishment number
-        if number[-3:] == '000':
-            raise InvalidComponent()  # establishment number wrong
-        ci.validate(number[:10])
+        _validate_natural(number)
     elif number[2] == '6':
-        # 6 = public RUC
-        if number[-4:] == '0000':
-            raise InvalidComponent()  # establishment number wrong
-        if _checksum(number[:9], (3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
-            raise InvalidChecksum()
+        # 6 = public RUC (or natural RUC)
+        try:
+            _validate_public(number)
+        except ValidationError:
+            _validate_natural(number)
     elif number[2] == '9':
         # 9 = juridical RUC
-        if number[-3:] == '000':
-            raise InvalidComponent()  # establishment number wrong
-        if _checksum(number[:10], (4, 3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
-            raise InvalidChecksum()
+        _validate_juridical(number)
     else:
         raise InvalidComponent()  # third digit wrong
     return number
diff --git a/tests/test_ec_ruc.doctest b/tests/test_ec_ruc.doctest
index 1bdaebf..2483a44 100644
--- a/tests/test_ec_ruc.doctest
+++ b/tests/test_ec_ruc.doctest
@@ -1,7 +1,7 @@
 test_ec_ruc.doctest - more detailed doctests for stdnum.ec.ruc module
 
 Copyright (C) 2014 Jonathan Finlay
-Copyright (C) 2014-2015 Arthur de Jong
+Copyright (C) 2014-2021 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
@@ -27,13 +27,15 @@ as module documentation.
 >>> from stdnum.exceptions import *
 
 
-Normal natural RUC values (third digit less than 6) that should just work.
+Normal natural RUC values (third digit less than or equal to 6) that should
+just work.
 
 >>> numbers = '''
 ... 0101016905001
 ... 0602910945001
 ... 0910005917001
 ... 0926687856001
+... 0962467429001
 ... 1001152287001
 ... 1102755442001
 ... 1104552037001
@@ -127,6 +129,7 @@ Normal public RUC values (third digit is 6) that should 
just work.
 ... 0960006420001
 ... 0968529830001
 ... 0968566440001
+... 0968599020001
 ... 1060000180001
 ... 1060000260001
 ... 1060000340001

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

Summary of changes:
 stdnum/ec/ruc.py          | 47 ++++++++++++++++++++++++++++++++++-------------
 tests/test_ec_ruc.doctest |  7 +++++--
 2 files changed, 39 insertions(+), 15 deletions(-)


hooks/post-receive
-- 
python-stdnum