lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.11-9-g4ac84c5

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

python-stdnum branch master updated. 1.11-9-g4ac84c5



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  4ac84c50b240a1c9a41f04761c49547a07ebe856 (commit)
      from  a6521e6fe0e092e24b543dc1e0540cef0a46f16a (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=4ac84c50b240a1c9a41f04761c49547a07ebe856

commit 4ac84c50b240a1c9a41f04761c49547a07ebe856
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun May 12 23:34:34 2019 +0200

    Add Venezuelan RIF (VAT number)
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/97

diff --git a/stdnum/ve/__init__.py b/stdnum/ve/__init__.py
new file mode 100644
index 0000000..83223b3
--- /dev/null
+++ b/stdnum/ve/__init__.py
@@ -0,0 +1,24 @@
+# __init__.py - collection of Venezuelan numbers
+# coding: utf-8
+#
+# 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
+
+"""Collection of Venezuelan numbers."""
+
+# provide aliases
+from stdnum.ve import rif as vat  # noqa: F401
diff --git a/stdnum/ve/rif.py b/stdnum/ve/rif.py
new file mode 100644
index 0000000..2ed3c9f
--- /dev/null
+++ b/stdnum/ve/rif.py
@@ -0,0 +1,87 @@
+# rif.py - functions for handling Venezuelan VAT numbers
+# coding: utf-8
+#
+# 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
+
+"""RIF (Registro de Identificación Fiscal, Venezuelan VAT number).
+
+The Registro de Identificación Fiscal (RIF) is the Venezuelan fiscal
+registration number. The number consists of 10 digits where the first digit
+denotes the type of number (person, company or government) and the last digit
+is a check digit.
+
+>>> validate('V-11470283-4')
+'V114702834'
+>>> validate('V-11470283-3')  # invalid check digit
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+"""
+
+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()
+
+
+# Known number types and their corresponding value in the check
+# digit calculation
+_company_types = {
+    'V': 4,   # natural person born in Venezuela
+    'E': 8,   # foreign natural person
+    'J': 12,  # company
+    'P': 16,  # passport
+    'G': 20,  # government
+}
+
+
+def calc_check_digit(number):
+    """Calculate the check digit for the RIF."""
+    number = compact(number)
+    weights = (3, 2, 7, 6, 5, 4, 3, 2)
+    c = _company_types[number[0]]
+    c += sum(w * int(n) for w, n in zip(weights, number[1:9]))
+    return '00987654321'[c % 11]
+
+
+def validate(number):
+    """Check if the number provided is a valid RIF. This checks the length,
+    formatting and check digit."""
+    number = compact(number)
+    if len(number) != 10:
+        raise InvalidLength()
+    if number[0] not in _company_types:
+        raise InvalidComponent()
+    if not isdigits(number[1:]):
+        raise InvalidFormat()
+    if number[-1] != calc_check_digit(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number provided is a valid RIF. This checks the length,
+    formatting and check digit."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_ve_rif.doctest b/tests/test_ve_rif.doctest
new file mode 100644
index 0000000..ce4eec3
--- /dev/null
+++ b/tests/test_ve_rif.doctest
@@ -0,0 +1,258 @@
+test_ve_nitp.doctest - more detailed doctests stdnum.ve.rif
+
+Copyright (C) 2015-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.ve.rif module.
+
+>>> from stdnum.ve import rif
+>>> from stdnum.exceptions import *
+
+
+Test a few corner cases.
+
+>>> rif.validate('V-11470283-4')
+'V114702834'
+>>> rif.validate('V-11470283-3')  # invalid check digit
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> rif.validate('V-114702839-4')  # too long
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> rif.validate('Z-11470283-4')  # invalid first digit
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> rif.validate('V-11A70283-4')  # not a digit
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+These have been found online and should all be valid numbers. An online
+validation service is available at
+http://contribuyente.seniat.gob.ve/BuscaRif/BuscaRif.jsp
+
+>>> numbers = '''
+...
+... E810812535
+... E820238691
+... E834220159
+... G-20000085-6
+... G200000023
+... G200000031
+... G200001496
+... G200001534
+... G200001941
+... G200002123
+... G200003030
+... G200008075
+... G20000826-1
+... G200008261
+... G200010320
+... G200010843
+... G200013761
+... G200014288
+... G200015969
+... G200016639
+... G200017643
+... G200018232
+... G200047461
+... G200081473
+... G200081988
+... G200088060
+... G200088346
+... G200088494
+... G200088893
+... G200089148
+... G200089202
+... G200093811
+... G200094257
+... G200094451
+... G200094907
+... G200095253
+... G200100141
+... J-00004793-6
+... J-00051895-5
+... J-07540999-0
+... J-08507040-0
+... J-29372153-9
+... J-29485175-4
+... J-29573388-7
+... J-30022035-4
+... J-30023227-1
+... J-30360145-6
+... J-30816256-6
+... J-30927229-2
+... J-30935829-4
+... J-30953416-5
+... J-31176347-3
+... J-31228443-9
+... J-31330629-0
+... J-31424055-2
+... J-31478723-3
+... J-31511979-0
+... J-31679729-5
+... J-31727846-1
+... J-40099327-0
+... J-40162851-6
+... J-40315878-9
+... J-40546341-4
+... J-40573653-4
+... J-40748167-3
+... J-40807479-6
+... J-40821643-4
+... J-40953532-0
+... J-40994900-1
+... J000004676
+... J000089337
+... J000760977
+... J001255524
+... J002477474
+... J002662239
+... J002972734
+... J075037987
+... J075050169
+... J075053354
+... J075077733
+... J075113527
+... J075213530
+... J075216954
+... J075429486
+... J075457323
+... J075607945
+... J075757661
+... J075831250
+... J075872266
+... J075887948
+... J293873886
+... J293913101
+... J294607730
+... J294625967
+... J295403925
+... J295479930
+... J295486308
+... J295635613
+... J296390088
+... J296630127
+... J296932565
+... J297362460
+... J298254041
+... J298284307
+... J298300949
+... J298390336
+... J298570172
+... J29867813-5
+... J299505048
+... J300926966
+... J301280199
+... J301339193
+... J30136953-0
+... J301392019
+... J301554841
+... J301873947
+... J302027861
+... J302037018
+... J302065623
+... J302410681
+... J302447410
+... J302453640
+... J30248707-2
+... J302527678
+... J302934141
+... J303758720
+... J304509367
+... J304577494
+... J304798350
+... J305110794
+... J305183465
+... J305258392
+... J305403171
+... J305706433
+... J305941483
+... J305979154
+... J306728520
+... J306953558
+... J307331780
+... J307573694
+... J307584947
+... J308192252
+... J308357219
+... J309233025
+... J309520512
+... J309715038
+... J310034168
+... J310308268
+... J310650322
+... J310948968
+... J311397159
+... J311999310
+... J312306009
+... J313032735
+... J313387819
+... J313430706
+... J313476366
+... J314565770
+... J315922614
+... J317067584
+... J317328400
+... J400316375
+... J401559352
+... J402153570
+... J403832528
+... P – 000 60157 – 3
+... P-00033812-0
+... V-03707781-6
+... V-07811271-5
+... V-09136615-7
+... V-09136616-5
+... V-10031105-0
+... V-11024023-2
+... V-11470283-4
+... V-11841315-2
+... V-12252850-9
+... V-13271127-1
+... V-17126146-1
+... V-17128754-1
+... V-19676417-4
+... V-20674844-0
+... V-25808097-8
+... V-27327212-3
+... V008215150
+... V021998164
+... V027564794
+... V029000928
+... V034360312
+... V037939567
+... V045512858
+... V072507564
+... V102259498
+... V131527361
+... V135744740
+... V177993626
+... V193631599
+... V244455138
+... V258279570
+... V297721182
+... V725655084
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not rif.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/{cu => ve}/__init__.py    |   7 +-
 stdnum/{md/idno.py => ve/rif.py} |  56 +++++----
 tests/test_ve_rif.doctest        | 258 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 297 insertions(+), 24 deletions(-)
 copy stdnum/{cu => ve}/__init__.py (83%)
 copy stdnum/{md/idno.py => ve/rif.py} (53%)
 create mode 100644 tests/test_ve_rif.doctest


hooks/post-receive
-- 
python-stdnum