Working with DATs in Python
The main documentation for the methods and classes you use to work with DATs are the DAT Class and Cell Class articles. This article gives examples of using those methods and classes.
The main class type describing any Operator is the base OP Class. You will need a reference to one of these to do anything. There are two global operator objects are always available (except for in the Textport):
merefers to the operator that is currently being evaluated or executed. For example, when executing a script,
merefers to the containing DAT. When evaluating an expression,
merefers to the containing operator.
rootrefers to the top level component
To get references to other OPs (for example, a node named
'pattern1' sitting next to the node
'constant1') the most common functions to use are:
ops(), for example
op() returns a single OP object, while
ops returns a (possibly empty) list of OPs.
They are described in td Module.
These functions search for operators from the current component, so both relative and absolute paths are supported.
The current component is defined as: The OP that
me is inside.
Note that the OP Class itself, also contains an
ops() method. In this case, nodes are searched from the OP.
me.op('..') will always return its own parent, while
op('..') will return the parent of the current component.
If you are typing a Python expression in a parameter of a node
'constant1', and you wish to get a reference to
'pattern1', you would type
If you are in a script you can assign this reference to a variable for easier repeated access.
n = op('pattern1')
In this case
op() will search relative to the DAT that is executing the script.
An OP also has a
parent() method that can be use to get the parent COMP of it.
If you are putting a Python statement in a parameter of a COMP and want to refer to a child of that COMP, you can use the
op() method for the OP, which is available as
me in the parameters.
TIP: To find out quickly what members and methods you have access to for any node, select that node and on its parameter dialog, click the Python Help icon. You will go the wiki for the python classes for that node. There you can find out what info you can get about a node, and what methods are available for it. The documentation can also be arrived at by right clicking on the node and selecting "Python Help..." from the menu.
Refer to Working with OPs in Python for more details about this section.
Using DATs in scripts
To use a DAT in a script you would first get a reference to the DAT(s) you are interested in using
op(). Ideally this would be assigned to a variable which you can use multiple times in the script without having to re-search for the OP every time you need it. Then you can use the
s on the DAT reference to refer to a specific cell. You can use either labels or indices to refer to a cell. For example this script copies cells from one table to another.
# get a reference to a DAT named 'table1' n1 = op('table1') # get a reference to a DAT named 'table2' n2 = op('table2')
# now get references to the two cells we are interested in c1 = n1['month', 'sales'] #Get cell in row labelled 'month' and column labelled 'sales' c2 = n2[3,6] # Get row 3 and column 6. (Note indices start at 0, not 1).
# Concatenate them as strings and update the cell with the new value total = c1 + c2 n2[3,6] = total
Common Python Tasks
|Get a cell value by index|
|Get a cell value by label|
|Get a cell value by row index, col label|
|Cast cell to integer and float|
|Get the number of table rows|
|Get the number of table columns|
|Set a cell value by indeces or labels|
|Set a cell value by label|
|Copy a table to another table|
|Append a row to a table|
|Append a column to a table|
|Access current cell in an Evaluate DAT|
|Access neighboring cells in an Evaluate DAT|