我要定义一个函数集群可以链接并调用给定则得到最终的结果,初始参数,一个去,我希望它可以作为Linux命令管道链:python函数可以足够聪明地显示它的能力吗?
test01() | test02() | test03() | test04()
[init_parameter] ----------------------------------------------------> [final result]
我在condsidering功能序列可以被添加/减少/再混合,例如:
test02() | test03() | test01() | test04()
[init_parameter] ----------------------------------------------------> [final result]
test03()| test01() | test04() | test01()
[init_parameter] ----------------------------------------------------> [final result]
我还希望这些功能可以配有嵌入其能力指示符可被用于智能参数预检测,例如,如果输入不是该函数可以接受或输入的类型超过其最大处理能力,链可以只是忽略了这个计算流而不是使用“try ... except ...”来捕捉那些“逻辑”错误。
请参见下面的代码,未经考验,只为说明我的想法:
def test01(f_inspect=false, a, **b):
my_capability = {
"function_cluster": "A01",
"input_acceptable": type([]),
"input_element_acceptable": type(1),
"input_length_max": 100,
"return_type": type([]),
"return_element": type(1),
"return_length_max": 100,
}
if f_inspect:
return my_capability
return [x+1 for x in a] # just sample and may raise error by python such as div 0
def test02(f_inspect=false, a, **b):
# similiar as test01
def test03(f_inspect=false, a, **b):
# similiar as test01
def test04(f_inspect=false, a, **b):
# similar as test01
#==========================================
#test if function chain is compatible
def f_capability_compatible(current_,next_):
return True if
next_["function_cluster"] == current_["function_cluster"] and
next_["input_acceptable"] is current_["return_type"] and
next_["input_length_max"] >= current_["return_element"] and
next_["input_length_max"] >= current_["return_length_max"]
return False
foos = [test01,test02,test03,test04]
from itertools import permutations
mypermutations = permutations(foos, 3) # get permutations of testXX
init_parameter = [x for x in range(1,100)]
dummy_function_parameter = {
"function_cluster": "A01",
"input_acceptable": type([]),
"input_element_acceptable": type(1),
"input_length_max": 100,
"return_type": type([]),
"return_element": type(1)
"return_length_max": 100,
}
chain_flag = [True for x in range(len(mypermutations))]
#[True, True, True, ..... True, True, True, True, True, True, True]
for x in len(mypermutations):
tmp_para = dummy_function_parameter
for y in mypermutations[x]:
test_result = f_capability_compatible(tmp_para,y(f_inspect=true))
chain_flag[x] = test_result
tmp_para = y(f_inspect=true)
if test_result == False :
print "this can not be chained due to parameter incompatible at position %s" %x
break
#==========================================
result_of_chain = []
# to invoke:
for x in len(mypermutations):
if chain_flag[x] == True:
try :
# invoking my_capability[x] chain in a go
tmp_return = init_parameter
for f in mypermutations[x]:
tmp_return = f(tmp_return) #parameter of testXX(a)
except :
result_of_chain[x] = "Error"
else:
result_of_chain[x] = "Incomp"
这里是我的问题, 它可以使这个函数链和联合的想法更简单吗?
============================================= ==========================================
更新为什么我需要谓词参数和返回值类型:
在Linux命令行,我们可以使用的命令是这样的:
$ cat sometfile | grep something | awk '{print $0}' | grep something > file
这工作,因为这些命令之间的数据流可以被认为是“文本”类型。
但是,对于那些未知的函数,基本上有输入参数和返回结果的多种可能性。如果我想调用这些函数,我必须知道它的定义。例如
>>> def str_in_asciiout_out(str):
return ord(str)
>>>
>>> str_in_asciiout_out("a")
97
>>> str_in_asciiout_out(100)
# oops, I have to, try… expect…
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
str_in_asciiout_out(100)
File "<pyshell#0>", line 2, in str_in_asciiout_out
return ord(str)
TypeError: ord() expected string of length 1, but int found
试着......除了......是正确和正确的编码方式。
但是,如果我想将数百个str_in_asciiout_out()函数合并到一个未知序列中,我所关注的是序列在短时间内可以传递的最佳最终结果。
例如,只是示例 假设我有定义1000层的功能,每个功能可能需要运行1天通过给定的输入以获得输出,I随机挑选200个功能为一链,并且 str_in_asciiout_out(100) 在运气不佳的最后一个位置,我可能会得到一个哎呀,直到浪费了199个小时。
这就是为什么我想知道该函数是否可以在耗时调用之前显示它的能力。
上面的代码是我知道的一个难看的解决方案,所以我粘贴这个想法,看看是否有更好的解决方案来解决我的问题。
这似乎是一个很大的麻烦去避免写一个`try:... except:`block。 – Amber 2011-01-30 05:07:49
你应该描述你试图解决什么问题,或者每个人都会试图猜测它是什么。为什么你需要检测某种功能(哪些以及为什么这些功能?)以及为什么你需要提前使用它们? – 2011-01-30 05:21:40