From: bjorklund.emil on
Hello pythonistas.

I'm a newbie to pretty much both programming and Python. I have a task
that involves writing a test script for every possible combination of
preference settings for a software I'm testing. I figured that this
was something that a script could probably do pretty easily, given all
the various possibilites.

I started creating a dictionary of all the settings, where each key
has a value that is a list of the possible values for that setting.
Most of the settings are simple booleans (setting is on or off), some
of them are drop-downs with several values. For example:

settings = {
'setting_a': (True, False),
'setting_b': (True, False),
'setting_c': (1, 2, 3, 4),
}

After this I tried figuring out a function that would generate the
different possible configurations, but I couldn't quite wrap my head
around it... Are there any general patterns/structures that are suited
for this type of task? Any pointers as to how one would go about
solving something like this would be greatly appreciated. It's not
that I really need to use Python for it, but I thought it could be a
good learning excercise... :-)

Kind regards,
//Emil
From: Mensanator on
On Jul 2, 4:53 pm, "bjorklund.e...(a)gmail.com"
<bjorklund.e...(a)gmail.com> wrote:
> Hello pythonistas.
>
> I'm a newbie to pretty much both programming and Python. I have a task
> that involves writing a test script for every possible combination of
> preference settings for a software I'm testing. I figured that this
> was something that a script could probably do pretty easily, given all
> the various possibilites.
>
> I started creating a dictionary of all the settings, where each key
> has a value that is a list of the possible values for that setting.
> Most of the settings are simple booleans (setting is on or off), some
> of them are drop-downs with several values. For example:
>
> settings = {
>     'setting_a': (True, False),
>     'setting_b': (True, False),
>     'setting_c': (1, 2, 3, 4),
>
> }
>
> After this I tried figuring out a function that would generate the
> different possible configurations, but I couldn't quite wrap my head
> around it...

Basically, for each setting of a, you must do each possible b,
and for each a,b setting you must do each possible c, etc.

> Are there any general patterns/structures that are suited
> for this type of task?

Lookup "Cartesian Product".

> Any pointers as to how one would go about
> solving something like this would be greatly appreciated.

for a in [True,False]:
for b in [True,False]:
for c in [1,2,3,4]:
print 'combined settings:',a,'\t',b,'\t',c

combined settings: True True 1
combined settings: True True 2
combined settings: True True 3
combined settings: True True 4
combined settings: True False 1
combined settings: True False 2
combined settings: True False 3
combined settings: True False 4
combined settings: False True 1
combined settings: False True 2
combined settings: False True 3
combined settings: False True 4
combined settings: False False 1
combined settings: False False 2
combined settings: False False 3
combined settings: False False 4


> It's not
> that I really need to use Python for it, but I thought it could be a
> good learning excercise... :-)

You may, then, also be interested in

Permutations with Replacement
Permutations without Replacement
Combinations with Replacement
Combinations without Replacement

>
> Kind regards,
> //Emil

From: Terry Reedy on


Mensanator wrote:
> On Jul 2, 4:53 pm, "bjorklund.e...(a)gmail.com"
> <bjorklund.e...(a)gmail.com> wrote:

>> After this I tried figuring out a function that would generate the
>> different possible configurations, but I couldn't quite wrap my head
>> around it...

> Lookup "Cartesian Product".
>
>> Any pointers as to how one would go about
>> solving something like this would be greatly appreciated.
>
> for a in [True,False]:
> for b in [True,False]:
> for c in [1,2,3,4]:
> print 'combined settings:',a,'\t',b,'\t',c

This has been added to itertools at least for 2.6/3.0

>>> import itertools as it
>>> for prod in it.product((True,False), (True,False), (1,2,3,4)):
print(prod) # or run test

(True, True, 1)
(True, True, 2)
(True, True, 3)
(True, True, 4)
(True, False, 1)
(True, False, 2)
(True, False, 3)
(True, False, 4)
(False, True, 1)
(False, True, 2)
(False, True, 3)
(False, True, 4)
(False, False, 1)
(False, False, 2)
(False, False, 3)
(False, False, 4)

The sequences of sequences can, of course, be a variable:

>>> options = ((True,False), (True,False), (1,2,3,4))
>>> for prod in it.product(*options): print(prod)

does the same thing. So you can change 'options' without changing the
test runner.

tjr

From: Bruno Desthuilliers on
Terry Reedy a �crit :
>
>
> Mensanator wrote:
(snip)
>> Lookup "Cartesian Product".
(snip)
>> for a in [True,False]:
>> for b in [True,False]:
>> for c in [1,2,3,4]:
>> print 'combined settings:',a,'\t',b,'\t',c
>
> This has been added to itertools at least for 2.6/3.0


Great !
From: Jeff on
On Jul 2, 5:53 pm, "bjorklund.e...(a)gmail.com"
<bjorklund.e...(a)gmail.com> wrote:
> Hello pythonistas.
>
> I'm a newbie to pretty much both programming and Python. I have a task
> that involves writing a test script for every possible combination of
> preference settings for a software I'm testing. I figured that this
> was something that a script could probably do pretty easily, given all
> the various possibilites.
>
> I started creating a dictionary of all the settings, where each key
> has a value that is a list of the possible values for that setting.
> Most of the settings are simple booleans (setting is on or off), some
> of them are drop-downs with several values. For example:
>
> settings = {
>     'setting_a': (True, False),
>     'setting_b': (True, False),
>     'setting_c': (1, 2, 3, 4),
>
> }
>
> After this I tried figuring out a function that would generate the
> different possible configurations, but I couldn't quite wrap my head
> around it... Are there any general patterns/structures that are suited
> for this type of task? Any pointers as to how one would go about
> solving something like this would be greatly appreciated. It's not
> that I really need to use Python for it, but I thought it could be a
> good learning excercise... :-)
>
> Kind regards,
> //Emil

http://www.artfulcode.net/articles/sequential-permutations-python/