Transformations > Python transformation > Developing the Python code

Developing the Python code

To define the Python transformation functionality, you enter Python code snippets on the Python tab. Enter code snippets to reconstruct input variables, load a pre-trained model, define output variables, and define additional transformation functionality.
Enter Python code snippets in the following sections of the Python editor:
Pre-Partition Python Code
Defines code that can be interpreted one time and shared among all rows of data.
Use the Pre-Partition Python Code section to perform the following tasks:
Main Python Code
Defines how the Python transformation behaves when it receives an input row while processing a partition. The Python transformation processes the code on the Main Python Code section for each partition and each row.
Post-Partition Python Code
Defines how the Python transformation behaves after it processes all input data in a partition. You can call the generateRow() method to generate output rows.
Use the following guidelines when you write Python code:

Creating Python code snippets

To create Python code snippets that define transformation functionality, use the Python editor on the Python tab.
The following image shows the Python tab with the Python editor expanded:
This screenshot shows areas of the Python editor as described in the legend.
  1. 1. Inputs and Outputs tabs. Use these tabs to add incoming fields and output fields as variables in the Python code snippets. The fields and methods displayed on these tabs vary based on which section of the code entry area is selected.
  2. 2. Go to list. Use to switch among the sections in the code entry area.
  3. 3. Minimize, Open Both, and Maximize icons. Use the Minimize and Maximize buttons to minimize and maximize the transformation properties. Use the Open Both icon to open the Mapping Designer canvas and the transformation properties at the same time.
  4. 4. Code entry area. Enter Python code snippets in the Pre-Partition Python Code, Main Python Code, and Post-Partition Python Code sections.
Tip: To expand the transformation properties so that you can see the code entry area more fully, click Maximize.
To create Python code snippets, perform the following tasks:
    1. Select the section in which you want to enter a code snippet in the Go to list.
    2. To access an incoming field or output field in the snippet, select the field on the Inputs or Outputs tab, and click Add.
    You can also create output fields on the Outputs tab by clicking Create New Field.
    3. Write appropriate Python code based on the section.

Referencing a resource file

A resource file contains the resources that you use in the Python code.
In the Python transformation, enter the relative path of the resource file. For example, if the resource file is stored in <Secure Agent installation directory>/ext/python/folder1/myfile, then the relative path would be /folder1/myfile.
When you access a resource file in the Python code, you reference the array resourceFilesArray[index]. To access a specific resource file, you specify an index to locate the resource file according to the order that the resource file appears in the list of resource file paths.
For example when you specify several resource files, you reference the first resource file in the Python code using resourceFilesArray[0]. You reference the second resource using resourceFilesArray[1].