I am testing a function. I would like to know what are the results respect to various parameters.
If the function has only one parameter then it is easy:
def func_a(a):
return a+1
def test_func(func,parameter_list):
return {t:func(t) for t in parameter_list}
print (test_func(func_a,[2,4,5]))
prints
{2: 3, 4: 5, 5: 6}
Here instead I do it with two parameters:
def strategy_2(a,b):
return a+b
def test_2_strategies(strategy,list_of_lists):
result={}
for l1 in list_of_lists[0]:
for l2 in list_of_lists[1]:
result[(l1,l2)]=strategy_2(l1,l2)
return result
print (test_2_strategies(strategy_2,[[1,2,3],[0.3,0.6]]))
and the result is:
{(1, 0.3): 1.3, (1, 0.6): 1.6, (2, 0.3): 2.3, (2, 0.6): 2.6, (3, 0.3): 3.3, (3, 0.6): 3.6}
Perfect.
But what if I wanted to make a similar function where the list of lists could have n lists inside. And test all the combinations.
I looked at decorators, lambda, functools.partial, but I seem to be unable to do it.
I tried for example this:
def func_b(a,b):
return a+b
def test_func(func,parameter_list):
return {t:func(t) for t in parameter_list}
def test_strategy_many_parameters(func,parameter_lists):
if len (parameter_lists)>1:
test_strategy_many_parameters(test_func(lambda x: func(x,y),parameter_lists[0]), parameter_lists[1:])
else:
return test_func(func,parameter_lists[0])
but it does not work.
I am now looking at this question: Passing functions with arguments to another function in Python?
but I fail to see how to apply it in this case.
I would like something like this:
def func(x1,x2,...,xn):
result=...
return result
def test_func(func,list_of_lists):
...
print(test_func(func,[[x1min,...,x1max],[x2min,...,x2max],...,[xnmin,...,xnmax]])
and the result would be:
{(x1min, x2min,...,xnmin):func(x1min, x2min,...,xnmin) , ,(x1max, x2max,...,xnmax):func(x1max, x2max,...,xnmax)}
What's an elegant way to do it (or just a way to do it).
Thanks.