Turning Strings into Functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Freddy

    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
  • Terry Reedy

    #2
    Re: Turning Strings into Functions


    "Freddy" <freddycooper01 @yahoo.com> wrote in message
    news:46610d4e.0 309251516.45a8e b01@posting.goo gle.com...[color=blue]
    > How do you turn a string into a callable function?[/color]

    If functions are defined as module scope, as is usual, an example:
    [color=blue][color=green][color=darkred]
    >>> def f(*args): print args[/color][/color][/color]
    ....[color=blue][color=green][color=darkred]
    >>> globals()['f'](*(1,'a'))[/color][/color][/color]
    (1, 'a')

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

    Terry J. Reedy


    Comment

    • Richard Brodie

      #3
      Re: Turning Strings into Functions


      "Freddy" <freddycooper01 @yahoo.com> wrote in message
      news:46610d4e.0 309251516.45a8e b01@posting.goo gle.com...
      [color=blue]
      > 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.[/color]

      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



      Comment

      • Freddy

        #4
        Re: Turning Strings into Functions

        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.Brodie@rl.ac .uk> wrote in message news:<bl0usu$j5 g@newton.cc.rl. ac.uk>...[color=blue]
        > "Freddy" <freddycooper01 @yahoo.com> wrote in message
        > news:46610d4e.0 309251516.45a8e b01@posting.goo gle.com...
        >[color=green]
        > > 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.[/color]
        >
        > 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[/color]

        Comment

        Working...