54.1 A Simple Python Function Example
While Python would typically be used to extend TreeAge Pro, we will start with a simple example that does not actually require Python. Specifically, we will create a Python Discount function that we can then validate against the built-in Discount function.
The Special Features/Python tutorial example model, Python Example – Discount.trex, contains two strategies. You can open this now.
Important note: When you open a model with a user-defined Python function, you will be prompted to enable Python scripts. This allows you to review Python scripts in any model you receive from an unknown source (search the model for “def ” and "python”). The types of Python commands useful in trees are not a security concern, but the core language does have file and Internet access methods which could theoretically be misused. To turn off the "Do you want to enable python scripts?" dialog, go to Window > Application Preferences then navigate to Analysis Tools > Python and check the box "Do not show enable Python question". |
The top strategy’s payoff references the Python function PythonDiscount, while the bottom strategy’s payoff references the built-in TreeAge function Discount(). Rolling back the model shows the values are equivalent.
A Python function is created within a variable definition. In this example, a variable called PythonDiscount has been defined at the root node in the Variables View. The figure below shows how the Add/Change Variable dialogue can be used to edit Python functions.
In the Add/Change variable dialogue, you will see the Python Editor option selected above the Build Expression. TreeAge Pro identifies the definition as a Python function because it starts with the keyword def. If you are creating your own function, select this editor option yourself. The Python Editor mode helps you write Python code by allowing required indentation and recognizing Python keywords.
A Python function must begin with the function declaration def followed by the name of the function, then parentheses, and finally a colon. If the function requires arguments, they are placed within the parentheses separated by commas.
def PythonDiscount(val, years, rate):
Note the Python function name must match the variable name in the tree, and Python is case sensitive. The PythonDiscount function requires three required arguments that must be provided when the function is called.
Python code following the function declaration must be indented using tabs or spaces, indicating that this code is included within the overall Python function. Python uses indentation to identify blocks of code. Additional indentation would be used to place a block of code within a logical condition as well.
Each line of code should end with a semicolon, although this is optional. To comment your code, enter “#” at the start of a comment line. The return statement is used to return a value from the function. In this case, the discounted value is returned. The end of the function is implicit.
In the tree view, user-defined function definitions are prefixed with “(F)”. Only the declaration line will be shown unless wrapping is turned on under Variables Display preferences.
For more information on the Python language and syntax, go to http://www.python.org/.