Frits
Frits

Reputation: 89

How to create a function that returns a function of the product of a list of functions

I would like to create a function that returns a function of the product of a list of functions. The list of functions should be of variable length and the functions should have different parameters.

E.g.:

def f(a, b, **kwargs):
    return a + b

def g(c, d, **kwargs):
    return c + d

def product_function(function_list, **kwargs):
    ...
    <create function that returns product function of functions in 
     function_list>
    ...
    return <productfunction>

In the example above this would be something like:

my_function = product_function([f,g])

This should return a function that can be used as if it was defined as:

def my_function(a, b, c, d):
    return f(a, b) * g(c, d)

I would like to use this for iterating over a list of combinations of factors and optimising parameters for these combinations to select the most predictive one in a data science project.

Upvotes: 0

Views: 419

Answers (1)

Aran-Fey
Aran-Fey

Reputation: 43156

You can do this with some help from the introspection utilities in the inspect module.

Specifically, I used inspect.signature to find each function's positional and keyword arguments, and Signature.bind_partial to prevent clashes between positional and keyword arguments. The following is a generic implementation of a function that combines other functions:

import inspect

def generic_operator_function(operator_function, default_value,
                              function_list, **kwargs):
    POSITIONALS = {inspect.Parameter.POSITIONAL_ONLY,
                   inspect.Parameter.POSITIONAL_OR_KEYWORD}
    KEYWORDS = {inspect.Parameter.POSITIONAL_OR_KEYWORD,
                inspect.Parameter.KEYWORD_ONLY}

    # if no functions were given, return the default value
    if not function_list:
        return lambda: default_value

    # for each function in the list, find out how many positional
    # arguments it accepts. Also find out which keyword arguments
    # it accepts.
    arg_maps = []
    kwarg_names = []
    for func in function_list:
        sig = inspect.signature(func)
        params = sig.parameters.values()

        # count the positional arguments and map them to
        # parameter names
        bound_args = sig.bind_partial(**kwargs).arguments
        arg_map = [param.name for param in params if param.kind in POSITIONALS
                                                  and param.name not in bound_args]
        arg_maps.append(arg_map)

        # find the names of all keyword arguments
        if any(param.kind == inspect.Parameter.VAR_KEYWORD for param in params):
            kwnames = True
        else:
            kwnames = {param.name for param in params if param.kind in KEYWORDS}
        kwarg_names.append(kwnames)

    # return a function that iterates through the function_list and
    # multiplies all results
    def combined_func(*args, **inner_kwargs):
        value = default_value

        i = 0
        for func, arg_map, kwnames in zip(function_list, arg_maps, kwarg_names):
            # if the function takes **kwargs, pass all kwargs. Otherwise, pass
            # only those that it supports.
            kw_arguments = kwargs.copy()
            kw_arguments.update(inner_kwargs)
            if kwnames is not True:
                kw_arguments = {k: v for k, v in kw_arguments.items() if k in kwnames}

            # take the next batch of arguments, but only those that aren't already
            # provided as keyword arguments
            arg_map = [arg for arg in arg_map if arg not in kw_arguments]
            numparams = len(arg_map)
            arguments = args[i:i+numparams]
            kw_arguments.update({arg: value for arg, value in zip(arg_map, arguments)})

            # call the function
            retval = func(**kw_arguments)
            value = operator_function(value, retval)

            i += numparams

        return value

    return combined_func

With this, you can easily define a bunch of functions similar to your product_function:

import operator

def product_function(*args, **kwargs):
    return generic_operator_function(operator.mul, 1, *args, **kwargs)

def sum_function(*args, **kwargs):
    return generic_operator_function(operator.add, 0, *args, **kwargs)

def append_function(*args, **kwargs):
    return generic_operator_function(lambda x, y: x+[y], [], *args, **kwargs)
>>> my_function = product_function([f,g])
>>> my_function(1,2, 3,4)
21
>>> sum_function([f,g])(1,2, 3,4)
10
>>> append_function([f,g])(1,2, 3,4)
[3, 7]

And it correctly passes on only those keyword arguments that each function supports:

>>> p = product_function([f,g], a=1, c=2)
>>> p(3, 4)
24

Upvotes: 1

Related Questions