From f2bc4812118730cafc9ca6152166dc4122ec3abb Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Sun, 21 Apr 2024 22:44:24 +0800 Subject: [PATCH] :bookmark: version 0.7.0 --- nepattern/__init__.py | 3 ++- nepattern/base.py | 36 ++++++++++++++++++++++++------------ pyproject.toml | 6 +++--- test.py | 9 +++++++-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/nepattern/__init__.py b/nepattern/__init__.py index 5a3fb25..e27ee5f 100644 --- a/nepattern/__init__.py +++ b/nepattern/__init__.py @@ -9,6 +9,7 @@ from .base import BOOLEAN as BOOLEAN from .base import BYTES as BYTES from .base import DATETIME as DATETIME +from .base import DelimiterInt as DelimiterInt from .base import DICT as DICT from .base import DirectPattern as DirectPattern from .base import DirectTypePattern as DirectTypePattern @@ -34,7 +35,7 @@ from .base import URL as URL from .base import UnionPattern as UnionPattern from .base import WIDE_BOOLEAN as WIDE_BOOLEAN -from .base import pipe as pipe +from .base import combine as combine from .context import Patterns as Patterns from .context import all_patterns as all_patterns from .context import create_local_patterns as create_local_patterns diff --git a/nepattern/base.py b/nepattern/base.py index b8a945a..b99708f 100644 --- a/nepattern/base.py +++ b/nepattern/base.py @@ -23,7 +23,7 @@ from tarina import DateParser, Empty, lang -from .core import BasePattern, MatchMode, ResultFlag, ValidateResult, _MATCHES, TInput, TOrigin, TMM +from .core import BasePattern, MatchMode, ResultFlag, ValidateResult, _MATCHES, TMM from .exception import MatchFailed from .util import TPattern @@ -186,7 +186,7 @@ def __init__(self, base: Iterable[BasePattern[Any, _T, Any] | _T]): def match(self, input_: Any): if not input_: - text = None + input_ = None if input_ not in self.for_equal: for pat in self.for_validate: if (res := pat.validate(input_)).success: @@ -468,7 +468,7 @@ def __calc_eq__(self, other): # pragma: no cover return isinstance(other, CustomMatchPattern) and self.__func__ == other.__func__ -NONE = CustomMatchPattern(type(None), lambda _, x: None, alias="none") # pragma: no cover +NONE = CustomMatchPattern(type(None), lambda _, __: None, alias="none") # pragma: no cover @final @@ -798,18 +798,30 @@ def __calc_eq__(self, other): # pragma: no cover ) -def pipe(previous: BasePattern[Any, Any, Literal[MatchMode.VALUE_OPERATE]], current: BasePattern[TOrigin, TInput, TMM]) -> BasePattern[TOrigin, TInput, TMM]: +def combine( + current: BasePattern[TOrigin, TInput, TMM], + previous: BasePattern[Any, Any, Literal[MatchMode.VALUE_OPERATE]] | None = None, + alias: str | None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, +) -> BasePattern[TOrigin, TInput, TMM]: _new = current.copy() - _match = _new.match - - def match(self, input_): - return _match(previous.match(input_)) - - _new.match = match.__get__(_new) + if previous: + _match = _new.match + + def match(self, input_): + return _match(previous.match(input_)) + + _new.match = match.__get__(_new) + if alias: + _new.alias = alias + _new.refresh() + if validators: + _new.validators = validators return _new -DelimiterInt = pipe( +DelimiterInt = combine( + INTEGER, BasePattern(mode=MatchMode.VALUE_OPERATE, origin=str, converter=lambda _, x: x.replace(",", "_")), - INTEGER + "DelimInt", ) diff --git a/pyproject.toml b/pyproject.toml index 64c16fa..4841882 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nepattern" -version = "0.6.5" +version = "0.7.0" description = "a complex pattern, support typing" authors = [ {name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com"}, @@ -33,8 +33,8 @@ repository = "https://github.com/ArcletProject/NEPattern" [project.optional-dependencies] [build-system] -requires = ["pdm-pep517>=1.0.0"] -build-backend = "pdm.pep517.api" +requires = ["pdm-backend"] +build-backend = "pdm.backend" [tool] [tool.pdm] diff --git a/test.py b/test.py index 7f4f36d..f593adc 100644 --- a/test.py +++ b/test.py @@ -578,12 +578,17 @@ def test_eq(): assert parser(str) == STRING -def test_pipe(): +def test_combine(): pre = BasePattern(mode=MatchMode.VALUE_OPERATE, origin=str, converter=lambda _, x: x.replace(",", "_")) - pat23 = pipe(pre, INTEGER) + pat23 = combine(INTEGER, pre) assert pat23.validate("123,456").value() == 123456 assert pat23.validate("1,000,000").value() == 1_000_000 + pat23_1 = combine(INTEGER, alias="0~10", validators=[lambda x: 0 <= x <= 10]) + assert pat23_1.validate(5).value() == 5 + assert pat23_1.validate(11).failed + assert str(pat23_1) == "0~10" + if __name__ == "__main__": import pytest