How to Create a Synonym for a Python Function


Each Python script used with the Adapter for Python must have a synonym (metadata object) that describes the input fields and output fields of the script. If a Python script contains multiple user written functions, and you want to be able to use more than one function within the script as a starting point, you must create a separate synonym for each function within the script.

The synonym will be created using a sample file that contains only the fields that are input parameters for the script. A few rows of sample data are sufficient for the Adapter for Python to determine the appropriate data types and lengths of the parameters. The sample file must be a .csv file. The data in the file does not have to contain actual data, but it should represent the highest values for numeric fields and the longest lengths for alphanumeric values that will appear the actual data. The Master File will contain the list of input fields and output fields. The Access File will contain information about the script file and sample input file.

  1. Right-click PYTHON on the list of configured adapters, and click Create metadata objects on the context menu.

    The Create Synonym for Python frame opens, as shown in the following image.

    Note that a metadata object is a synonym. The synonym for a Python function will consist of a Master File (which describes the input fields and output fields needed for running the function) and an Access File (which contains information about the sample data file and the script file).

  2. Enter or select values for the following parameters.
    PYTHON Script

    Is the Python script. Enter an application directory name and script name, or click the ellipsis (...) to navigate to an application directory and select a script, then click OK. The Python script will have the extension .py.

    Function Name

    Select the name of the (starting) function in the script file for which to create a synonym.

    For example, the Python script named arithmetic_example.py contains the definition for the function adder:

    def adder(csvin,csvout): 
    Select file with sample input data for the PYTHON Script

    Open the file picker (...) to select the application directory and file that contains the sample data for creating the synonym. Click OK.

    This file is used to determine the field names, data types, and lengths for the data sent to the Python script in csvin. If the sample file has no header record, the field names will be FIELD_1 through FIELD_n.

    CSV files with header

    If the input .csv file does not have a header row, uncheck Input. If the output .csv file should not have a header row, uncheck Output. The header requirements are contained in the function code.

    For example, in the following sample code the input file contains no header record (fieldnames) and the fieldnames object is defined at runtime using the fieldnames argument for the reader. The output file will contain a header record, as defined in the fieldnames argument for the writer, and is written to the file using the statement writer.writeheader():

    with open(csvin,  'r', newline='') as file_in, \
             open(csvout, 'w', newline='') as file_out:
    reader = csv.DictReader(file_in, fieldnames=['input_field'],
                            quoting=csv.QUOTE_NONNUMERIC)
    writer = csv.DictWriter(file_out, fieldnames=['output_field'],
                            quoting=csv.QUOTE_NONNUMERIC)
    writer.writeheader()
    Application

    Enter the name of the application directory in which to create the synonym, or click the ellipsis (...) to navigate to an application directory, then click OK.

    Synonym Name

    Enter a name for the resulting synonym, or accept the default name.

  3. Click the Create Synonym button on the ribbon.

    The synonym is created in the specified application directory.