Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96513
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Subject | Re: Phone Tree |
| References | <6ae5c632-b061-4b86-87ae-24c50435445b@googlegroups.com> |
| Date | 2015-09-13 20:03 +0200 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.485.1442167403.8327.python-list@python.org> (permalink) |
Somewhere in there you may find that dictionary dispatching is
something worth doing. I don't know. This whole sort of problem
is sort of grating, in that it is trying to replicate one of the
most irritating user experiences on the planet ...
From python3: Patterns, Recipes and Idioms
http://www.google.se/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CEUQFjAEahUKEwiJv4CtxPTHAhVqvnIKHY68Bec&url=http%3A%2F%2Fpython-3-patterns-idioms-test.readthedocs.org%2Fen%2Flatest%2FMultipleDispatching.html&usg=AFQjCNG0UFOKpxJNVDSCt9dtAJ55SC_zEA
The guts of the thing is a simple dictionary:
outcome = {
(Paper, Rock): Outcome.WIN,
(Paper, Scissors): Outcome.LOSE,
(Paper, Paper): Outcome.DRAW,
(Scissors, Paper): Outcome.WIN,
(Scissors, Rock): Outcome.LOSE,
(Scissors, Scissors): Outcome.DRAW,
(Rock, Scissors): Outcome.WIN,
(Rock, Paper): Outcome.LOSE,
(Rock, Rock): Outcome.DRAW,
}
Which, in this case, plays the child's game rock, paper, scissors with
2 players. But you could use a tuple with 10 values.
If a lot of your outcomes are going to arrive at the same answer 'Stick
the device in a box, print off this shipping label, and send it via
UPS to our repair service centre.' then this approach will have more
appeal.
Sometimes what you want is to dispatch on a set of functions with
arguments, and you can stick that in your dictionary as well.
outcomes = { (a,c,d) : (function1_name, (arg1, arg2, arg3))
(a, b, d) : (function2_name, (arg1,))}
But whether this is going to help or not really depends on your data.
If you find yourself itching for a case statement, then it probably
will. But I am not sure that this was your intent in the first place.
Laura
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Phone Tree Azureaus <ltoshea@gmail.com> - 2015-09-13 07:39 -0700 Re: Phone Tree Chris Angelico <rosuav@gmail.com> - 2015-09-14 00:59 +1000 Re: Phone Tree Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-13 12:53 -0400 Re: Phone Tree Laura Creighton <lac@openend.se> - 2015-09-13 20:03 +0200 Re: Phone Tree Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-13 23:57 +0000
csiph-web