Phone Tree
Laura Creighton
lac at openend.se
Sun Sep 13 14:03:11 EDT 2015
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
More information about the Python-list
mailing list