From 021d9b32caeb3f8a7be215202898cf2818b3e9f4 Mon Sep 17 00:00:00 2001 From: Zhymabek Roman Date: Tue, 11 Apr 2023 12:50:48 +0600 Subject: [PATCH] release: 0.6.0 release fix: refactor fix: add dev requirements fix: add ruff formater --- CHANGES.md | 13 +++++++++++++ README.md | 7 +++++-- duties.py | 6 ++++++ kz_iin_validator/__init__.py | 6 +++--- kz_iin_validator/iin_generator.py | 6 +++--- kz_iin_validator/iin_validator.py | 28 +++++++++++++--------------- requirements-dev.txt | 3 +++ ruff.toml | 11 +++++++++++ setup.py | 2 +- tests/test_basic.py | 23 +++++++++++------------ 10 files changed, 69 insertions(+), 36 deletions(-) create mode 100644 CHANGES.md create mode 100644 duties.py create mode 100644 requirements-dev.txt create mode 100644 ruff.toml diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..6dc0390 --- /dev/null +++ b/CHANGES.md @@ -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 diff --git a/README.md b/README.md index 53570b0..797ccb8 100644 --- a/README.md +++ b/README.md @@ -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}") diff --git a/duties.py b/duties.py new file mode 100644 index 0000000..55cb58d --- /dev/null +++ b/duties.py @@ -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") diff --git a/kz_iin_validator/__init__.py b/kz_iin_validator/__init__.py index 12cacb5..a978088 100644 --- a/kz_iin_validator/__init__.py +++ b/kz_iin_validator/__init__.py @@ -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" diff --git a/kz_iin_validator/iin_generator.py b/kz_iin_validator/iin_generator.py index 43e15fd..5902016 100644 --- a/kz_iin_validator/iin_generator.py +++ b/kz_iin_validator/iin_generator.py @@ -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}" diff --git a/kz_iin_validator/iin_validator.py b/kz_iin_validator/iin_validator.py index 3711944..e7fecac 100644 --- a/kz_iin_validator/iin_validator.py +++ b/kz_iin_validator/iin_validator.py @@ -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}$") @@ -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): @@ -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: diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..748e9b3 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,3 @@ +duty==0.7.0 +pytest==7.2.1 +ruff==0.0.261 diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..983690f --- /dev/null +++ b/ruff.toml @@ -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"]} diff --git a/setup.py b/setup.py index 57e98a6..209b7c9 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/test_basic.py b/tests/test_basic.py index cfa3643..b462576 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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(): @@ -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