[Tutor] Decision matrix

Erik Price erikprice@mac.com
Thu Feb 13 07:48:01 2003


On Thursday, February 13, 2003, at 03:30  AM, Poor Yorick wrote:

> Two problems with using a dictionary:
>
> 1.  I would like the flags to be accepted in any order.
> 2.  If I come up with other flags in the future, I would like to deal
> with them in the most simple possible manner.  Adding an exponentially
> larger number of permutations as dictionary keys doesn't seem like a
> good method.

Hey Yorick,

I was thinking that this is what you originally meant in your first 
post: that calling a combination of flags will call that combination of 
functions.  If I am wrong, stop me right here because the rest of this 
email assumes that this is what you meant.

The thing is, your example you posted to the tutor the other day seemed 
to suggest otherwise.  In your sample, you were using a combination of 
the flags to call a specific function.  In other words, if flag A was 
for func1, flag B for func2, and flag C for func3, you were saying that 
flag ABC should call func4, instead of calling each of func1, func2, 
and func3.

Am I correct?  I guess I'm having a hard time nailing down exactly what 
you want.

If I am correct, though, and you are saying that you *do* want to call 
the combination of functions indicated by the passed-in flags (and not 
an entirely different function), then I have an idea:

|>>> def func1():
|...  return "func1 called"
|...
|>>> def func2():
|...  return "func2 called"
|...
|>>> def func3():
|...  return "func3 called"
|...
|>>> funclist = {
|...  "a":func1,
|...  "b":func2,
|...  "c":func3
|...  }
|>>>
|>>> user_input = "acb"
|>>>
|>>> for flag in user_input:
|...  print "Now calling %s: '%s'" % (flag, funclist[flag]())
|...
|Now calling a: 'func1 called'
|Now calling c: 'func3 called'
|Now calling b: 'func2 called'
|>>>

Note that it even preserves the order of the flags in the user_input 
string!

Essentially, since a string can be handled as a list of characters[1], 
you can just parse the string to extract the flags and then call the 
appropriate function in the dictionary based on the character in your 
string.  In fact, it would probably be a lot better to write a function 
("parse_flags") to do this for you.  Even if the flags were more than 
one character long (i.e., "help", "run", "debug"), you could probably 
write the function to examine the user input to pick out the words, and 
call the appropriate function in the dictionary using the word as the 
key.

This works for you in two ways:

1) You don't have to come up with every possible combination of flags 
and define a new function which calls the other functions.  If I am not 
mistaken, that is what you were initially suggesting to do.  This keeps 
your dictionary very straightforward, and it doesn't grow exponentially 
as you had feared

2) You use the appropriate data structure for the job -- a dictionary.  
Really, a flag is nothing more than a "key" to some other value, 
whether it be a function or a string or a number or whatever.  When you 
have keys in a group, you should probably be using a dictionary at some 
level.

Of course, if I've been totally mistaken about what you wanted to do, 
then I apologize and you can disregard this idea.


Erik

[1] Is a string really a list of characters, or does it just allow you 
to treat it as one?  Anyone know?



>
> The solution I've been thinking of today is to code a list of possible
> functions for each flag and then find the intersection of the lists
> indicated by the flags passed into the function.  Here is a pseudocode
> example:
>
> def fList(path, mode):
>     '''return a list of files, folders, or both, optionally including
> subdirectories'''
>     #flag tuples
>     f = (Files, FilesS, FilesDirs, FilesDirsS)
>     d = (Dirs, DirsS, FilesDirs, FilesDirsS)
>     r = (DirsS, FilesS, FilesDirsS)
>     choice = Intersection(f, d, r)
>     return choice(path)
>
>
> Poor Yorick
> gp@pooryorick.com
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>





-- 
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org