-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathchaining.py
287 lines (218 loc) · 8.09 KB
/
chaining.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
Method chaining interface.
.. versionadded:: 1.0.0
"""
import typing as t
import pydash as pyd
from pydash.exceptions import InvalidMethod
from ..helpers import UNSET, Unset
from .all_funcs import AllFuncs
__all__ = (
"chain",
"tap",
"thru",
)
ValueT_co = t.TypeVar("ValueT_co", covariant=True)
T = t.TypeVar("T")
T2 = t.TypeVar("T2")
class Chain(AllFuncs, t.Generic[ValueT_co]):
"""Enables chaining of :attr:`module` functions."""
#: Object that contains attribute references to available methods.
module = pyd
invalid_method_exception = InvalidMethod
def __init__(self, value: t.Union[ValueT_co, Unset] = UNSET) -> None:
self._value = value
def _wrap(self, func) -> "ChainWrapper":
"""Implement `AllFuncs` interface."""
return ChainWrapper(self._value, func)
def value(self) -> ValueT_co:
"""
Return current value of the chain operations.
Returns:
Current value of chain operations.
"""
return self(self._value)
def to_string(self) -> str:
"""
Return current value as string.
Returns:
Current value of chain operations casted to ``str``.
"""
return self.module.to_string(self.value())
def commit(self) -> "Chain[ValueT_co]":
"""
Executes the chained sequence and returns the wrapped result.
Returns:
New instance of :class:`Chain` with resolved value from
previous :class:`Class`.
"""
return Chain(self.value())
def plant(self, value: t.Any) -> "Chain[ValueT_co]":
"""
Return a clone of the chained sequence planting `value` as the wrapped value.
Args:
value: Value to plant as the initial chain value.
"""
# pylint: disable=no-member,maybe-no-member
wrapper = self._value
wrappers = []
if hasattr(wrapper, "_value"):
wrappers = [wrapper]
while isinstance(wrapper._value, ChainWrapper):
wrapper = wrapper._value # type: ignore
wrappers.insert(0, wrapper)
clone: Chain[t.Any] = Chain(value)
for wrap in wrappers:
clone = ChainWrapper(clone._value, wrap.method)( # type: ignore
*wrap.args, # type: ignore
**wrap.kwargs, # type: ignore
)
return clone
def __call__(self, value) -> ValueT_co:
"""
Return result of passing `value` through chained methods.
Args:
value: Initial value to pass through chained methods.
Returns:
Result of method chain evaluation of `value`.
"""
if isinstance(self._value, ChainWrapper):
# pylint: disable=maybe-no-member
value = self._value.unwrap(value)
return value
class ChainWrapper(t.Generic[ValueT_co]):
"""Wrap :class:`Chain` method call within a :class:`ChainWrapper` context."""
def __init__(self, value: ValueT_co, method) -> None:
self._value = value
self.method = method
self.args = ()
self.kwargs: t.Dict = {}
def _generate(self):
"""Generate a copy of this instance."""
# pylint: disable=attribute-defined-outside-init
new = self.__class__.__new__(self.__class__)
new.__dict__ = self.__dict__.copy()
return new
def unwrap(self, value=UNSET):
"""
Execute :meth:`method` with :attr:`_value`, :attr:`args`, and :attr:`kwargs`.
If :attr:`_value` is an instance of :class:`ChainWrapper`, then unwrap it before calling
:attr:`method`.
"""
# Generate a copy of ourself so that we don't modify the chain wrapper
# _value directly. This way if we are late passing a value, we don't
# "freeze" the chain wrapper value when a value is first passed.
# Otherwise, we'd locked the chain wrapper value permanently and not be
# able to reuse it.
wrapper = self._generate()
if isinstance(wrapper._value, ChainWrapper):
# pylint: disable=no-member,maybe-no-member
wrapper._value = wrapper._value.unwrap(value)
elif not isinstance(value, ChainWrapper) and value is not UNSET:
# Override wrapper's initial value.
wrapper._value = value
if wrapper._value is not UNSET:
value = wrapper._value
return wrapper.method(value, *wrapper.args, **wrapper.kwargs)
def __call__(self, *args, **kwargs):
"""
Invoke the :attr:`method` with :attr:`value` as the first argument and return a new
:class:`Chain` object with the return value.
Returns:
New instance of :class:`Chain` with the results of :attr:`method` passed in as
value.
"""
self.args = args
self.kwargs = kwargs
return Chain(self)
class _Dash(object):
"""Class that provides attribute access to valid :mod:`pydash` methods and callable access to
:mod:`pydash` method chaining."""
def __getattr__(self, attr):
"""Proxy to :meth:`Chain.get_method`."""
return Chain.get_method(attr)
def __call__(self, value: t.Union[ValueT_co, Unset] = UNSET) -> Chain[ValueT_co]:
"""Return a new instance of :class:`Chain` with `value` as the seed."""
return Chain(value)
def chain(value: t.Union[T, Unset] = UNSET) -> Chain[T]:
"""
Creates a :class:`Chain` object which wraps the given value to enable intuitive method chaining.
Chaining is lazy and won't compute a final value until :meth:`Chain.value` is called.
Args:
value: Value to initialize chain operations with.
Returns:
Instance of :class:`Chain` initialized with `value`.
Example:
>>> chain([1, 2, 3, 4]).map(lambda x: x * 2).sum().value()
20
>>> chain().map(lambda x: x * 2).sum()([1, 2, 3, 4])
20
>>> summer = chain([1, 2, 3, 4]).sum()
>>> new_summer = summer.plant([1, 2])
>>> new_summer.value()
3
>>> summer.value()
10
>>> def echo(item):
... print(item)
>>> summer = chain([1, 2, 3, 4]).for_each(echo).sum()
>>> committed = summer.commit()
1
2
3
4
>>> committed.value()
10
>>> summer.value()
1
2
3
4
10
.. versionadded:: 1.0.0
.. versionchanged:: 2.0.0
Made chaining lazy.
.. versionchanged:: 3.0.0
- Added support for late passing of `value`.
- Added :meth:`Chain.plant` for replacing initial chain value.
- Added :meth:`Chain.commit` for returning a new :class:`Chain` instance initialized with
the results from calling :meth:`Chain.value`.
"""
return Chain(value)
def tap(value: T, interceptor: t.Callable[[T], t.Any]) -> T:
"""
Invokes `interceptor` with the `value` as the first argument and then returns `value`. The
purpose of this method is to "tap into" a method chain in order to perform operations on
intermediate results within the chain.
Args:
value: Current value of chain operation.
interceptor: Function called on `value`.
Returns:
`value` after `interceptor` call.
Example:
>>> data = []
>>> def log(value):
... data.append(value)
>>> chain([1, 2, 3, 4]).map(lambda x: x * 2).tap(log).value()
[2, 4, 6, 8]
>>> data
[[2, 4, 6, 8]]
.. versionadded:: 1.0.0
"""
interceptor(value)
return value
def thru(value: T, interceptor: t.Callable[[T], T2]) -> T2:
"""
Returns the result of calling `interceptor` on `value`. The purpose of this method is to pass
`value` through a function during a method chain.
Args:
value: Current value of chain operation.
interceptor: Function called with `value`.
Returns:
Results of ``interceptor(value)``.
Example:
>>> chain([1, 2, 3, 4]).thru(lambda x: x * 2).value()
[1, 2, 3, 4, 1, 2, 3, 4]
.. versionadded:: 2.0.0
"""
return interceptor(value)