Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Avoiding if..elsif statements

Reply
Thread Tools

Avoiding if..elsif statements

 
 
unexpected
Guest
Posts: n/a
 
      08-25-2006
I have a program where based on a specific value from a dictionary, I
call a different function. Currently, I've implemented a bunch of
if..elsif statements to do this, but it's gotten to be over 30 right
now and has gotten rather tedious. Is there a more efficient way to do
this?

Code:

value = self.dictionary.get(keyword)[0]

if value == "something":
somethingClass.func()
elsif value == "somethingElse":
somethingElseClass.func()
elsif value == "anotherthing":
anotherthingClass.func()
elsif value == "yetanotherthing":
yetanotherthingClass.func()

Is it possible to store these function calls in a dictionary so that I
could just call the dictionary value?

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-25-2006
"unexpected" <(E-Mail Removed)> wrote:

> I have a program where based on a specific value from a dictionary, I
> call a different function. Currently, I've implemented a bunch of
> if..elsif statements to do this, but it's gotten to be over 30 right
> now and has gotten rather tedious. Is there a more efficient way to do
> this?
>
> Code:
>
> value = self.dictionary.get(keyword)[0]
>
> if value == "something":
> somethingClass.func()
> elsif value == "somethingElse":
> somethingElseClass.func()
> elsif value == "anotherthing":
> anotherthingClass.func()
> elsif value == "yetanotherthing":
> yetanotherthingClass.func()
>
> Is it possible to store these function calls in a dictionary so that I
> could just call the dictionary value?


but of course (did you try it?). here's an outline:

dispatch = {
"something": somethingClass.func, # note: no () here
"somethingElse": somethingElseClass.func,
"anotherthing": anotherthingClass.func,
"yetanotherthing": yetanotherthingClass.func,
}

...

dispatch[value]() # note: do the call here!

or, a bit more robust:

try:
func = dispatch[value]
except KeyError:
print "- no handler for", value
else:
func()

tweak as necessary.

</F>



 
Reply With Quote
 
 
 
 
Daniel Nogradi
Guest
Posts: n/a
 
      08-25-2006
> Code:
>
> value = self.dictionary.get(keyword)[0]
>
> if value == "something":
> somethingClass.func()
> elsif value == "somethingElse":
> somethingElseClass.func()
> elsif value == "anotherthing":
> anotherthingClass.func()
> elsif value == "yetanotherthing":
> yetanotherthingClass.func()
>
> Is it possible to store these function calls in a dictionary so that I
> could just call the dictionary value?


How about (untested):

def x():
print 'x'

def y():
print 'y'

funcdict={ 'valuex': x, 'valuey': y }

funcdict['valuex']()
 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      08-25-2006
unexpected wrote:
> I have a program where based on a specific value from a dictionary, I
> call a different function. Currently, I've implemented a bunch of
> if..elsif statements to do this, but it's gotten to be over 30 right
> now and has gotten rather tedious. Is there a more efficient way to do
> this?
>
> Code:
>
> value = self.dictionary.get(keyword)[0]
>
> if value == "something":
> somethingClass.func()
> elsif value == "somethingElse":
> somethingElseClass.func()
> elsif value == "anotherthing":
> anotherthingClass.func()
> elsif value == "yetanotherthing":
> yetanotherthingClass.func()
>
> Is it possible to store these function calls in a dictionary so that I
> could just call the dictionary value?


Yup.

dispatch = dict(
something = somethingClass.func,
somethingElse = somethingElseClass.func,
anotherthing = anotherthingClass.func,
yetanotherthing = yetanotherthingClass.func
)

def default():
pass


# call it like this

dispatch.get(switch_value, default)()

 
Reply With Quote
 
Chaz Ginger
Guest
Posts: n/a
 
      08-25-2006
unexpected wrote:
> I have a program where based on a specific value from a dictionary, I
> call a different function. Currently, I've implemented a bunch of
> if..elsif statements to do this, but it's gotten to be over 30 right
> now and has gotten rather tedious. Is there a more efficient way to do
> this?
>
> Code:
>
> value = self.dictionary.get(keyword)[0]
>
> if value == "something":
> somethingClass.func()
> elsif value == "somethingElse":
> somethingElseClass.func()
> elsif value == "anotherthing":
> anotherthingClass.func()
> elsif value == "yetanotherthing":
> yetanotherthingClass.func()
>
> Is it possible to store these function calls in a dictionary so that I
> could just call the dictionary value?
>

Why not do it this way?

foo =
{'something':somethingClass.func,'somethingelse':s omethingelseClass.func)

if foo.has_key(value) :
foo[value]()
else :
raise OMG, "%s isn't known" % value



 
Reply With Quote
 
unexpected
Guest
Posts: n/a
 
      08-25-2006
the missing () was the trick!

