Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add russian OGRN #459

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
844ae99
ogrn validation
nvmbrasserie Nov 20, 2024
c9fb4a1
updated ogrn validation
nvmbrasserie Nov 20, 2024
27c0b67
validation fixes
nvmbrasserie Nov 20, 2024
c63ad42
formatted
nvmbrasserie Nov 20, 2024
d6af3fc
more formatting
nvmbrasserie Nov 20, 2024
8c03dbc
comments removed
nvmbrasserie Nov 20, 2024
1051b7d
flake8 fixes
nvmbrasserie Nov 20, 2024
e5dc540
Traceback (most recent call last)
nvmbrasserie Nov 20, 2024
70a9d89
flake8 2
nvmbrasserie Nov 20, 2024
3ac38cb
ValidationError added
nvmbrasserie Nov 20, 2024
3744f80
formatted again
nvmbrasserie Nov 20, 2024
053a151
test added
nvmbrasserie Nov 20, 2024
7c82334
test fixes
nvmbrasserie Nov 20, 2024
1ec3084
docs and tests
nvmbrasserie Nov 20, 2024
91a3b24
rewritten test
nvmbrasserie Nov 20, 2024
5a10fb3
happy test
nvmbrasserie Nov 20, 2024
c237cfc
tests successful
nvmbrasserie Nov 20, 2024
3cc376b
flake fix
nvmbrasserie Nov 20, 2024
2b629c3
more ==
nvmbrasserie Nov 20, 2024
682451b
index rst
nvmbrasserie Nov 20, 2024
05d2a57
all tests passed
nvmbrasserie Nov 20, 2024
9e2d517
flake fix
nvmbrasserie Nov 20, 2024
b29c5f4
duplicate removed
nvmbrasserie Nov 21, 2024
6972043
ru ogrn rst
nvmbrasserie Nov 21, 2024
15d8f0b
tox reconfigured
nvmbrasserie Nov 21, 2024
83561a1
test py27
nvmbrasserie Nov 21, 2024
9b01d76
isinstance upd
nvmbrasserie Nov 21, 2024
a558ac6
coding fix
nvmbrasserie Nov 21, 2024
764fac7
flake8 fix
nvmbrasserie Nov 21, 2024
0abf886
federal subject codes check removed
nvmbrasserie Dec 10, 2024
2754347
formatting
nvmbrasserie Dec 10, 2024
f220b9f
federal codes test removed
nvmbrasserie Dec 10, 2024
2352ffd
flake fix
nvmbrasserie Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Available formats
ro.onrc
rs.pib
ru.inn
ru.ogrn
se.orgnr
se.personnummer
se.postnummer
Expand Down
5 changes: 5 additions & 0 deletions docs/stdnum.ru.ogrn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stdnum.ru.ogrn
==============

.. automodule:: stdnum.ru.ogrn
:members:
97 changes: 97 additions & 0 deletions stdnum/ru/ogrn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

# ogrn.py - functions for handling Russian company registration numbers
# coding: utf-8
#
# Copyright (C) 2010-2024 Arthur de Jong and others
#
# 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 (Primary State Registration Number).

The OGRN is a Russian identifier for legal entities that consists of either 13 or 15 digits.

>>> validate('1022200525819')
True
>>> validate('1022500001325')
True
>>> validate('10277395526422') # 14 digits
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('1022500001328') # invalid check digit
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('0022500001325') # starts with 0
Traceback (most recent call last):
...
InvalidComponent: ...
>>> validate('102250000') # too short
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('385768585948949') # 15 digits
True
"""

import re

from stdnum.exceptions import *


def validate(text):
"""Determine if the given string is a valid OGRN."""
if text[0] == '0':
raise InvalidComponent()
control_digit = int(text[-1])
if control_digit != calculate_control_digit(text):
raise InvalidChecksum()
return True


def format(text):
"""Normalize the given string to a valid OGRN."""
if not isinstance(text, str):
return None
match = re.compile(r'\b(\d{13}|\d{15})\b').search(text)
if match is None:
raise InvalidLength()
return match.group(1)


def calculate_control_digit(grn):
"""Calculate the control digit of the OGRN based on its length."""
if len(grn) == 13:
number = int(grn[:12])
mod_result = number % 11
calculated_digit = mod_result if mod_result != 10 else 0
return calculated_digit
elif len(grn) == 15:
number = int(grn[:14])
mod_result = number % 13
calculated_digit = mod_result if mod_result != 10 else 0
return calculated_digit
else:
raise InvalidLength()


def is_valid(text):
"""Check if the number is a valid OGRN."""
try:
normalized_text = format(text)
return bool(normalized_text and validate(normalized_text))
except ValidationError:
return False
43 changes: 43 additions & 0 deletions tests/test_ru_ogrn.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test_ru_ogrn.doctest - more detailed doctests for the stdnum.ru.ogrn module

Copyright (C) 2010-2024 Arthur de Jong and others

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, normalization, and validity of various
OGRN numbers, ensuring they conform to expected behavior.

>>> from stdnum.ru import ogrn
>>> from stdnum.exceptions import *

This is a list of OGRNs that should all be valid numbers:

>>> valid_numbers = '''
...
... 1027739552642
... 1022600000092
... 1022500000566
... 1027100000311
... 1022400007508
... 1022300001811
... 1022300000502
...
... '''
>>> [x for x in valid_numbers.splitlines() if x and not ogrn.is_valid(x)]
[]
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[tox]
envlist = py{27,36,37,38,39,310,311,312,py,py3},flake8,docs,headers
skip_missing_interpreters = true
requires = virtualenv<20.22.0

[testenv]
deps = pytest
Expand Down
Loading