Execute function on all possible combinations of parameters

Viewed 5399

I have sets of values that I want to apply as parameters to a function:

params = {
    'a': [1, 2, 3],
    'b': [5, 6, 7],
    'x': [None, 'eleven', 'f'],
    # et cetera
}

I want to run myfunc() with all possible combinations, so myfunc(a=1, b=5, x=None ...), myfunc(a=2, b=5, x=None ...) ... myfunc(a=3, b=7, x='f' ...). Is there something (for example in itertools) that can help? I thought about using itertools.product() but that doesn't keep the names of the parameters and just gives me tuples of the combinations.

3 Answers

I developed combu which is that solution.

  1. Install combu

pip install combu

  1. Use combu
# Case 1: Directly call
import combu

for res, param in combu.execute(myfunc, params):
    print(res, params)

# Case 2: Use class
from combu import Combu

comb = Combu(myfunc)

for res, param in comb.execute(params):
    print(res, params)
Related