-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelined.py
42 lines (31 loc) · 1.06 KB
/
pipelined.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
from types import MethodType
class Pipeline:
def __init__(self, funcs):
if not isinstance(funcs, list):
funcs = [funcs]
self.funcs = funcs
def add(self, func):
self.funcs.append(func)
def __call__(self, instance, *args):
args = [instance] + list(args)
for func in self.funcs:
args = [instance] + [func(*args)]
return args[-1]
def __get__(self, instance=None, owner=None):
if not instance and owner:
return self
return MethodType(self, instance)
class PipelineNamespace(dict):
def __setitem__(self, key, value):
if key in self:
if not isinstance(self[key], Pipeline):
super(PipelineNamespace, self).__setitem__(key, Pipeline(self[key]))
self[key].add(value)
else:
super(PipelineNamespace, self).__setitem__(key, value)
class PipelinedMeta(type):
@classmethod
def __prepare__(mcs, name, bases):
return PipelineNamespace()
class Pipelined(metaclass=PipelinedMeta):
pass