470,648 Members | 1,521 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

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
Apr 27 '06 #1
6 1417
val bykoski <va*@vtek.com> wrote:

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.


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, ti**@probo.com
Providenza & Boekelheide, Inc.
Apr 27 '06 #2
On 27/04/2006 10:38 AM, val bykoski wrote:
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


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
Apr 27 '06 #3
val bykoski wrote:
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


# 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
Apr 27 '06 #4
val
John Machin wrote:
On 27/04/2006 10:38 AM, val bykoski wrote:
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


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


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

Apr 27 '06 #5
val
Hi Kent,
Thanks. Great help. It does work now,
and with expressions as well.
my very best,
Val

Apr 27 '06 #6
val
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

Apr 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by JustSomeGuy | last post: by
7 posts views Thread by dam_fool_2003 | last post: by
19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
7 posts views Thread by heltena | last post: by
7 posts views Thread by Jim Lewis | last post: by
17 posts views Thread by 2005 | last post: by
40 posts views Thread by nufuhsus | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.