I'm not sure how verbose I should go so please ask for elaboration if this is too terse.
Is there a way to pack the for a,b,c in product(d['a'],d['b'],d['c']):
in some syntactical sugar so I would only need to type mute variables a,b,c
only once for this loop itself?
Something like this may be?
my_for_loop('a','b','c'):
API_Call1(a)
API_Call2(b,c)
instead of
for a,b,c in product(d['a'],d['b'],d['c']):
API_Call1(a)
API_Call2(b,c)
How could my_for_loop
look like? I am a bit lost at how to approach this conceptually.
More detail:
I have an API that requires calling it for each cell of a cartesian product of some lists. I am using the product function to avoid nested loops. Let's say we have a list1 and a list2 it can be done in a following way
from itertools import product
for a,b in product(list1,list2):
API_Call(a,b)
I have created a dictionary_of_lists={'a':list1,'b':list2,'c':list3...}
to be able to write it like this
for a,b in product(dictionary_of_lists['a'],dictionary_of_lists['b']):
API_Call(a,b)
for c,b in product(dictionary_of_lists['c'],dictionary_of_lists['b']):
API_Call(c,b)
for e,f,g,h in product(dictionary_of_lists['e'],dictionary_of_lists['f'],dictionary_of_lists['g'],dictionary_of_lists['h'],):
API_Call1(e,f,g,h)
API_Call2(e,h)
...
So basically the variables that the loop creates are used in that API calls and they are mute otherwise, their name doesn't matter. There are many of these calls and there is some convoluted logic around them. So I would like to keep the loop itself simple and should I need to change the variables I won't have to to change them at three places for each such loop.
my_for_loop('a','b'):
API_Call(a,b)
my_for_loop('b','c'):
API_Call(c,b)
my_for_loop('e','f','g','h'):
API_Call1(e,f,g,h)
API_Call2(e,h)
...
ADDITION: I have simplified a few things but was taken by surprise where exactly ambiguity was lurking :-)
Thanks for all the answers so far!
It's a good suggestion to have the dproduct wrapper. I have one indeed, just did not want to preempt your suggestions.
The variable names are mute for the code logic but they have some meaning for the sake of maintenance of the code. So they can not consist of a single letter each.
In an attempt to clarify further: I would like to avoid using the variable names three times - in the "for ..." part, in the call to dproduct wrapper and in the API calls. Two times - in the call to the wrapper and in the API calls is OK because it reflects the logic.
Below is a more elaborated example of the code I have now.
def dproduct(d, keys):
subset_d = dict((k, d[k]) for k in keys if k in d)
return product(*[subset_d.values()])
for foo, bar in dproduct(d, ['foo','bar',]):
some logic here
if API_Call1(foo,bar) == 123:
some other stuff, API_Call6(quux,baz,)
some more stuff and a call to another dproduct
for quux, sl, baz in dproduct(d, ['quux','sl','baz',]):
blah blah, API_Call2(quux,sl,baz)
other stuff
for pa, mf in dproduct(d, ['pa','mf',]):
API_Call4(pa,mf)
for quux, sl, baz in dproduct(d, ['quux','sl','baz',]):
further logic
if API_Call1(quux, sl, baz) == 342: some other stuff
some more stuff and a call to another dproduct
for pa,mf in dproduct(d, ['pa','mf',]):
API_Call3(pa,mf)