Generating list of possible configurations

B

bjorklund.emil

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
 
M

Mensanator

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
 
T

Terry Reedy

Mensanator said:
On Jul 2, 4:53 pm, "(e-mail address removed)"
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
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:

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

tjr
 
B

Bruno Desthuilliers

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 !
 
J

Jeff

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/
 
M

Mensanator

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.
 
T

Terry Reedy

Mensanator said:
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
 
M

Mensanator

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.
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.


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.
 
G

George Sakkis

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
 
M

Mensanator

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.
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).
 
C

casevh

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
 
G

George Sakkis

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
 
M

Mensanator

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.
 
M

Mensanator

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.
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.
 
B

bjorklund.emil

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top