lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.20-24-g1386f67

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

python-stdnum branch master updated. 1.20-24-g1386f67



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  1386f677ea3dbe4743b62f986c85dd08d025bcb2 (commit)
      from  8519221a54c2baa3a3d7253969402f84aa2353b5 (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=1386f677ea3dbe4743b62f986c85dd08d025bcb2

commit 1386f677ea3dbe4743b62f986c85dd08d025bcb2
Author: nvmbrasserie <ivan@opensanctions.org>
Date:   Tue Dec 10 14:19:26 2024 +0100

    Add Russian ОГРН
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/459

diff --git a/stdnum/ru/ogrn.py b/stdnum/ru/ogrn.py
new file mode 100644
index 0000000..e3d6558
--- /dev/null
+++ b/stdnum/ru/ogrn.py
@@ -0,0 +1,88 @@
+# ogrn.py - functions for handling Russian company registration numbers
+# coding: utf-8
+#
+# Copyright (C) 2024 Ivan Stavropoltsev
+# Copyright (C) 2025 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
+
+"""ОГРН, OGRN, PSRN, ОГРНИП, OGRNIP (Russian Primary State Registration 
Number).
+
+The ОГРН (Основной государственный регистрационный номер, Primary State
+Registration Number) is a Russian identifier for legal entities. The number
+consists of 13 or 15 digits and includes information on the type of
+organisation, the registration year and a tax inspection code. The 15 digit
+variant is called ОГРНИП (Основной государственный регистрационный номер
+индивидуального предпринимателя, Primary State Registration Number of an
+Individual Entrepreneur).
+
+More information:
+
+* https://ru.wikipedia.org/wiki/Основной_государственный_регистрационный_номер
+* 
https://ru.wikipedia.org/wiki/Основной_государственный_регистрационный_номер_индивидуального_предпринимателя
+
+>>> validate('1022200525819')
+'1022200525819'
+>>> validate('385768585948949')
+'385768585948949'
+>>> validate('1022500001328')
+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, ' ')
+
+
+def calc_check_digit(number):
+    """Calculate the control digit of the OGRN based on its length."""
+    if len(number) == 13:
+        return str(int(number[:-1]) % 11 % 10)
+    else:
+        return str(int(number[:-1]) % 13)
+
+
+def validate(number):
+    """Determine if the given number is a valid OGRN."""
+    number = compact(number)
+    if not isdigits(number):
+        raise InvalidFormat()
+    if len(number) == 13:
+        if number[0] == '0':
+            raise InvalidComponent()
+    elif len(number) == 15:
+        if number[0] not in '34':
+            raise InvalidComponent()
+    else:
+        raise InvalidLength()
+    if number[-1] != calc_check_digit(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid OGRN."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_ru_ogrn.doctest b/tests/test_ru_ogrn.doctest
new file mode 100644
index 0000000..4302e39
--- /dev/null
+++ b/tests/test_ru_ogrn.doctest
@@ -0,0 +1,78 @@
+test_ru_ogrn.doctest - more detailed doctests for the stdnum.ru.ogrn module
+
+Copyright (C) 2024 Ivan Stavropoltsev
+
+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.ru.ogrn module.
+
+These tests validate the format, normalisation, and validity of various
+OGRN numbers, ensuring they conform to expected behaviour.
+
+>>> from stdnum.ru import ogrn
+>>> from stdnum.exceptions import *
+
+
+Checks of the 13 digit ОГРН, OGRN
+
+>>> ogrn.validate('1022500001325')
+'1022500001325'
+>>> ogrn.validate('10277395526422')  # 14 digits
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> ogrn.validate('0022500001325')  # starts with 0
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> ogrn.validate('102250000')  # too short
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> ogrn.validate('1022500001328')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+Checks of the 13 digit ОГРНИП, OGRNIP
+
+>>> ogrn.validate('985768585948944')  # OGRNIP with invalid start digit
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> ogrn.validate('385768585948948')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+This is a list of OGRNs that should all be valid numbers:
+
+>>> valid_numbers = '''
+...
+... 1022300000502
+... 1022300001811
+... 1022400007508
+... 1022500000566
+... 1022600000092
+... 1027100000311
+... 1027739552642
+...
+... '''
+>>> [x for x in valid_numbers.splitlines() if x and not ogrn.is_valid(x)]
+[]

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

Summary of changes:
 stdnum/ru/ogrn.py          | 88 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_ru_ogrn.doctest | 78 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+)
 create mode 100644 stdnum/ru/ogrn.py
 create mode 100644 tests/test_ru_ogrn.doctest


hooks/post-receive
-- 
python-stdnum