Skip to content

Commit

Permalink
Add MatchAll
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 8, 2023
1 parent 509dc3f commit b25bef2
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions pyfuncpatmatch/pyfuncpatmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
import inspect

class PatMatchAll:
def __init__(self) -> None:
pass

class PatEqMatch:
def __init__(self, value: Any) -> None:
self.value = value
Expand All @@ -15,17 +19,17 @@ def __init__(
self,
var_name_fst: str,
var_name_rest: str,
fst_eq_match: Optional[PatEqMatch] = None,
rest_eq_match: Optional[PatEqMatchList] = None,
fst_eq_match: Union[PatEqMatch, PatMatchAll] = PatMatchAll(),
rest_eq_match: Union[PatEqMatchList, PatMatchAll] = PatMatchAll(),
) -> None:
self.var_name_fst = var_name_fst
self.var_name_rest = var_name_rest
self.fst_eq_match = fst_eq_match
self.rest_eq_match = rest_eq_match


PatternMatch = Union[Any, PatListExtract, PatEqMatch]
LArgsPatternMatch = Union[List[PatternMatch], Tuple[PatternMatch]]
PatternMatch = Union[Any, PatListExtract, PatEqMatch, PatMatchAll]
LArgsPatternMatch = Union[List[PatternMatch], Tuple[PatternMatch], PatMatchAll]
LArgsPatternEqMatch = Union[List[PatEqMatch], Tuple[PatEqMatch]]
LArgsPatternExtractMatch = Union[List[PatListExtract], Tuple[PatListExtract]]
KArgsPatternMatch = Dict[str, PatternMatch]
Expand Down Expand Up @@ -60,27 +64,32 @@ def __call__(self, *args, **kwds) -> Any:

def _args_to_kwargs(self, args: Union[LArgsPatternMatch, list], kwargs: Union[KArgsPatternMatch, dict]) -> Union[KArgsPatternMatch, dict]:
ret = {}
for value, key in zip(args, self.parameters.keys()):
ret[key] = value
if not isinstance(args, PatMatchAll):
for value, key in zip(args, self.parameters.keys()):
ret[key] = value
for key, value in kwargs.items():
ret[key] = value
return ret

def _parse_kwargs(self,k_args: KArgsPatternMatch) -> Tuple[Dict[str, PatEqMatch], Dict[str, PatListExtract]]:
exact_patterns: Dict[str, PatEqMatch] = {}
def _parse_kwargs(self, k_args: KArgsPatternMatch) -> Tuple[Dict[str, Union[PatEqMatch, PatMatchAll]], Dict[str, PatListExtract]]:
exact_patterns: Dict[str, Union[PatEqMatch, PatMatchAll]] = {}
extract_pattern: Dict[str, PatListExtract] = {}
for key_arg, item_arg in k_args.items():
if isinstance(item_arg, PatListExtract):
extract_pattern[key_arg] = item_arg
elif isinstance(item_arg, PatEqMatch):
exact_patterns[key_arg] = item_arg
elif isinstance(item_arg, PatMatchAll):
exact_patterns[key_arg] = item_arg
else:
exact_patterns[key_arg] = PatEqMatch(value=item_arg)
return (exact_patterns, extract_pattern)

def _is_exact_paterns(self, call_args: list, call_kwargs: dict) -> bool:
call_k_wargs = self._args_to_kwargs(call_args, call_kwargs)
for key, exact in self.exact_patterns.items():
if isinstance(exact, PatMatchAll):
continue
if call_k_wargs[key] != exact.value:
return False
return True
Expand All @@ -91,14 +100,14 @@ def _exe_extract_paterns(self, call_args: list, call_kwargs: dict) -> Union[dict
call_k_wargs = self._args_to_kwargs(call_args, call_kwargs)
for key, extract in self.extract_patterns.items():
value = call_k_wargs.get(key)
if value is None or isinstance(value, (PatListExtract, PatEqMatch)):
if value is None or isinstance(value, (PatListExtract, PatEqMatch, PatMatchAll)):
return "not ok"
fst = value[0]
rest = value[1:]
if extract.fst_eq_match is not None:
if not isinstance(extract.fst_eq_match, PatMatchAll):
if fst != extract.fst_eq_match.value:
return "not ok"
if extract.rest_eq_match is not None:
if not isinstance(extract.rest_eq_match, PatMatchAll):
if rest != extract.rest_eq_match.value:
return "not ok"
call_k_wargs.pop(key)
Expand All @@ -108,12 +117,14 @@ def _exe_extract_paterns(self, call_args: list, call_kwargs: dict) -> Union[dict


def patfunc(
l_args: LArgsPatternMatch = [],
k_args: KArgsPatternMatch = {},
l_args: LArgsPatternMatch = PatMatchAll(),
k_args: Union[KArgsPatternMatch, PatMatchAll] = PatMatchAll(),
func_instead: Optional[Callable] = None
):
if isinstance(l_args, list):
l_args = tuple(l_args)
if isinstance(k_args, PatMatchAll):
k_args = {}
def inner_decorator(true_func: Callable):
return _PatDecoratorClass(func_instead, true_func, l_args, k_args)
return inner_decorator

0 comments on commit b25bef2

Please sign in to comment.