runScript
Description
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.
Syntax
runScript( scriptFunction, [pollRate] )
Examples
Code Snippet
#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(zipCode):
import
system
import
re
#Regular Expression library
yahooURL
=
"http://xml.weather.yahoo.com/forecastrss?p="
response
=
system.net.httpGet(yahooURL
+
str
(zipCode))
# 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)
else
:
system.gui.errorBox(
"Yahoo weather service changed"
)
return
-
1
Expression Code Snippet
// then Run the previous code with this expression
runScript(
"shared.weather.getTempAt('95818')"
,
15000
)
//This would bind a property to the temperature in sunny Sacramento, CA, and would refresh itself every 15 seconds.