473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Turning Strings into Functions

How do you turn a string into a callable function? I'm writing a
program which takes user input and then looks up the associated
function to run from a shelved dictionary. So, I have a dictionary
with entries like this:
myDict["Case1"] = {"desc":"This is Case1", "func1": "Handler1",
"func2" : "Handler2", "args": "ACCEPT"}

Then my program gets the string "Case 1" from the user, and needs to
assign func1 and func2 as handlers for certain events.
The argument to the handlers are sockets, and I can't know in advance
what they will be.

I need to assign the handler function before it is called. So I need
to turn the string into a function (there IS a function in this module
called Handler1() ). I don't think I can use an exec call, because
that would require me to build a string with the argument first.

Any suggestions?

Freddy
Jul 18 '05 #1
3 1546

"Freddy" <fr************@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
How do you turn a string into a callable function?


If functions are defined as module scope, as is usual, an example:
def f(*args): print args .... globals()['f'](*(1,'a'))

(1, 'a')

Or you can define your own dict mapping names to functions.

Terry J. Reedy
Jul 18 '05 #2

"Freddy" <fr************@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
How do you turn a string into a callable function? I'm writing a
program which takes user input and then looks up the associated
function to run from a shelved dictionary.


I see you have an answer already, so I'll give you a question.
Is there any particular reason why you are storing the name
of the function in the dictionary rather than the function itself?

i.e. myDict["Case1"] = {"desc":"This is Case1", "func1": Handler1} etc

Jul 18 '05 #3
I guess I could do that. Right now I'm creating the use cases
dictionary in a totally separate module which knows nothing about my
main module's functions. But I will try either moving the code into my
current module, or including the testcases.py into my higher level
module.
Thanks for the suggestion.

Freddy

"Richard Brodie" <R.******@rl.ac.uk> wrote in message news:<bl********@newton.cc.rl.ac.uk>...
"Freddy" <fr************@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
How do you turn a string into a callable function? I'm writing a
program which takes user input and then looks up the associated
function to run from a shelved dictionary.


I see you have an answer already, so I'll give you a question.
Is there any particular reason why you are storing the name
of the function in the dictionary rather than the function itself?

i.e. myDict["Case1"] = {"desc":"This is Case1", "func1": Handler1} etc

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: c_monty | last post by:
I am used to Delphi and VB, where functions can return strings. I recently starting learning C and my findings are that you can have external functions build strings, but the function cannot return...
4
by: Trying_Harder | last post by:
Firstly, are functions defined in strings.h (`strcasecmp' and `strncasecmp') ANSI? if yes , then why am I getting an implicit declaration warning only with -Wall (Warnings all) flag? and if...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
3
by: darrel | last post by:
Often, I want to use a string in multiple functions on a page. I can declare and re-set the string in each function, or I can just delcare the string outside of the individual functions so that...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.