What is the correct way to call a function that has been assigned to a variable? I want to call a specific function depending on a parameter like this:
Decide Function Based on Column Name
Assigned to the variable it looks like this
I attempt to use it like this
count, ds = self.custom.searchFunction(combinedDs, returnDs=True)
This Error Happens
Thanks,
Nick
When you assigned it to a Perspective property, it was stringified. Perspective properties cannot hold arbitrary jython objects.
ok, if that is something that I cannot change, I’ll just choose which function to call at the time of execution instead of trying to stick it in a variable.
There is no way to overcome this “stringification” correct?
You’re basically already making a map/dictionary. I assume you use this on some button press or something but in your onAction
extension function (or wheverever, doesn’t really matter) you can do something like
funcs = {
"recircCount": common.flexsort.getRecircCount,
"overflowCount": common.flexsort.getOverflowCount,
"redLightCount": common.flexsort.getRedLightCount,
...
}
# some logic to figure out what key to use
keyToUse = ?
try:
funcs[keysToUse]()
except KeyError:
# Opportunity to use a fallback function if keyToUse is not in dictionary
someDefaultFunc()
If you need to call this function mapping function in multiple places, you could also put it in a project script like
def callMappedFunction(keyToUse, *args, **kwargs):
funcs = {
"recircCount": common.flexsort.getRecircCount,
"overflowCount": common.flexsort.getOverflowCount,
"redLightCount": common.flexsort.getRedLightCount,
...
}
try:
return funcs[keyToUse](*args, **kwargs)
except KeyError:
return defaultFunction(*args, **kwargs)
and call this where you need it on your view.
There is a way to go from the string name of your library to importing/calling the function using importlib I believe, but would probably recommend against it and just hardcode/white list what functions you want to allow.
Brian’s approach is good, but funcs
needs to be at the top level of a project script to be most efficient.
2 Likes
I will try this and update the results here later on
1 Like
Not at the moment, no. There may be hope for the future, though. (: