简体   繁体   English

将参数传递给函数,该参数必须作为参数传递给python中的另一个函数

[英]Passing an argument to the function , which has to be passed as an argument to another function in python

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 . 现在有另一个函数接受该函数作为参数,比如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 . 我需要将local_fn作为参数传递给global_fn,参数split_value每次在循环中都会更改。 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 ) . #####我如何将split_value传递给set_your_data(local_fn),然后将其传递给fn_index_data(global_fn)。 Hope this code is giving a good and reasonable context . 希望这段代码能提供一个良好而合理的环境。

Is it doable , with **kwargs or something ? **kwargs东西可行吗? Whatever comments will be useful. 任何评论都会有用。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递/更改一个函数的参数,该参数作为另一个函数的参数传递 - passing/changing arguments of a function which is passed as an argument in another function Python-将函数作为参数传递给另一个函数 - Python - passing a function as an argument to another function 通过Python中的另一个函数传递函数参数 - Passing a function argument through another function in Python 如何调试已作为参数传递给另一个 function 的 function - How to debug a function that has been passed as an argument to another function 在python中将函数作为参数传递 - passing a function as an argument in python 不带参数传递Python函数 - Passing Python function with no argument python将参数传递给函数 - python passing argument to a function python lambda“绑定变量”如何与返回的 function 值一起工作,该值又作为另一个参数传递? - How does a python lambda 'bound variable' work with a returned function value which is in turn passed as another argument? 调用带有参数的 .Net 函数形式 python(通过引用传递) - Calling a .Net function form python which takes an argument (passed by reference) Python Executor - 以参数作为参数传递函数 - Python Executor - Passing function with argument as argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM