将参数传输到组合函数的最内层调用 [英] Transmit parameter to the inner-most call of a composed function
问题描述
我正在构建数据流水线工具,希望能够一起编译一系列功能.
I'm building a data pipelining tool and want to be able to compile a series of functions together.
所有函数都作用于可迭代对象,并将可迭代对象作为产生的值传递.
All the functions act on an iterable, and pass an iterable as a yielded value.
因此,如果f(x)
过滤器和g(x)
进行编辑,并且h(x)
生成一个随机值,那么我希望能够组合它们的组合,以便可以根据要求调用f(g(h(x)))
或h(f(g(x)))
.
So if f(x)
filters, and g(x)
edits, and h(x)
generates a random value, then I want to be able to compose combinations of these so I can call f(g(h(x)))
or h(f(g(x)))
as per requirements.
我希望能够在不知道x
将会是什么之前准备这些合成(它们具有不同于初始迭代的不同参数签名).
I'd like to be able to prepare these compositions (they have different parameter signatures beyond the initial iterable) before knowing what x
is going to be.
为了使事情复杂化,f
,g
和h
在共享它们消耗和发出的可迭代参数时,它们具有不同的参数签名.
To complicate matters, f
, g
and h
, while sharing the iterable parameter that they both consume and emit, they have different parameter signatures.
为了方便起见,我可以将这些参数包装在partial
语句中以隐藏杂乱的参数-但接下来,我想根据f(g(h(x)))
示例构成一系列功能-这个想法是我想穿鞋-在运行时对特定的x
进行迭代.
For convenience, I can wrap these parameters up in a partial
statement to hide the messy parameters - but next I want to compose a series of functions as per the f(g(h(x)))
example - the idea being that I want to shoe-horn a particular x
iterable at runtime.
我发现对partial
进行了功能处理,随后嵌套它们意味着我无法访问该最深的参数-并且我遇到类似AttributeError: 'generator' object has no attribute 'keywords'
的错误.
I'm finding that having partial
'ed the functions, subsequently nesting them means I can't access that deepest parameter - and I'm getting errors like AttributeError: 'generator' object has no attribute 'keywords'
.
换句话说,是否有一种方法可以让您推迟在运行时指定最内层函数所需的参数来链接或嵌套函数?
In other words, is there a way to chain or nest functions in a way that allows you to defer specifying a parameter required by the innermost function at runtime?
例如以下工作正常:
data_source = [ OrderedDict({"id" : "1", "name" : "Tom", "sync" : "a"}),
OrderedDict({"id" : "2", "name" : "Steve", "sync" : "a"}),
OrderedDict({"id" : "3", "name" : "Ulrich", "sync" : "b"}),
OrderedDict({"id" : "4", "name" : "Victor", "sync" : "b"}),
OrderedDict({"id" : "5", "name" : "Wolfgang", "sync" : "c"}),
OrderedDict({"id" : "6", "name" : "Xavier", "sync" : "c"}),
OrderedDict({"id" : "7", "name" : "Yves", "sync" : "c"}),
OrderedDict({"id" : "8", "name" : "Zaphod", "sync" : "d"}),
OrderedDict({"id" : "9", "name" : "Albert", "sync" : "d"})]
def f(x, filt):
for content in x:
if content['name']==filt:
print ("test")
yield content
def g(x,old, new):
for content in x:
if content["name"]==old:
content["name"]=new
yield content
def h(x, which):
for content in x:
if random.random()>0.5:
content[which]=random.randint(0,100)
yield content
p_f = partial(f, filt="Albert")
p_g = partial(g, old="Yves", new="Yeti")
p_h = partial(h, which='id')
iterator=(d for d in data_source)
for result in p_f(p_g(p_h(iterator))):
print (result)
哪个输出:
OrderedDict([('id', '1'), ('name', 'Tom'), ('sync', 'a')])
OrderedDict([('id', 57), ('name', 'Steve'), ('sync', 'a')])
OrderedDict([('id', '3'), ('name', 'Ulrich'), ('sync', 'b')])
OrderedDict([('id', '4'), ('name', 'Victor'), ('sync', 'b')])
OrderedDict([('id', 33), ('name', 'Wolfgang'), ('sync', 'c')])
OrderedDict([('id', '6'), ('name', 'Xavier'), ('sync', 'c')])
OrderedDict([('id', 83), ('name', 'Yeti'), ('sync', 'c')])
OrderedDict([('id', '8'), ('name', 'Zaphod'), ('sync', 'd')])
test
OrderedDict([('id', '9'), ('name', 'Albert'), ('sync', 'd')])
但是我希望能够尽早编写该函数-稍后绑定所组合函数的迭代器.
But I want to be able to compose that function early - and bind the iterator of the composed function later.
类似的东西:
p_compiled = p_f(p_g(p_h))
for result in p_compiled(iterator):
print (result)
但是当我这样做时,我得到了TypeError: 'generator' object is not callable
.
But when I do this, I get TypeError: 'generator' object is not callable
.
推荐答案
听起来您只需要一个compose()
函数:
It sounds like you just want a compose()
function:
def compose(f, g):
return lambda x: f(g(x))
p_compiled = compose(p_f, compose(p_g, p_h))
for result in p_compiled(iterator):
print (result)
这篇关于将参数传输到组合函数的最内层调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!