Skip to content

Commit

Permalink
release: 0.6.0 release
Browse files Browse the repository at this point in the history
fix: refactor
fix: add dev requirements
fix: add ruff formater
  • Loading branch information
ZhymabekRoman committed Apr 11, 2023
1 parent 1511c37 commit 021d9b3
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 36 deletions.
13 changes: 13 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CHANGES
===============

kz-iin-validator 0.6.0
-----------

### Changes between 0.5.0 and 0.6.0
* Function `validate_iin` was renamed to `safe_validate_iin`
* Private function `_validate_iin` was renamed to `validate_iin`
* Private property `_datetime` was renamed to `datetime`
* Argument `raise_exception=False` from `safe_validate_iin` was deleted
* Fix typos: `lenght` to `length`
* Minor fixes
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ python3 -m pip install git+https://github.com/ZhymabekRoman/kz-iin-validator
## Использование:
### Валидация ИИН:
```python
from kz_iin_validator import validate_iin
from kz_iin_validator import safe_validate_iin, validate_iin

iin = "061211600012"
validated_iin, error = validate_iin(iin)
# use kz_iin_validator as in Golang (preferred in production)
validated_iin, error = safe_validate_iin(iin)
# or directly with exceptions raising:
# validated_iin = validate_iin(iin)

print(f"ИИН: {iin}")

Expand Down
6 changes: 6 additions & 0 deletions duties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from duty import duty


@duty
def format(ctx):
ctx.run("ruff kz_iin_validator/ tests/ duties.py --fix", title="Formating code")
6 changes: 3 additions & 3 deletions kz_iin_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .iin_validator import IIN, ValidatedIIN, _validate_iin, validate_iin
from .iin_generator import generate_iin
from .exceptions import IINValidateError
from .iin_generator import generate_iin
from .iin_validator import IIN, ValidatedIIN, safe_validate_iin, validate_iin

__version__ = "0.5.0"
__version__ = "0.6.0"
6 changes: 3 additions & 3 deletions kz_iin_validator/iin_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def generate_iin():
day = random.randint(1, day_bound)

# zfill
year_str = '{:02d}'.format(year)
month_str = '{:02d}'.format(month)
day_str = '{:02d}'.format(day)
year_str = f'{year:02d}'
month_str = f'{month:02d}'
day_str = f'{day:02d}'

dob = f"{year_str}{month_str}{day_str}"

Expand Down
28 changes: 13 additions & 15 deletions kz_iin_validator/iin_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from enum import Enum, auto
from typing import Union

from .utils import is_digit_string
from .exceptions import IINValidateError
from .utils import is_digit_string

IIN_REGEX_WEAK_FAST = re.compile(r"^[0-9]{12}$")
IIN_REGEX_WEAK = re.compile(r"^((0[48]|[2468][048]|[13579][26])0230[1-5]|000230[34]|\d\d((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[469]|11)(0[1-9]|[12]\d|30)|02(0[1-9]|[1-2]\d)))[0-6]\d{5}$")
Expand Down Expand Up @@ -36,23 +36,19 @@ class ValidatedIIN(IIN):
is_validated: bool = True


def validate_iin(iin: Union[str, IIN], weak_fast_check: bool = False, raise_exception: bool = False, full_error_info: bool = True):
def safe_validate_iin(iin: Union[str, IIN], weak_fast_check: bool = False):
# Golang like exception returning logic
try:
result = _validate_iin(iin, weak_fast_check)
result = validate_iin(iin, weak_fast_check)
except Exception as ex:
if raise_exception:
raise ex
if full_error_info:
exception_msg = f"During validating IIN exception was caught: {str(ex)}"
else:
exception_msg = "During validating IIN exception was caught"
exception_msg = f"During validating IIN exception was caught: {str(ex)}"
return None, exception_msg
else:
return result, None


def _validate_iin(iin: Union[str, IIN], weak_fast_check: bool = False):
# TODO: refactor into separate functions
def validate_iin(iin: Union[str, IIN], weak_fast_check: bool = False) -> ValidatedIIN:
if isinstance(iin, IIN):
iin = iin.iin
elif not isinstance(iin, str):
Expand All @@ -67,15 +63,17 @@ def _validate_iin(iin: Union[str, IIN], weak_fast_check: bool = False):
if iin_regex_weak is None:
raise IINValidateError("Not correct integers range")

if len(iin) != 12:
raise IINValidateError("IIN must be 12 lenght")

if not is_digit_string(iin):
raise IINValidateError("IIN must contains only numbers")

if len(iin) != 12:
raise IINValidateError("IIN must be 12 length")

# iin helper functions
iin_int = lambda index: int(iin[index])
iin_int_range = lambda x, y: int(iin[x:y])
def iin_int(index):
return int(iin[index])
def iin_int_range(x, y):
return int(iin[x:y])

is_person = (iin_int(6) != 0)
if is_person:
Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
duty==0.7.0
pytest==7.2.1
ruff==0.0.261
11 changes: 11 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
target-version = "py37"

select = [
"E", # pycodestyle
"F", # pyflakes
"UP", # pyupgrade,
"I", # isort
]

line-length = 200
per-file-ignores = {"__init__.py" = ["F401"]}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def read_requirements(filename):

setup(
name="kz_iin_validator",
version="0.5.0",
version="0.6.0",
python_requires='>=3.7',
author="Zhymabek Roman",
author_email="robanokssamit@yandex.ru",
Expand Down
23 changes: 11 additions & 12 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from kz_iin_validator import validate_iin, generate_iin, IINValidateError, _validate_iin
from kz_iin_validator.utils import is_digit_string
import pytest
import random

import pytest

def test_valid_iin():
for _ in range(20000):
iin = generate_iin()
validate_iin(iin)
from kz_iin_validator import IINValidateError, generate_iin, safe_validate_iin, validate_iin
from kz_iin_validator.utils import is_digit_string


def test_valid_iin():
for _ in range(20000):
iin = generate_iin()
iin = _validate_iin(iin)
validate_iin(iin)
validated_iin = validate_iin(iin)
assert validated_iin is not None

for _ in range(20000):
iin = generate_iin()
validate_iin(iin, weak_fast_check=True)
validated_iin = safe_validate_iin(iin, weak_fast_check=True)
assert validated_iin is not None


def test_non_valid_iin():
Expand Down Expand Up @@ -53,10 +52,10 @@ def test_non_valid_iin():
with pytest.raises(IINValidateError):
validate_iin(iin, weak_fast_check=True)

iin_result, err = validate_iin(iin, raise_exception=False, full_error_info=False)
iin_result, err = safe_validate_iin(iin)
assert err is not None

iin_result, err = validate_iin(iin, raise_exception=False, full_error_info=True)
iin_result, err = safe_validate_iin(iin)
assert err is not None


Expand Down

0 comments on commit 021d9b3

Please sign in to comment.