lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.18-28-gd0f4c1a

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

python-stdnum branch master updated. 1.18-28-gd0f4c1a



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  d0f4c1a5998b63a76089be5797acff1e489ccd86 (commit)
      from  b8ee83071e63501f2410b9085f53d28c48696025 (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=d0f4c1a5998b63a76089be5797acff1e489ccd86

commit d0f4c1a5998b63a76089be5797acff1e489ccd86
Author: Blaž Bregar <blaz@vexo.systems>
Date:   Fri Jun 30 12:53:07 2023 +0200

    Add Slovenian Corporate Registration Number
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/414

diff --git a/stdnum/si/__init__.py b/stdnum/si/__init__.py
index c6aaf42..98349b8 100644
--- a/stdnum/si/__init__.py
+++ b/stdnum/si/__init__.py
@@ -21,6 +21,7 @@
 
 """Collection of Slovenian numbers."""
 
-# provide vat as an alias
+# provide aliases
 from stdnum.si import ddv as vat  # noqa: F401
 from stdnum.si import emso as personalid  # noqa: F401
+from stdnum.si import maticna as businessid  # noqa: F401
diff --git a/stdnum/si/maticna.py b/stdnum/si/maticna.py
new file mode 100644
index 0000000..6aa9135
--- /dev/null
+++ b/stdnum/si/maticna.py
@@ -0,0 +1,92 @@
+# maticna.py - functions for handling Slovenian Corporate Registration Numbers
+# coding: utf-8
+#
+# Copyright (C) 2023 Blaž Bregar
+#
+# 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
+
+"""Matična številka poslovnega registra (Corporate Registration Number)
+
+The Corporate registration number represent a unique identification of
+each unit of the business register, assigned by the registry administrator
+at the time of entry in the business register, which shall not be changed.
+
+The number consists of 7 or 10 digits and includes a check digit. The first 6
+digits represent a unique number for each unit or company, followed by a
+check digit. The last 3 digits represent an additional business unit of the
+company, starting at 001. When a company consists of more than 1000 units, a
+letter is used instead of the first digit in the business unit. Unit 000
+always represents the main registered address.
+
+More information:
+
+* http://www.pisrs.si/Pis.web/pregledPredpisa?id=URED7599
+
+>>> validate('9331310000')
+'9331310'
+>>> validate('9331320000')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+"""
+
+import re
+
+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."""
+    number = clean(number, '. ').strip().upper()
+    if len(number) == 10 and number.endswith('000'):
+        number = number[0:7]
+    return number
+
+
+def calc_check_digit(number):
+    """Calculate the check digit."""
+    weights = (7, 6, 5, 4, 3, 2)
+    total = sum(int(n) * w for n, w in zip(number, weights))
+    remainder = -total % 11
+    if remainder == 0:
+        return 'invalid'  # invalid remainder
+    return str(remainder % 10)
+
+
+def validate(number):
+    """Check if the number is a valid Corporate Registration number. This
+    checks the length and check digit."""
+    number = compact(number)
+    if len(number) not in (7, 10):
+        raise InvalidLength()
+    if not isdigits(number[:6]):
+        raise InvalidFormat()
+    if not re.match(r'^([A-Za-z0-9]\d{2})?$', number[7:]):
+        raise InvalidFormat()
+    if calc_check_digit(number) != number[6]:
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if provided is valid ID. This checks the length,
+    formatting and check digit."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_si_maticna.doctest b/tests/test_si_maticna.doctest
new file mode 100644
index 0000000..b5f0d60
--- /dev/null
+++ b/tests/test_si_maticna.doctest
@@ -0,0 +1,123 @@
+test_si_maticna.doctest - more detailed doctests for the stdnum.si.maticna 
module
+
+Copyright (C) 2023 Blaž Bregar
+
+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.si.emso. It
+tries to validate a number of numbers that have been found online.
+
+>>> from stdnum.si import maticna
+>>> from stdnum.exceptions import *
+
+
+Tests for some corner cases.
+
+>>> maticna.validate('9331310000')
+'9331310'
+>>> maticna.validate('9331310255')
+'9331310255'
+>>> maticna.validate('9331310 000')
+'9331310'
+>>> maticna.validate('9331310')
+'9331310'
+>>> maticna.validate('9331310255')
+'9331310255'
+>>> maticna.validate('8982279A00')
+'8982279A00'
+>>> maticna.validate('8982279J00')
+'8982279J00'
+>>> maticna.validate('8982279z00')
+'8982279Z00'
+>>> maticna.validate('8982279f00')
+'8982279F00'
+>>> maticna.validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> maticna.validate('9331320000')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> maticna.validate('933A310100')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> maticna.validate('93313100A0')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> maticna.validate('9331310AA0')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> maticna.validate('5491710')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> maticna.validate('9331310$$$')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> maticna.validate('9015310')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> maticna.validate('2961970')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> maticna.validate('5015170')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> maticna.validate('3919110')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 1876031010
+... 3490360000
+... 3501264000
+... 5147174000
+... 5263565019
+... 5300231000
+... 5300231136
+... 5300231150
+... 5464943000
+... 5464943003
+... 5464943608
+... 5491711
+... 5860571000
+... 5860571084
+... 5860571255
+... 5860580000
+... 5860580150
+... 7282664000
+... 8071993000
+... 8339414000
+... 8982279000
+... 9331310
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not maticna.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/si/__init__.py         |   3 +-
 stdnum/si/maticna.py          |  92 +++++++++++++++++++++++++++++++
 tests/test_si_maticna.doctest | 123 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 217 insertions(+), 1 deletion(-)
 create mode 100644 stdnum/si/maticna.py
 create mode 100644 tests/test_si_maticna.doctest


hooks/post-receive
-- 
python-stdnum