我相信我明白你想要什么,它是用来定义图形的合成糖。在
生成的代码与Pedro Cattori的代码非常相似
主要的区别在于,在定义图之前不必定义Input。在
我改变的其他小事情是:
在函数中重命名数据流
通过wraps()调用保留修饰函数的名称
使用hasattr()而不是实例测试来允许其他类,如Input()。在import functools
class Input():
def set(self, value):
self.value = value
def execute(self):
return self.value
def Function(f):
@functools.wraps(f)
def g(*args,**kwargs):
return Executor(f,args)
return g
class Executor():
def __init__(self,f,args):
self.f = f
self.args = args
def execute(self):
return self.f(*(arg.execute() if hasattr(arg,"execute") else arg
for arg in self.args))
@Function
def f(g, seed):
return g**2 % seed
@Function
def g(a, b, h):
return a * b + h
@Function
def h(c, d):
return c / d
seed = Input()
# setting up the execution / dataflow
graph = f(g(1, 2, h(3, 4)), seed)
#you can also do in several steps
H = h(3,4)
graph2 = f(g(1,2,H),seed)
#inputs value can be late binded
seed.set(5)
# executing the dataflow
print(graph.execute()) #2.5625
#both way to the definition give same result
print(graph2.execute()) #2.5625