関数クラスターをチェーン化して、指定された初期パラメーターを使用して一度に呼び出すことができ、最終結果を取得できることを定義したいのですが、Linuxコマンドパイプチェーンとして機能する可能性があります。
test01() | test02() | test03() | test04()
[init_parameter] ----------------------------------------------------> [final result]
私は、関数シーケンスを追加/削減/リミックスできることを検討しています。たとえば、次のようになります。
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
これらのコマンド間のデータストリームは「テキスト」タイプと見なすことができるため、これは機能します。
ただし、Pythonでは、これらの不明な関数については、基本的に、入力パラメーターと戻り結果のさまざまなタイプの可能性があります。これらの関数を呼び出したい場合は、その定義を知っている必要があります。例えば
>>> 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日実行する必要があり、200個の関数をチェーンにランダムに選択し、str_in_asciiout_out(100)は最後の位置にあります。運が良ければ、199時間無駄になるまでおっとするかもしれません。
そのため、時間の無駄な呼び出しの前に、関数がその能力を示すことができるかどうかを知りたいのです。
上記のコードは私が知っている醜い解決策なので、私の問題に対してより良い解決策があるかどうかを確認するためにアイデアを貼り付けます。