473,795 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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":"Thi s 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 1564

"Freddy" <fr************ @yahoo.com> wrote in message
news:46******** *************** ***@posting.goo gle.com...
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.goo gle.com...
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":"Thi s 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.goo gle.com...
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":"Thi s 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
2713
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 the string itself, rather, it needs to update a variable that is an array or points to an array. Correct? Below is a 'simple' test to work with a string that was created in an external function (and external file). Based on the result I...
4
12288
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 not, why am I not getting warnings with `gcc -ansi -pedantic blah.c' Secondly , is there a function in `C' for comparing strings ignoring their cases? Only criterion is this has to be ANSI compliant (being
4
10733
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 ASCII characters in the 0-127 range, although I had thought that I might want to use the 128-255 range for special symbols or foreign character codes. This all worked OK for a long time, but a recent update to the compiler on my system has...
7
5641
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 strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
3
1270
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 they can all see it. This works fine. My question is if there is any reason NOT to go ahead and declare ALL strings at the top? Is there a performance/memory hit in doing that? Or is it simply a matter of clean code syntax/layout?
4
455
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 COMPACT COHERENT CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/ /* The basics of C++ are Classes, that build Types. Which are used to create quick and dirty routines in the smallest possible space. The Classes & Routines uses the...
28
6065
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 C-professional, just a hobby-programmer trying to teach it myself. I found C rather difficult without those lists (and corresponding string-functions). Slowly getting into C-strings, I thought, what if I take a C-string and
95
5429
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 ____________________________________ | | | ------------------ | | | BUTTON | | | ...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.