Call a Project Library function by reference (without using runScript)?

I want to be able pass a reference to a function to be executed by another function. I do not want to use runScript().
The "master" function will ...

  1. do a bunch of preprocessing stuff that is similar no matter which function was passed as an argument
  2. execute the passed function (with arguments) which knows the details of the particular operation
  3. do a bunch of postprocessing stuff that is similar no matter which function was passed as an argument

runScript isn't relevant because that is a function of the expression language.

Functions are first class objects in Python. You can pass them as the parameter to a function and then use them.

>>> def foo(msg):
...     print msg
... 
>>> def bar(f):
...     f("hello")
... 
>>> bar(foo)
hello
>>> 

Yep. That's one of the reasons I don't want to use it.

Ok, that's basically what I'm doing now, except that the function reference is being retrieved from a view custom property (which I assume is the problem). The function doesn't appear to be executing and no errors in the log. I'll kick it around some more. I just wanted to make sure I'm not spitting in the wind.

View/component/session properties in Perspective are 'flattened' down to JSON types. True object references aren't possible. You'll have to come up with some other way to do what you're trying to do entirely; maybe describe it better and someone can come up with something to help?

1 Like

Thank you. I'll give it some more thought.

This is the definition of a decorator.

For instance, you might want a function that looks similar to this:

def masterDecorator(func):
    def innerFunc(*args, **kwargs):
        print "before execution"

        returnedValue = func(*args, **kwargs)

        print "after execution"

        return returnedValue
    return innerFunc

Then you can decorate any function that you want:

@masterDecorator
def sumTwoNumbers(a,b):
    return a + b

print sumTwoNumbers(2,3)

Output:

>>> 
before execution
after execution
5
>>> 
2 Likes

Thanks. I'll check it out.

How would a decorator handle "interruptions" to the process where the operator may have to decide on a course of action. I normally use sort of "state" system where I step through a process. In this case as I step through the process I manipulate a state tag such as a view custom property. This process means that the original call is not what is actually executing, hence the need to save the "core" function argument somewhere so that once I get to that point I know who to call.