By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,257 Members | 2,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,257 IT Pros & Developers. It's quick & easy.

Generating list of possible configurations

P: n/a
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
Jul 2 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Jul 2, 4:53*pm, "bjorklund.e...@gmail.com"
<bjorklund.e...@gmail.comwrote:
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
Jul 2 '08 #2

P: n/a


Mensanator wrote:
On Jul 2, 4:53 pm, "bjorklund.e...@gmail.com"
<bjorklund.e...@gmail.comwrote:
>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

Jul 3 '08 #3

P: n/a
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 !
Jul 3 '08 #4

P: n/a
On Jul 2, 5:53*pm, "bjorklund.e...@gmail.com"
<bjorklund.e...@gmail.comwrote:
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/s...ations-python/
Jul 3 '08 #5

P: n/a
On Jul 3, 2:13�am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
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 !
Well, it will be great at some point in the future
when Python 2.6/3.0 have actually been released and
third party extensions such as gmpy have caught up.

Until then, such solutions are worthless, i.e., of
no value.
Jul 3 '08 #6

P: n/a


Mensanator wrote:
On Jul 3, 2:13�am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>Terry Reedy a �crit :
>>This has been added to itertools at least for 2.6/3.0
Great !

Well, it will be great at some point in the future
when Python 2.6/3.0 have actually been released and
The betas 'have actually been released' and I am happily using 3.0. The
core features that I care about have mostly been untouched and where
they have, they work.
third party extensions such as gmpy have caught up.
Not my personal concern. And certainly not a direct concern for nearly
all uses of itertools.product.
Until then, such solutions are worthless, i.e., of no value.
Why are you so anxious to generalize your personal negative views to
everyone else. Worthless to you, worthwhile to me. And for someone who
does not need cross-products today or in the next few months,
potentially valuable information for when the final releases do arrive,
maybe in September.

tjr

Jul 3 '08 #7

P: n/a
On Jul 3, 2:52*pm, Terry Reedy <tjre...@udel.eduwrote:
Mensanator wrote:
On Jul 3, 2:13�am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Terry Reedy a �crit :
This has been added to itertools at least for 2.6/3.0
Great !
Well, it will be great at some point in the future
when Python 2.6/3.0 have actually been released and

The betas 'have actually been released' and I am happily using 3.0. *
But you're not using it for production work. Unless you're ignoring
the recommendations.
The
core features that I care about have mostly been untouched and where
they have, they work.
That's not the issue.
>
third party extensions such as gmpy have caught up.

Not my personal concern. *
Right. Which is why your 3.0 specific advice is worthless.
Those ARE personal concerns of just about everyone else.
And certainly not a direct concern for nearly
all uses of itertools.product.
Until then, such solutions are worthless, i.e., of no value.

Why are you so anxious to generalize your personal negative views to
everyone else. *
Not to everyone else, to the new and inexperienced Pyhton users.
Maybe you don't remember what it was like being a new user where
you need to have such things pointed out to you. And I'm not being
negative, just realistic. If I ask a question today, I want an
answer I can use tomorrow, not one I can use six months from now.
Worthless to you, worthwhile to me. *
The OP's opinion is the only one that matters. What do you suppose
is the percentage of posts on this newsgroup by those using 3.0?
And for someone who
does not need cross-products today or in the next few months,
potentially valuable information for when the final releases do arrive,
maybe in September.
That's fine for them. It's been said here that they will be a minority
for a long time.
>
tjr
Jul 3 '08 #8

P: n/a
On Jul 3, 5:49*pm, Mensanator <mensana...@aol.comwrote:
On Jul 3, 2:52*pm, Terry Reedy <tjre...@udel.eduwrote:
Worthless to you, worthwhile to me. *

