forked from robinhilliard/pipes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extras.py
103 lines (85 loc) · 3.11 KB
/
extras.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
from typing import (
Callable,
Generic,
TypeVar,
)
from typing_extensions import Concatenate, ParamSpec
from pipe_operator.python_flow.base import Pipe, PipeStart
from pipe_operator.shared.exceptions import PipeError
from pipe_operator.shared.utils import (
is_one_arg_lambda,
)
TInput = TypeVar("TInput")
TOutput = TypeVar("TOutput")
TValue = TypeVar("TValue")
FuncParams = ParamSpec("FuncParams")
class Then(Generic[TInput, TOutput]):
"""
Pipe-able element that allows the use of 1-arg lambda functions in the pipe.
The lambda must take only 1 argument and can be typed explicitly if necessary.
Args:
f (Callable[[TInput], TOutput]): The function that will be called in the pipe.
Raises:
PipeError: If `f` is not a 1-arg lambda function.
Examples:
>>> (
... PipeStart("1")
... >> Then[str, int](lambda x: int(x) + 1)
... >> Then(lambda x: x + 1)
... >> PipeEnd()
... )
3
"""
__slots__ = ("f", "args", "kwargs", "is_tap", "is_thread")
def __init__(self, f: Callable[[TInput], TOutput]) -> None:
self.f = f
self.args = ()
self.kwargs = {} # type: ignore
self.is_tap = False
self.is_thread = False
self.check_f()
def check_f(self) -> None:
"""f must be a 1-arg lambda function."""
if not is_one_arg_lambda(self.f):
raise PipeError(
"`Then` only supports 1-arg lambda functions. Use `Pipe` instead."
)
def __rrshift__(self, other: PipeStart) -> PipeStart[TOutput]:
# Never called, but needed for typechecking
return other.__rshift__(self)
class Tap(Pipe[TInput, FuncParams, TInput]):
"""
Pipe-able element that produces a side effect and returns the original value.
Useful to perform async actions or to call an object's method that changes the state
without returning anything.
Args:
f (Callable[Concatenate[TInput, FuncParams], object]): The function that will be called in the pipe.
args (FuncParams.args): All args (except the first) that will be passed to the function `f`.
kwargs (FuncParams.kwargs): All kwargs that will be passed to the function `f`.
Examples:
>>> class BasicClass
... def __init__(self, x: int) -> None:
... self.x = x
... def increment(self) -> None:
... self.x += 1
>>> (
... PipeStart(1)
... >> Pipe(BasicClass)
... >> Tap(lambda x: x.increment())
... >> Then(lambda x: x.x + 3)
... >> Tap(lambda x: x + 100)
... >> PipeEnd()
... )
5
"""
def __init__(
self,
f: Callable[Concatenate[TInput, FuncParams], object],
*args: FuncParams.args,
**kwargs: FuncParams.kwargs,
) -> None:
kwargs["_tap"] = True
super().__init__(f, *args, **kwargs) # type: ignore
def __rrshift__(self, other: PipeStart) -> PipeStart[TInput]:
# Never called, but needed for typechecking
return other.__rshift__(self)