-
Notifications
You must be signed in to change notification settings - Fork 0
/
pypline.py
46 lines (34 loc) · 1.03 KB
/
pypline.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
class pypline:
def __init__(self, iter):
self.iter = iter
self.args = []
self.kwargs = {}
def __ror__(self, other):
return self.iter(other, *self.args, **self.kwargs)
def __or__(self, other):
return self.iter(other, *self.args, **self.kwargs)
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
return self
class pypline_conversion:
def __init__(self, func):
self.func = func
self.args = []
self.kwargs = {}
def __or__(self, other):
return (x for x in other if self.func(x, *self.args, **self.kwargs))
def __ror__(self, other):
return (x for x in other if self.func(x, *self.args, **self.kwargs))
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
return self
#region utilities
sort = pypline(sorted)
reverse = pypline(reversed)
@pypline
def display(iter, file=None):
for item in iter:
print(item, file=file)
#endregion