RE: Python: Making A Program Run A Function That Is Not Hard Coded

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 2 Dec 2010 10:15:00 -0500

You don't need the quotes around the func in the dictionary.

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: Thursday, December 02, 2010 8:56 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Python: Making A Program Run A Function That Is Not Hard Coded

 

Hi,

When I try to execute this code, nothing prints to the screen. How do I
debug it?

 

myfuncs = { "string1" : "func1", 

                "string2" : "func2" }

 

def func1():

  print "func 1"

 

def func2():

  print "func 2"

 

  eval(myfuncs["string1"])

 

raw_input("Press enter to quit")

 

Thanks.

 

Jim

 

Jim Homme,

Usability Services,

Phone: 412-544-1810. Skype: jim.homme

Internal recipients,  Read my accessibility blog
<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx> . Discuss
accessibility here
<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx> .
Accessibility Wiki: Breaking news and accessibility advice
<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%2
0Wiki/Forms/AllPages.aspx> 

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Ken Perry
Sent: Thursday, December 02, 2010 8:06 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Python: Making A Program Run A Function That Is Not Hard Coded

 

When I went looking for the best way to do this it turns out that a
dictionary works as a perfect case statement and does all your checking at
the same time. Note if you don't use has_key you will need to put it in a
try block like this:

 

MyFuncs={"string1",func1,"string2",func2}

 

Def func1(blah):

  Do something

 

Def func2(bla):

  Do something

 

Try:

  Myfuncs["string1"]("pass in")

Except:

  Don't do nothing

 

Note I added a parameter just to show it can be done.  Make sure to make the
parameters match if you have parameters all functions must have them or at
least defaults.

 

Ken

 

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: Thursday, December 02, 2010 7:55 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Python: Making A Program Run A Function That Is Not Hard Coded

 

Hi,

I'm trying to be clear about this question, but maybe this can't be done. 

 

Let's say that you have a list that contains twenty strings. 

Your program reads in a string from a file.

The string from the file matches one of the strings in your list.

You want to perform a function that may be associated with that string.

You don't want to write twenty if statements to see if the string in the
file matches and perform the function associated with that string.

You want to somehow get the program to see the string and the function
associated with it and just perform it.

 

Is this possible?

 

Thanks.

 

Jim

 

Jim Homme,

Usability Services,

Phone: 412-544-1810. Skype: jim.homme

Internal recipients,  Read my accessibility blog
<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx> . Discuss
accessibility here
<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx> .
Accessibility Wiki: Breaking news and accessibility advice
<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%2
0Wiki/Forms/AllPages.aspx> 

 

 

  _____  

This e-mail and any attachments to it are confidential and are intended
solely for use of the individual or entity to whom they are addressed. If
you have received this e-mail in error, please notify the sender immediately
and then delete it. If you are not the intended recipient, you must not
keep, use, disclose, copy or distribute this e-mail without the author's
prior permission. The views expressed in this e-mail message do not
necessarily represent the views of Highmark Inc., its subsidiaries, or
affiliates.

Other related posts: