54.7 Other Python syntax options

TreeAge Pro supports several syntax options for calling Python functions. The standard ones previously mentioned as well as others are described in the following table.

Function Syntax

Description

myPythonFunction

Call Python functions that require no arguments by simply referencing the function name.

Python( "expression" )

If the current Python can successfully evaluate the variable name, expression, or function, the Python() function will return that value. If it cannot evaluate it, an error is returned. The Python() function returns a numeric value.

Example: Python( "10+1") returns 11

Example: Python("int('0xff', 16)") returns 255

PythonFunc( "module"; "function"; argument1 ; ... )

Execute a function from an imported module.

This will only work if the module has already been imported.

Example: PythonFunc( "random"; "normalvariate"; mu; sd)

returns a random number from Normal(mu,sd) where mu and sd are numbers

PythonGetList( "list"; "treeage table" )

The PythonGetList() function copies a two-dimensional Python nested list into a TreeAge table. The Python expression can be a literal list (as seen below), a variable containing a list, or a function returning a list.

Example: PythonGetList("[[0,.1,.2],[1,.15,.25]]" ; "myTable") loads myTable and returns the number of rows (2)

PythonRun( "command"; ... )

Returns 0 for failure or 1 for success. Specify a valid Python statement. To run a series of scripts or modules,

separate multiple double-quoted paths using semicolons. If Python successfully runs the last specified

statement, the PythonRun() function will return a value of 1. If Python encounters an error evaluating your

commands, the PythonRun() function returns a 0 value -- not an error. To force a TreeAge Pro error, use the

result as the denominator of a division operation. To return the value of variables or functions created or modified

by the executed commands, use the Python() function.

Example: PythonRun( "from random import *" ; "seed(1)"; "x=random()" ) = 1

value = Python("x") = .134364...

PythonRunFile( "scriptfile" ; ... )

Returns 0 for failure or 1 for success. Specify the full path and filename of a valid Python script text file or

module to run; or, specify just the file name if it is stored in the Python default path. To run a series of scripts

or modules, separate multiple double-quoted paths using semicolons. (Note that forward slashes can be used

instead of backslashes in the path specification.)

Example: PythonFile( "C:\Python24\lib\TransplantQueue.py" )

Using PythonRunFile() to execute a file is equivalent to having Python evaluate the execfile() function:

PythonRun( "execfile( 'C:\Python24\lib\TransplantQueue.py')" )

If the script/module executes successfully, use the Python() function to return the value of variables:

queueWait = Python("now()")

User( "myPythonFunction"; argument1 ; ... )

User command to call Python function and pass arguments to the function.

Th User( ) function is still supported but the other commands listed above are preferable.