Reputation: 495
I am not sure whether it is a hard question or not . I am having a function , lets say
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)
Now there is another function which accepts this function as an argument , lets say global_fn . I need to pass local_fn as an argument to global_fn , with the param split_value changing every time in a loop . Eg:
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)
##### How i pass split_value to set_your_data ( local_fn ) and then pass this to fn_index_data ( global_fn ) . Hope this code is giving a good and reasonable context .
Is it doable , with **kwargs
or something ?
Whatever comments will be useful.
Upvotes: 0
Views: 61
Reputation: 1663
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)
Upvotes: 1