在python中使用什么符号来调用函数_如何创建一个在Python中不立即求值的符号函数执行DAGs?...

我相信我明白你想要什么,它是用来定义图形的合成糖。在

生成的代码与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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值