Skip to content

Commit

Permalink
🔖 version 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Apr 21, 2024
1 parent 223cf3b commit f2bc481
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
3 changes: 2 additions & 1 deletion nepattern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 24 additions & 12 deletions nepattern/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
)
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"},
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 7 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2bc481

Please sign in to comment.