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

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 2 Dec 2010 13:59:07 -0500

Hi Ken,
Maybe I'm still not getting it. When I use the code below, the function doesn't 
print anything out.

# A dictionary of functions that could possibly run.
# Note that we have parentheses in the values because it litterally runs them 
as code.

myfuncs = { "func1" : "func1()",
                "func2" : "func2()" }

# Two functions with parameters that we don't use.

def func1(x = "x"):
  """Func1: Print something to prove this runs"""
  print "func 1"

def func2(x = "x"):
  """Func2: Print something to prove this runs"""
  print "func 2"

# Run the code contained in the first element of the above dictionary.

myfuncs["func1"]

raw_input("Press enter to quit")

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%20Wiki/Forms/AllPages.aspx>

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

Actually I don't know why you guys are dead set on using eveal this is how I do 
it and it gives you the ability to add parameters later without changing the 
code.  In fact I have used this in all kinds of ways to help sort functions for 
weird reasons.  Here is how I do a dictionary of functions.




def func1():
    print "func 1"


def func2():
    print "func 2"

myFuncs={"s1":func1,"s2":func2}


myFuncs["s2"]()







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

Hi,
Here is code that uses a dictionary rather than a list. Note that I will later 
add the exception code. But this works.

# A dictionary of functions that could possibly run.
# Note that we have parentheses in the values because it litterally runs them 
as code.

myfuncs = { "func1" : "func1()",
                "func2" : "func2()" }

# Two functions with parameters that we don't use.

def func1(x = "x"):
  """Func1: Print something to prove this runs"""
  print "func 1"

def func2(x = "x"):
  """Func2: Print something to prove this runs"""
  print "func 2"

# Run the code contained in the first element of the above dictionary.

eval(myfuncs["func1"])

raw_input("Press enter to quit")

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%20Wiki/Forms/AllPages.aspx>

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

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%20Wiki/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%20Wiki/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: