The Pipeline Creator Python API is based on code produced by SWIG, which is a tool that can expose a C/C++ API
in a variety of target languages, including Python, Perl, and Java. SWIG generates a set of
language-specific constructs that allow bidirectional data exchange between the underlying
codebase and the target language.
Python (on Server)
Add the Python (on Server) component to your pipeline. The Python
source code is contained in the Script parameter.
- Add a Python (on Server) component to your pipeline.
- Select the component in the work area.
- From the Parameter panel click
Script in the Parameter panel to open the
script editor.
The component's default code is a simple script, which is the minimum required:
import swigpython3 as pilotbase #for bare-metal stuff
import pilotpython #nicer python classes
def onInitialize(ctxt):
context= pilotpython.Context(ctxt)
return pilotpython.READY_FOR_INPUT_OR_NEW_DATA
def onProcess(ctxt, dr):
context= pilotpython.Context(ctxt)
data= pilotpython.DataRecord(dr)
return pilotpython.READY_FOR_INPUT_OR_NEW_DATA
def onFinalize(ctxt):
context= pilotpython.Context(ctxt)
None
The import statements load the SWIG layer and the pure Python layer. The
main features of the script are as follows:
-
onInitialize(ctxt): A function that is called once when the pipeline
starts and the component is being initialized. In most cases, this function is used to
read parameter values and to specify up the component.
-
onProcess(ctxt, dr): A function that is called for each data record
received by the component. The ctxt and dr parameters
are the Context and DataRecord objects provided by the
SWIG layer.
-
onFinalize(ctxt): A function that is called once, when the last data
record has been processed. Any resources that the component is still using are released
here.
-
The constants that are returned by onInitialize() and
onProcess() inform Pipeline Creator of the component’s state and are explained later in this guide.
-
The assignment operations in the three functions generate objects from the pilotpython
classes, which you can use in your code:
context= pilotpython.Context(ctxt): Creates a
Context object, providing access to Pipeline Creator items such as component parameters, pipeline-level properties, and the status bar
of Pipeline Creator Client.
data= pilotpython.DataRecord(dr): Creates a
DataRecord object, providing access to each incoming data
record.
The default script only reads data, without processing or outputting it. Add your own code
to manipulate this data, as described in the following sections.
Component Parameters
Every component has a set of parameters that you can modify before running a pipeline. The
parameters' values are read at runtime, and the component’s behavior adjusts
accordingly.
The pilotpython API lets you read component parameters using an instance of the
Context class. In the default code, this instance is called
context:
params= context.getComponentParameters(): Gets a reference to the
component’s parameters.
weightParam= params.get("Filter Weight"): Gets a parameter.
_weight= weightParam.getValue().getFloat(): Gets an attached Value
object, and then the value itself (in this case, a float).
Global Variables
Like component parameters, global variables are accessible by means of an instance of the
Context class:
globalProps= context.getGlobalProperties()
floatData= globalProps.get("GlobalFloat").getValue().getFloat()
Unlike component parameters, global properties are writable, and like data record
properties, they are represented by a PropertyCollection object.
Getting Data Record Properties
To be useful in a data stream, your component needs access to the properties of the
incoming data records. Data records are hierarchical data structures, but often you are only
interested in properties at the top level. This section demonstrates how to read, modify,
and create top-level properties.
To get a data record’s property collection, call the getProperties()
method of the DataRecord object for the record. This returns a
PropertyCollection object containing the properties.
Each property is defined by a Property object, which, in turn, contains a
name, and a Value object. The Value object holds the
actual value represented by the property. See Python Values.
Reading Properties
A PropertyCollection object has two methods to retrieve a property:
get(name): Raises an exception if the
property does not exist.
find(name): Returns None if the property
does not exist.
The name parameters are strings. Both these methods return a
Property object if the property exists.
A PropertyCollection also has a method to test for a property’s existence,
isDefined(name). This returns True if the
property exists, False if it does not.
Once you have obtained a Property object, call its
getValue() method to obtain access to the value held by the property.
This is returned as a Value object.
At this point, it is helpful to know the type of value that is to be accessed. If you do
not know the value type, you can get it by calling the getClassTag() method
of the Value object. This returns a const string that is unique for every
value type. It has the form.
SciTegic.value.<Something>Value
where <Something> is the type. For example, a string has the class tag
SciTegic.value.StringValue. To get to the represented value, use one of
the getXXX() methods. For example, to retrieve
the value of a myValue object instance as a string, call
myValue.getString().
Creating a Property
The easiest way to create a property is to use the PropertyCollection’s
defineXXX() methods, as in this example:
props.defineStringProperty("smiles", line);
props.defineStringArrayProperty("Fragments", frags)
- The
defineByteArrayProperty(name,
value) method expects a value of type
bytearray.
defineDateTimeProperty(name,
value) processes datetime.date,
datetime.time, and datetime.datetime types.
defineHashTableProperty(name,
value) expects a value of type dict.
- The other
defineXXXArrayProperty(name,
value) methods expect homogeneous list objects of
their respective types.
Modifying a Property
A property contains a Value object, not the value itself. Modifying the
property is modifying or replacing the Value object.
For example, if a property already has a value, and you want to change just the value
itself, you can use one line:
props.get("color").getValue().setString("green")
Metadata
Every node or property can have metadata associated with it. The Python (on
Server) component makes this metadata available by means of
PropertyCollection objects. To enable retrieval of a
PropertyCollection for their metadata, the Node and
Property classes define two methods:
-
findMetaData(): Returns a PropertyCollection
containing metadata if present, or None if not.
-
getMetaData(): Returns a PropertyCollection in all
cases. If the queried object does not have metadata associated with it, the metadata is
created.
Checking for the Presence of Metadata
You can use findMetaData() to check for metadata, because that method
returns None if no metadata is defined:
def onProcess(ctxt, dr):
context= pilotpython.Context(ctxt)
data= pilotpython.DataRecord(dr)
props= data.getProperties()
priceProp= props.find('Price')
metaData= priceProp.findMetaData()
if (metaData is not None):
currencyProp= metaData.find('Currency')
if (currencyProp is not None):
props.defineStringProperty("Current Currency", currencyProp.getValue().getString())
return pilotpython.READY_FOR_INPUT_DATA
Guaranteeing the Presence of Metadata
If an algorithm requires the metadata of a node or a property (for example, if metadata is
written to the algorithm), retrieve the metadata using getMetaData(). This
guarantees that a PropertyCollection is available, by creating it if it
does not exist.
def onProcess(ctxt, dr):
context= pilotpython.Context(ctxt)
data= pilotpython.DataRecord(dr)
metaData= data.getRoot().getMetaData()
metaData.defineStringProperty("?", "42")
Manipulating Hierarchical Data
Pipeline Creator supports more than just the flat data structure discussed above. With the
Node class, you can build data trees to represent hierarchical data. Each
node can have a PropertyCollection attached to it, and also has a set of
methods to retrieve parent and child information. Access to the data tree is provided by the
DataRecord.getRoot() and DataRecord.findRoot()
methods.
Adding Properties and Children to a DataRecord
The following code snippet shows how to add a node to the hierarchy, and how to specify a
property in the new node’s PropertyCollection:
def addAnimal(ctxt, farmNode, kind):
newAnimal= ctxt.makeNewNode()
newAnimal.setName('animal')
newAnimal.getProperties().defineStringProperty('kind', kind)
farmNode.appendChild(newAnimal)
Context.makeNewNode(): Creates a new Node object, and
returns it. If you do not attach the new node to a parent after using it, destroy it with
Node.deleteInstance(). Otherwise, its memory is not released.
Node.setName(name): Sets a node's name to
the string name. No return value.
Node.appendChild(node): Attaches the node
object node to a parent node. No return value.
Node.getProperties(): Returns a node's properties as a
PropertyCollection.
Retrieving Nested Nodes
You can use the following methods to retrieve nodes in a data hierarchy:
Node.getChildAt(index): Returns the child
node of a node at position index. index must be an
integer.
Node.findChildrenByName(name): Searches a
node for children called name, and returns a list of matching
Node instances. name must be a string.
findChildrenByName() only matches children of the parent node object;
it does not iterate over the entire sub-tree of the node.
Deleting Nodes
To delete a single node, call the removeChild() method of the parent node,
specifying the node to delete as the parameter. The following example function deletes child
nodes if they fulfill certain criteria:
def onProcess(ctxt, dr):
context= pilotpython.Context(ctxt)
data= pilotpython.DataRecord(dr)
animals= data.getRoot().findChildrenByName('animal')
for animal in animals:
if (isPig(animal)):
data.getRoot().removeChild(animal)
return pilotpython.READY_FOR_INPUT_DATA
You can also use the following methods to delete children of a node:
Node.clear(): Removes all children and properties from a node. No
return value.
Node.removeChildAt(index): Removes the child
of a node at position index. No return value.
Node.removeAllChildren(): Removes all children of a node, but leaves
the node’s PropertyCollection intact. No return value.
Directing Records to the Pass or Fail Port
In addition to controlling the type of incoming DataRecord object that a
component expects (see Component State Return Values), you can control which output port to
send it to with the method
DataRecord.routeTo(port), where
port is one of the following constants:
pilotpython.PASS_PORT: Sends data to the Pass port
pilotpython.FAIL_PORT: Sends data to the Fail port
pilotpython.NO_PORT: Removes the record from the data stream
Component State Return Values
A component has to inform its pipeline what kind of data record it expects to receive: a
new one or an existing one. This is done by means of the state returned by the
onInitialize(ctxt) and onProcess(ctxt, dr) functions.
Both of these functions return a constant representing the component state.
The following component states are supported:
pilotpython.DONE_PROCESSING_DATA: The component is done processing. It
requests that the pipeline does not invoke its onProcess() function
again.
pilotpython.READY_FOR_INPUT_DATA: The component expects to receive a
data record that already exists (that is that carries data with it).
pilotpython.READY_FOR_INPUT_OR_NEW_DATA: The pipeline can invoke the
component’s onProcess() function with either a new data record or an
existing one.
pilotpython.READY_FOR_INPUT_THEN_NEW_DATA: The pipeline is directed to
send existing data records to the component (if any exist), and then new ones as long as
this state is returned.
pilotpython.READY_FOR_NEW_DATA: The component expects to receive new,
empty DataRecord objects.
Depending on the category of component, there are some general guidelines on how to use the
state to get the required component behavior:
| Component Category |
State returned by Component |
| Calculator |
Always pilotpython.READY_FOR_INPUT_DATA. |
| Reader or Generator |
As long as data can be read from some source or other,
pilotpython.READY_FOR_NEW_DATA, then
pilotpython.DONE_PROCESSING_DATA. |
| Filter |
Always pilotpython.READY_FOR_INPUT_DATA. |
| Writer |
Usually pilotpython.READY_FOR_INPUT_DATA. If the component
defines an upper limit of items to write, the component can return
pilotpython.DONE_PROCESSING_DATA when this limit is
reached. |
| Integrator |
Usually pilotpython.READY_FOR_INPUT_THEN_NEW_DATA. Incoming
input data is usually cached until the pipeline sends a new data record and the
component returns the cached and processed data. |