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

  • From: "qubit" <lauraeaves@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 2 Dec 2010 14:34:56 -0600

Hi Ty --
I disagree about the try block -- try blocks are defined to assist in error 
recovery. The sample code Ken sent maybe skips the exceptions, but you could 
change that by adding handlers for the various exceptions.
Just musing.
--le

  ----- Original Message ----- 
  From: Littlefield, Tyler 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Thursday, December 02, 2010 1:48 PM
  Subject: Re: Python: Making A Program Run A Function That Is Not Hard Coded


  On another note, I wouldn't surround your function call in a try block like 
Ken did. That's bad practice and will just mask the errors. If you get errors, 
they should generally be dealt with, not hidden. If you do anything in that 
function that throws an error, it'll just be skipped because the call to it was 
wrapped in a try block.
  On 12/2/2010 12:35 PM, Homme, James wrote: 
    Hi Ty, 

    Can you please explain what you said? 

      

    Thanks. 

      

    Jim 

      

    Jim Homme, 

    Usability Services, 

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

    Internal recipients,  Read my accessibility blog. Discuss accessibility 
here. Accessibility Wiki: Breaking news and accessibility advice 

      

    From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Littlefield, Tyler
    Sent: Thursday, December 02, 2010 2:27 PM
    To: programmingblind@xxxxxxxxxxxxx
    Subject: Re: Python: Making A Program Run A Function That Is Not Hard Coded 

     

    Use Ken's code, not what Jim gave you. Jim's code will add a key and a 
return value to the dict where as Ken's will allow you to call the code with 
dict['func]()
    On 12/2/2010 12:13 PM, Ken Perry wrote: 

    That is not my code below. 

     

    ken 

     

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

     

    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. Discuss accessibility 
here. Accessibility Wiki: Breaking news and accessibility advice 

     

    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. Discuss accessibility 
here. Accessibility Wiki: Breaking news and accessibility advice 

     

    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. Discuss accessibility 
here. Accessibility Wiki: Breaking news and accessibility advice 

     

    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. Discuss accessibility 
here. Accessibility Wiki: Breaking news and accessibility advice 

     

     

    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. 






--      Thanks,  Ty  


-- 

Thanks,
Ty

Other related posts: