我不确定这是否是一个难题。我有一个功能,让我们说
def load(es , fn_set_your_data , input_file, **kwargs):
success, some_ = bulk(es, fn_set_your_data(input_file, **kwargs))
def fn_index_data( index_name , doc_type , input_file , fn_set_your_data , mapping = False , force = False):
es = Elasticsearch()
if es.indices.exists(index= index_name):
return "Index Already exists"
else:
if mapping:
es.indices.create(index=index_name, body=mapping, ignore=400)
print "Mapping is done"
load(es , fn_set_your_data , input_file , index_name = index_name , doc_type_name = doc_type)
现在有另一个函数接受这个函数作为参数,比方说global_fn。我需要将local_fn作为参数传递给global_fn,并且param split_value每次都在循环中更改。例如:
def set_your_data(input_file, index_name , doc_type_name , split_value = 1):
global global_count
for skill_ , items_ in input_file.iteritems():
main_item = items_['main_item'].strip()
main_item_split = main_item.split()
if len(main_item_split) == split_value :
query = {'item' : main_item}
yield {
"_index": index_name,
"_type": doc_type_name,
"_id": global_count,
"_source": query
}
else:
continue
if __name__ == "__main__":
index_name_list = ['percolate_bigram' , 'percolate_ngram' , 'percolate_bigram']
doc_type = 'alert'
for idx, index_name in enumerate(index_name_list):
split_value = idx
fn_index_data(index_name = index_name , doc_type = doc_type , input_file = input_data , fn_set_your_data = set_your_data , mapping = mapping)
#####我如何将split_value传递给set_your_data(local_fn),然后将其传递给fn_index_data(global_fn)。希望这段代码能给出一个良好而合理的背景。
可行,**kwargs
还是其他什么?
无论什么评论都有用。
答案 0 :(得分:1)
def set_your_data(split_value=1):
def set_your_data_inner(input_file, index_name , doc_type_name):
global global_count
for skill_ , items_ in input_file.iteritems():
main_item = items_['main_item'].strip()
main_item_split = main_item.split()
if len(main_item_split) == split_value :
query = {'item' : main_item}
yield {
"_index": index_name,
"_type": doc_type_name,
"_id": global_count,
"_source": query
}
else:
continue
return set_your_data_inner
if __name__ == "__main__":
index_name_list = ['percolate_bigram' , 'percolate_ngram' , 'percolate_bigram']
doc_type = 'alert'
for idx, index_name in enumerate(index_name_list):
fn_index_data(index_name = index_name , doc_type = doc_type , input_file = input_data , fn_set_your_data = set_your_data(idx) , mapping = mapping)