Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Expression_function



Panel
titleDescription

 Runs a single line of Python code as an expression. If a poll rate is specified, the function will be run repeatedly at the poll rate. This is a very powerful way for you to add extensions to the expression language. For example, one could write a project script module function called shared.weather.getTempAt(zip) that queried a web service for the current temperature at a given zipcode, and then bind the value of a label to the return value of that function.

The scriptFunction is a entered as a string and the pollRate is in milliseconds. You can optionally add any function arguments after the poll rate.

...

Note
titlerunScript Polling in Tags

The runScript function can be used in expression tags, but the poll rate doesn't work exactly the same as in an expression binding. All Tags have a Scan Class that dictates the minimum amount of time between each evaluation. The runScript poll rate only polls up to the rate of the scan class set on the tag.

For example, if an Expression Tag is configured with runScript to run at a poll rate of 60 seconds and is using the "default" (1 second) scan class, the Tag's Expression will still execute every 1 second. So a scan class rate of 60 seconds will be necessary for a runScript expression to poll at any rate between 0 and 60 seconds.


Panel
titleSyntax

 runScript(scriptFunction[, [ pollRate][, [argumentsargs...])

  • Parameters

string scriptFunction - A single line of python code. Typically the path to a script module.

int pollRate - The poll rate of the script.

object args - Any number of argument objects that will be passed into the given script.

  • Results

object - The return value of the specified function.


Panel
titleExamples

Here is our scripting function we are going to run that is located in a shared script called textScript.

# You could implement shared.weather.getTempAt(zip) with this Python script: # This function would query Yahoo Weather for the temperature at # the given zipcode and find the temperature using a regular expression def getTempAt(woeid=0): import system import re #Regular Expression library try: # the Yahoo URL must be created with a woeid (Where On Earth ID) based on your zip code. # look up your woeid here: http://woeid.rosselliot.co.nz/lookup/   # check if no value was entered, if so change it to Folsom CA. if woeid == 0: # the woeid for sunny Folsom, CA is: woeid = 12797905   yahooURL = "https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D" response = system.net.httpGet(yahooURL + str(woeid)) # NOTE - if you've never seen regular expressions before, don't worry, they look # confusing even to people who use them frequently. pattern = re.compile('.*?<yweather:condition (.*?)/>', re.DOTALL) match = pattern.match(response) if match: subText = match.group(1) temp = re.compile('.*?temp="(.*?)"').match(subText).group(1) return int(temp) except: system.gui.errorBox("Yahoo weather service changed") return -1
Code Block
languagepy
titleCode Snippet
- Python Function
def myFunc(text="Hello World!"):
	return text


Code Block
languageactionscript3
titleExpression Code Snippet
// run a shared function with this expression
runScript("shared.weathertextScript.getTempAtmyFunc()", 150000) //This would bind a property to run the temperature in sunny Folsom, CA, script and wouldreturn refresh itself every 15 seconds"Hello World!".


Code Block
languageactionscript3
titleExpression Code Snippet
// run a shared function dynamically with this expression
// using string concatenation. A poll rate is unnecessary, as it will refresh when the tag value changes.
runScript("shared.weathertextScript.getTempAtmyFunc('" +{Root Container.Numeric Text Field.intValue}+"_gensim_/Writeable/WriteableString1} + "')", 0) // bindsThis awould propertyrun withthe afunction dynamicand woeidpass codein andthe doesvalue notof refresh automaticallythe WriteableString1 tag.


Code Block
languageactionscript3
titleExpression Code Snippet
// run a shared function dynamically with this expression
// using optional arguments. A poll rate is unnecessary, as it will refresh when the tag value changes.
// Note the missing "()" at the end of the scriptFunction string
runScript("shared.weathertextScript.getTempAtmyFunc", 0, {Root Container.Numeric Text Field.intValue_gensim_/Writeable/WriteableString1})