However, I'm passing in a few variables, so I can't just take it
out-though every single function would be passing the same variables.

so something.func() is actually
something.func(string, list)

How would I modify it to include them? Sorry I didn't include them the
first time, I was trying to simplify it to make it easier...oops!

Fredrik Lundh wrote:
> "unexpected" <(E-Mail Removed)> wrote:
>
> > I have a program where based on a specific value from a dictionary, I
> > call a different function. Currently, I've implemented a bunch of
> > if..elsif statements to do this, but it's gotten to be over 30 right
> > now and has gotten rather tedious. Is there a more efficient way to do
> > this?
> >
> > Code:
> >
> > value = self.dictionary.get(keyword)[0]
> >
> > if value == "something":
> > somethingClass.func()
> > elsif value == "somethingElse":
> > somethingElseClass.func()
> > elsif value == "anotherthing":
> > anotherthingClass.func()
> > elsif value == "yetanotherthing":
> > yetanotherthingClass.func()
> >
> > Is it possible to store these function calls in a dictionary so that I
> > could just call the dictionary value?

>
> but of course (did you try it?). here's an outline:
>
> dispatch = {
> "something": somethingClass.func, # note: no () here
> "somethingElse": somethingElseClass.func,
> "anotherthing": anotherthingClass.func,
> "yetanotherthing": yetanotherthingClass.func,
> }
>
> ...
>
> dispatch[value]() # note: do the call here!
>
> or, a bit more robust:
>
> try:
> func = dispatch[value]
> except KeyError:
> print "- no handler for", value
> else:
> func()
>
> tweak as necessary.
>
> </F>


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-25-2006
"unexpected" <(E-Mail Removed)> wrote:

> However, I'm passing in a few variables, so I can't just take it
> out-though every single function would be passing the same variables.
>
> so something.func() is actually
> something.func(string, list)
>
> How would I modify it to include them?


just add the parameters to the call:

dispatch[value](string, list) # note: do the call here!

in Python, an explicit call is always written as

expression(argument list)

where expression yields a callable object. in your original case,
the expression was a bound method; in the modified example,
the expression is a dictionary lookup. the actual call part looks
the same way, in both cases.

</F>



 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      08-26-2006
unexpected wrote:
> Currently, I've implemented a bunch of
> if..elsif statements to do this, but it's gotten to be over 30 right
> now and has gotten rather tedious. Is there a more efficient way to do
> this?


Use something other than Perl.




Carl Banks

 
Reply With Quote
 
Ghalib Suleiman
Guest
Posts: n/a
 
      08-26-2006
Try it and see. Functions are first-class citizens in Python.

On Aug 25, 2006, at 6:36 PM, unexpected wrote:

> I have a program where based on a specific value from a dictionary, I
> call a different function. Currently, I've implemented a bunch of
> if..elsif statements to do this, but it's gotten to be over 30 right
> now and has gotten rather tedious. Is there a more efficient way to do
> this?
>
> Code:
>
> value = self.dictionary.get(keyword)[0]
>
> if value == "something":
> somethingClass.func()
> elsif value == "somethingElse":
> somethingElseClass.func()
> elsif value == "anotherthing":
> anotherthingClass.func()
> elsif value == "yetanotherthing":
> yetanotherthingClass.func()
>
> Is it possible to store these function calls in a dictionary so that I
> could just call the dictionary value?
>
> --
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
Tal Einat
Guest
Posts: n/a
 
      08-27-2006

Fredrik Lundh wrote:
> "unexpected" <(E-Mail Removed)> wrote:
>
> > However, I'm passing in a few variables, so I can't just take it
> > out-though every single function would be passing the same variables.
> >
> > so something.func() is actually
> > something.func(string, list)
> >
> > How would I modify it to include them?

>
> just add the parameters to the call:
>
> dispatch[value](string, list) # note: do the call here!
>


This will work great if all of your functions recieve the same
argument(s). If not, there are still simple solutions.

I would suggest a solution like this, since it's simple and generic:

class Command (object):
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
args = self.args+args
kw.update(self.kw)
apply(self.func, args, kw)

An instance of the Command class can be called just like a function,
and it will call the orginial function with the arguments it was
instantiated with. (You can also pass additional arguments at the call
itself)

dispatch = {
"something": Command(somethingClass.func),
"somethingElse": Command(somethingElseClass.func, "moo",
[1,2,3]),
"anotherthing": Command(anotherthingClass.func, 'a', 'b', 'c'),
"yetanotherthing": Command(yetanotherthingClass.func,
verbose=True),
}

dispatch[value]()

- Tal Einat
reduce(lambda m,x:[m[i]+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*1[3]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM
Re: Avoiding latches Keith R. Williams VHDL 0 07-14-2003 05:09 PM



Advertisments