Python nodes can be made to be generic and reusable by defining node properties, and accessing them from within the node code.
There are two mechanisms for accessing properties within a Python node:
Using the run time property names
initialize
method, see Initialize.For example, in the Python2Implementation property you define a member PROPERTY_BASE, as follows:
PROPERTY_BASE = "ls.brain.node.sumExample"
This means that all properties will have the following run time property name format, where <runtime-property-name-extension>
is the suffix of the run time property name:
PROPERTY_BASE.<runtime-property-name-extension>
For example, for the OutputFieldName property, the PROPERTY_BASE part of the run time property name is ls.brain.node.sumExample
and the suffix is outputFieldName
.
Therefore, within the initialize
method of the node these properties are loaded into the variables on the BrainNode
object as follows:
# Obtain the values set on the node propertiesself.outputAsDouble = self.properties.getBool(PROPERTY_BASE + ".outputAsDouble")self.inputFieldName = self.properties.getString(PROPERTY_BASE + ".inputFieldName")
Consider the first property from the above example. This code obtains the properties member from self.properties
defined on the BrainNode
interface. The code then attempts to access the boolean property called:
PROPERTY_BASE + ".outputAsDouble"
From the PROPERTY_BASE variable, this corresponds to the run time property name:
ls.brain.node.sumExample.outputAsDouble
This run time property name is used by the OutputAsDouble property.
With all of this put together, the following line states "obtain the Boolean property OutputAsDouble and store it on the outputAsDouble
variable on this object":
self.outputAsDouble = self.properties.getBool( PROPERTY_BASE + ".outputAsDouble");
Multi-line properties
When the property is a multi-line property (i.e. any property which does not have a property type of "choice", "string", "filename", "password" or "boolean"), then a little bit more work needs to be done to extract the property value.
In such cases, the properties.getString("<Runtime Property Name>")
mechanism will actually return a filename which corresponds to the file where the contents of the property value are stored. Therefore, after obtaining the filename, the property contents need to be extracted. The following example shows how to parse the contents of a multi-line property with the run time property name ls.brain.node.textProperty
into an array of strings:
import os
…
def setup:
self.textParamFilename = self.properties.getString("ls.brain.node.textProperty", '') self.textParamLines= file(self.textParamFilename, "r").read()
Assuming that you just want the contents of the property in an array, where each element in the array corresponds to a line in the property, then this mechanism can be used for all of the multi-line property types.
Textual substitution of properties
Textual substitution can be used to access properties within the Python code, using the {{^propertyname^}} syntax, see Explicit property reference.
The following example shows the use of textual substitution to obtain the output field name:
self.outputFieldName = "{{^OutputFieldName^}}"