The OP's opinion is the only one that matters.
I bet the OP doesn't know (or care) what gmpy is.
What do you suppose
is the percentage of posts on this newsgroup by those using 3.0?
Taking into account 2.6 too (we're not talking about only 3.0 here),
probably not much less than those who even know what is gmpy, let
alone dismiss a beta Python release because their obscure pet module
is not available yet.
You will probably sound less negative if you refrain from projecting
your own very specialized needs to those of the average pythonista.

George
Jul 3 '08 #9

P: n/a
On Jul 3, 6:24*pm, George Sakkis <george.sak...@gmail.comwrote:
On Jul 3, 5:49*pm, Mensanator <mensana...@aol.comwrote:
On Jul 3, 2:52*pm, Terry Reedy <tjre...@udel.eduwrote:
Worthless to you, worthwhile to me. *
The OP's opinion is the only one that matters.

I bet the OP doesn't know (or care) what gmpy is.
But he'll care if he tries to use something specific to
2.6 and it fails and he doesn't know why.
>
What do you suppose
is the percentage of posts on this newsgroup by those using 3.0?

Taking into account 2.6 too (we're not talking about only 3.0 here),
probably not much less than those who even know what is gmpy, let
alone dismiss a beta Python release because their obscure pet module
is not available yet.
That was just an example. When you consider ALL the pet
modules like PIL, Numpy, Win32, etc., that's a lot, isn't it.
You will probably sound less negative if you refrain from projecting
your own very specialized needs to those of the average pythonista.
Funny how you don't complain when Mr. Reedy projects HIS
specialized needs to the average pythonista.

I was just trying to be helpful (I admit I often sound
negative when I'm not trying to be).
>
George
Jul 3 '08 #10

P: n/a
On Jul 3, 6:54*am, Mensanator <mensana...@aol.comwrote:
>
Well, it will be great at some point in the future
when Python 2.6/3.0 have actually been released and
third party extensions such as gmpy have caught up.

Until then, such solutions are worthless, i.e., of
no value.
gmpy is supported on Python 2.6. A new version and Windows binaries
were released shortly after 2.6b1 was released.

casevh
Jul 4 '08 #11

P: n/a
On Jul 3, 7:51 pm, Mensanator <mensana...@aol.comwrote:
On Jul 3, 6:24 pm, George Sakkis <george.sak...@gmail.comwrote:
Taking into account 2.6 too (we're not talking about only 3.0 here),
probably not much less than those who even know what is gmpy, let
alone dismiss a beta Python release because their obscure pet module
is not available yet.

That was just an example. When you consider ALL the pet
modules like PIL, Numpy, Win32, etc., that's a lot, isn't it.
A few points:
- The OP acknowledged he's a newbie, and as a newbie he'll probably
spend some time getting used to the language and the standard library
before jumping to the dozens 3rd party packages.
- I am sure many Python users are productive without ever touching an
external package; that is after all the point of "batteries included".
- Even if they do have external dependencies, chances are that they
are pure Python modules, which typically work without modification on
new 2.x versions.
I was just trying to be helpful (I admit I often sound
negative when I'm not trying to be).
Well, something like "Until then, such solutions are worthless, i.e.,
of no value" is too strong, subjective and biased to be really
helpful.

George
Jul 4 '08 #12

P: n/a
On Jul 3, 8:10�pm, casevh <cas...@gmail.comwrote:
On Jul 3, 6:54�am, Mensanator <mensana...@aol.comwrote:
Well, it will be great at some point in the future
when Python 2.6/3.0 have actually been released and
third party extensions such as gmpy have caught up.
Until then, such solutions are worthless, i.e., of
no value.

gmpy is supported on Python 2.6. A new version and Windows binaries
were released shortly after 2.6b1 was released.
Great!

Looks like I'll be installing 2.6 this weekend.
>
casevh
Jul 4 '08 #13

P: n/a
On Jul 3, 9:09�pm, George Sakkis <george.sak...@gmail.comwrote:
On Jul 3, 7:51 pm, Mensanator <mensana...@aol.comwrote:
On Jul 3, 6:24 pm, George Sakkis <george.sak...@gmail.comwrote:
Taking into account 2.6 too (we're not talking about only 3.0 here),
probably not much less than those who even know what is gmpy, let
alone dismiss a beta Python release because their obscure pet module
is not available yet.
That was just an example. When you consider ALL the pet
modules like PIL, Numpy, Win32, etc., that's a lot, isn't it.

A few points:
- The OP acknowledged he's a newbie, and as a newbie he'll probably
spend some time getting used to the language and the standard library
before jumping to the dozens 3rd party packages.
- I am sure many Python users are productive without ever touching an
external package; that is after all the point of "batteries included".
- Even if they do have external dependencies, chances are that they
are pure Python modules, which typically work without modification on
new 2.x versions.
Yes, these points are valid, although I think
mine are valid also. But there's no point in any
further arguing.
>
I was just trying to be helpful (I admit I often sound
negative when I'm not trying to be).

Well, something like "Until then, such solutions are worthless, i.e.,
of no value" is too strong, subjective and biased to be really
helpful.
I was trying NOT to imply "broken" or "doesn't
do anything useful". I guess I'll have to try to
be less succinct.
>
George
Jul 4 '08 #14

P: n/a
Wow, I didn't have time to look back on this thread for a while,
surprised of the activity. Anyhow, thanks for the answers, and thanks
for pointing out that the itertools-variants are not available in 2.5.

Cheers!

//emil
Jul 4 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.