/
magic.py
240 lines (179 loc) · 9.94 KB
/
magic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import operator
import re
from functools import wraps
from typing import Any, Callable, Optional, Pattern, Sequence, Set, Tuple, Type, TypeVar, Union
from magic_filter.exceptions import RejectOperations, SwitchModeToAll, SwitchModeToAny
from magic_filter.operations import (
BaseOperation,
CallOperation,
CastOperation,
CombinationOperation,
ComparatorOperation,
FunctionOperation,
GetAttributeOperation,
GetItemOperation,
ImportantCombinationOperation,
ImportantFunctionOperation,
RCombinationOperation,
SelectorOperation,
)
from magic_filter.util import and_op, contains_op, in_op, or_op
MagicT = TypeVar("MagicT", bound="MagicFilter")
class MagicFilter:
__slots__ = ("_operations",)
def __init__(self, operations: Tuple[BaseOperation, ...] = ()) -> None:
self._operations = operations
# An instance of MagicFilter cannot be used as an iterable object because objects
# with a __getitem__ method can be endlessly iterated, which is not the desired behavior.
__iter__ = None
@classmethod
def ilter(cls, magic: "MagicFilter") -> Callable[[Any], Any]:
@wraps(magic.resolve)
def wrapper(value: Any) -> Any:
return magic.resolve(value)
return wrapper
@classmethod
def _new(cls: Type[MagicT], operations: Tuple[BaseOperation, ...]) -> MagicT:
return cls(operations=operations)
def _extend(self: MagicT, operation: BaseOperation) -> MagicT:
return self._new(self._operations + (operation,))
def _replace_last(self: MagicT, operation: BaseOperation) -> MagicT:
return self._new(self._operations[:-1] + (operation,))
def _exclude_last(self: MagicT) -> MagicT:
return self._new(self._operations[:-1])
def _resolve(self, value: Any, operations: Optional[Tuple[BaseOperation, ...]] = None) -> Any:
initial_value = value
if operations is None:
operations = self._operations
rejected = False
for index, operation in enumerate(operations):
if rejected and not operation.important:
continue
try:
value = operation.resolve(value=value, initial_value=initial_value)
except SwitchModeToAll:
return all(
self._resolve(value=item, operations=operations[index + 1 :]) for item in value
)
except SwitchModeToAny:
return any(
self._resolve(value=item, operations=operations[index + 1 :]) for item in value
)
except RejectOperations:
rejected = True
value = None
continue
rejected = False
return value
def resolve(self: MagicT, value: Any) -> Any:
return self._resolve(value=value)
def __getattr__(self: MagicT, item: Any) -> MagicT:
if item.startswith("_"):
raise AttributeError(f"{type(self).__name__!r} object has no attribute {item!r}")
return self._extend(GetAttributeOperation(name=item))
attr_ = __getattr__
def __getitem__(self: MagicT, item: Any) -> MagicT:
if isinstance(item, MagicFilter):
return self._extend(SelectorOperation(inner=item))
return self._extend(GetItemOperation(key=item))
def __eq__(self: MagicT, other: Any) -> MagicT: # type: ignore
return self._extend(ComparatorOperation(right=other, comparator=operator.eq))
def __ne__(self: MagicT, other: Any) -> MagicT: # type: ignore
return self._extend(ComparatorOperation(right=other, comparator=operator.ne))
def __lt__(self: MagicT, other: Any) -> MagicT:
return self._extend(ComparatorOperation(right=other, comparator=operator.lt))
def __gt__(self: MagicT, other: Any) -> MagicT:
return self._extend(ComparatorOperation(right=other, comparator=operator.gt))
def __le__(self: MagicT, other: Any) -> MagicT:
return self._extend(ComparatorOperation(right=other, comparator=operator.le))
def __ge__(self: MagicT, other: Any) -> MagicT:
return self._extend(ComparatorOperation(right=other, comparator=operator.ge))
def __invert__(self: MagicT) -> MagicT:
if (
self._operations
and isinstance(self._operations[-1], ImportantFunctionOperation)
and self._operations[-1].function == operator.not_
):
return self._exclude_last()
return self._extend(ImportantFunctionOperation(function=operator.not_))
def __call__(self: MagicT, *args: Any, **kwargs: Any) -> MagicT:
return self._extend(CallOperation(args=args, kwargs=kwargs))
def __and__(self: MagicT, other: Any) -> MagicT:
if isinstance(other, MagicFilter):
return self._extend(CombinationOperation(right=other, combinator=and_op))
return self._extend(CombinationOperation(right=other, combinator=operator.and_))
def __rand__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.and_))
def __or__(self: MagicT, other: Any) -> MagicT:
if isinstance(other, MagicFilter):
return self._extend(ImportantCombinationOperation(right=other, combinator=or_op))
return self._extend(ImportantCombinationOperation(right=other, combinator=operator.or_))
def __ror__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.or_))
def __xor__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.xor))
def __rxor__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.xor))
def __rshift__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.rshift))
def __rrshift__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.rshift))
def __lshift__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.lshift))
def __rlshift__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.lshift))
def __add__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.add))
def __radd__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.add))
def __sub__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.sub))
def __rsub__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.sub))
def __mul__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.mul))
def __rmul__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.mul))
def __truediv__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.truediv))
def __rtruediv__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.truediv))
def __floordiv__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.floordiv))
def __rfloordiv__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.floordiv))
def __mod__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.mod))
def __rmod__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.mod))
def __matmul__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.matmul))
def __rmatmul__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.matmul))
def __pow__(self: MagicT, other: Any) -> MagicT:
return self._extend(CombinationOperation(right=other, combinator=operator.pow))
def __rpow__(self: MagicT, other: Any) -> MagicT:
return self._extend(RCombinationOperation(left=other, combinator=operator.pow))
def __pos__(self: MagicT) -> MagicT:
return self._extend(FunctionOperation(function=operator.pos))
def __neg__(self: MagicT) -> MagicT:
return self._extend(FunctionOperation(function=operator.neg))
def is_(self: MagicT, value: Any) -> MagicT:
return self._extend(CombinationOperation(right=value, combinator=operator.is_))
def is_not(self: MagicT, value: Any) -> MagicT:
return self._extend(CombinationOperation(right=value, combinator=operator.is_not))
def in_(self: MagicT, iterable: Union[Sequence[Any], Set[Any]]) -> MagicT:
return self._extend(FunctionOperation(in_op, iterable))
def contains(self: MagicT, value: Any) -> MagicT:
return self._extend(FunctionOperation(contains_op, value))
def len(self: MagicT) -> MagicT:
return self._extend(FunctionOperation(len))
def regexp(self: MagicT, pattern: Union[str, Pattern[str]], *, search: bool = False) -> MagicT:
if isinstance(pattern, str):
pattern = re.compile(pattern)
regexp_mode = pattern.search if search else pattern.match
return self._extend(FunctionOperation(regexp_mode))
def func(self: MagicT, func: Callable[[Any], Any]) -> MagicT:
return self._extend(FunctionOperation(func))
def cast(self: MagicT, func: Callable[[Any], Any]) -> MagicT:
return self._extend(CastOperation(func))