list of functions question

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

    #1

    list of functions question

    Hi The List:
    I have a modeling app where i'm detecting events (in temporal
    dynamics) applying a set of (boolean) functions - kind of:

    event_list = "f1 f2 etc".split() # each fi detects a specific event
    i have defs for functions fi, or simple boolean expressions for each, so
    that evList is a list of defs or boolean expressions
    for ev in evList:
    if ev: # this supposedly is a call ev(t)
    # doing smth with the event

    I didn't succeed, though, (blindly) trying various options.
    I thought/tried "apply()" but couldn't get it work.
    I'd appreciate pointers to how to handle this kind of
    functions or events lists (or objects?) and how to call those
    functions in a loop.
    thanks,val
  • Tim Roberts

    #2
    Re: list of functions question

    val bykoski <val@vtek.com > wrote:[color=blue]
    >
    >Hi The List:
    > I have a modeling app where i'm detecting events (in temporal
    >dynamics) applying a set of (boolean) functions - kind of:
    >
    >event_list = "f1 f2 etc".split() # each fi detects a specific event
    >i have defs for functions fi, or simple boolean expressions for each, so
    >that evList is a list of defs or boolean expressions
    >for ev in evList:
    > if ev: # this supposedly is a call ev(t)
    > # doing smth with the event
    >
    > I didn't succeed, though, (blindly) trying various options.
    >I thought/tried "apply()" but couldn't get it work.
    >I'd appreciate pointers to how to handle this kind of
    >functions or events lists (or objects?) and how to call those
    >functions in a loop.[/color]

    If you hadn't tried the string shortcut, it would have worked:

    event_list = [f1, f2, etc]

    As it is, event_list is a list of strings, not a list of functions.

    Then, when you want to call it, remember that it has to be treated like a
    function:

    for ev in event_list:
    if ev(t):
    pass

    If you REALLY need the list of functions to come from a string, you can
    also do:

    event_list = [eval(f) for f in "f1 f2 etc".split()]
    --
    - Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • John Machin

      #3
      Re: list of functions question

      On 27/04/2006 10:38 AM, val bykoski wrote:[color=blue]
      > Hi The List:
      > I have a modeling app where i'm detecting events (in temporal
      > dynamics) applying a set of (boolean) functions - kind of:
      >
      > event_list = "f1 f2 etc".split() # each fi detects a specific event
      > i have defs for functions fi, or simple boolean expressions for each, so
      > that evList is a list of defs or boolean expressions
      > for ev in evList:
      > if ev: # this supposedly is a call ev(t)
      > # doing smth with the event
      >
      > I didn't succeed, though, (blindly) trying various options.
      > I thought/tried "apply()" but couldn't get it work.
      > I'd appreciate pointers to how to handle this kind of
      > functions or events lists (or objects?) and how to call those
      > functions in a loop.
      > thanks,val[/color]

      This may be something like what you are trying to achieve:

      # untested
      def fx(arg):
      pass
      def fy(arg):
      pass
      def fdefault(arg):
      pass

      funcmap = {
      'x1': fx,
      'x2': fx,
      'y' : fy,
      }

      eventlist = "y x2 x2 x1 y".split()
      for ev in eventlist:
      efunc = funcmap.get(ev, fdefault)
      if efunc(t): # what is t????
      # do something

      Comment

      • Kent Johnson

        #4
        Re: list of functions question

        val bykoski wrote:[color=blue]
        > Hi The List:
        > I have a modeling app where i'm detecting events (in temporal
        > dynamics) applying a set of (boolean) functions - kind of:
        >
        > event_list = "f1 f2 etc".split() # each fi detects a specific event
        > i have defs for functions fi, or simple boolean expressions for each, so
        > that evList is a list of defs or boolean expressions
        > for ev in evList:
        > if ev: # this supposedly is a call ev(t)
        > # doing smth with the event[/color]

        # Make a list of the actual functions, not their names
        # For the events that are expressions, encapsulate them in functions
        event_list = [f1, f2, etc]

        # Call each one:
        for ev in event_list:
        if ev(t):
        # do something

        Kent

        Comment

        • val@vtek.com

          #5
          Re: list of functions question

          John Machin wrote:[color=blue]
          > On 27/04/2006 10:38 AM, val bykoski wrote:[color=green]
          > > Hi The List:
          > > I have a modeling app where i'm detecting events (in temporal
          > > dynamics) applying a set of (boolean) functions - kind of:
          > >
          > > event_list = "f1 f2 etc".split() # each fi detects a specific event
          > > i have defs for functions fi, or simple boolean expressions for each, so
          > > that evList is a list of defs or boolean expressions
          > > for ev in evList:
          > > if ev: # this supposedly is a call ev(t)
          > > # doing smth with the event
          > >
          > > I didn't succeed, though, (blindly) trying various options.
          > > I thought/tried "apply()" but couldn't get it work.
          > > I'd appreciate pointers to how to handle this kind of
          > > functions or events lists (or objects?) and how to call those
          > > functions in a loop.
          > > thanks,val[/color]
          >
          > This may be something like what you are trying to achieve:
          >
          > # untested
          > def fx(arg):
          > pass
          > def fy(arg):
          > pass
          > def fdefault(arg):
          > pass
          >
          > funcmap = {
          > 'x1': fx,
          > 'x2': fx,
          > 'y' : fy,
          > }
          >
          > eventlist = "y x2 x2 x1 y".split()
          > for ev in eventlist:
          > efunc = funcmap.get(ev, fdefault)
          > if efunc(t): # what is t????
          > # do something[/color]

          John,
          Thanks alot. It does work with the fx, fy,etc as (boolean)
          expressions.
          And it is faster vs. using defs.
          Thanks again, the great list...
          Val

          Comment

          • val@vtek.com

            #6
            Re: list of functions question

            Hi Kent,
            Thanks. Great help. It does work now,
            and with expressions as well.
            my very best,
            Val

            Comment

            • val@vtek.com

              #7
              Re: list of functions question

              Tim,
              Greatly appreciate your help. You are right - the functions work
              from the list; i don't actually need the string with events.
              Thanks again - great list and great people...
              Val

              Comment

              Working...