How to Run a User-Written Python Function


PYTHON([app/]synonym, input1 [, input2 ...], output)

where:

[app/]synonym

Is the application and synonym name for the Python script.

input1 [, input2 ...]

Are the input arguments.

output

Is the output argument. This argument must match the name of a field in the OUTPUT_DATA segment in the Master File.

Example: Running a Python Script

The following is the arithmetic_example_multiple_computes.py Python script, which calculates four output fields, ADDITION, SUBTRACTION, MULTIPLICATION, and DIVISION in the function named arithmetic. All of the files for this example reside in an application directory named python.

# arithmetic_example_multiple_computes.py
 
import csv
import time
 
def arithmetic(csvin, csvout):
 
    with open(csvin,  'r', newline='') as file_in,\
         open(csvout, 'w', newline='') as file_out:
 
        fieldnames = ['addition', 'subtraction',
                      'multiplication', 'division']
 
        reader = csv.DictReader(file_in, quoting=csv.QUOTE_NONNUMERIC)
 
        writer = csv.DictWriter(file_out, quoting=csv.QUOTE_NONNUMERIC,
                                fieldnames=fieldnames)
        writer.writeheader()
 
        for row in reader:
            addition       = row['a_number'] + row['another_number']
            subtraction    = row['a_number'] - row['another_number']
            multiplication = row['a_number'] * row['another_number']
            division       = row['a_number'] / row['another_number']
 
            writer.writerow({'addition':       addition,
                             'subtraction':    subtraction,
                             'multiplication': multiplication,
                             'division':       division})

The .csv file with the sample data, arithmetic_sample_input.csv, has a header record and two data records to be used to determine the data types and lengths for the input arguments.

"a_number","another_number"
1,1
100000,100000

The synonym creation frame for this script is shown in the following image.

The generated Master File (arithmetic_example_syn.mas) follows:

FILENAME=ARITHMETIC_EXAMPLE_SYN, SUFFIX=PYTHON  , $
  SEGMENT=INPUT_DATA, SEGTYPE=S0, $
    FIELDNAME=A_NUMBER, ALIAS=a_number, USAGE=I11, ACTUAL=STRING,
      MISSING=ON,
      TITLE='a_number', $
    FIELDNAME=ANOTHER_NUMBER, ALIAS=another_number, USAGE=I11, ACTUAL=STRING,
      MISSING=ON,
      TITLE='another_number', $
  SEGMENT=OUTPUT_DATA, SEGTYPE=U, PARENT=INPUT_DATA, $
    FIELDNAME=ADDITION, ALIAS=addition, USAGE=D10.1, ACTUAL=STRING,
      MISSING=ON,
      TITLE='addition', $
    FIELDNAME=SUBTRACTION, ALIAS=subtraction, USAGE=D5.1, ACTUAL=STRING,
      MISSING=ON,
      TITLE='subtraction', $
    FIELDNAME=MULTIPLICATION, ALIAS=multiplication, USAGE=D15.1, ACTUAL=STRING,
      MISSING=ON,
      TITLE='multiplication', $
    FIELDNAME=DIVISION, ALIAS=division, USAGE=D5.1, ACTUAL=STRING,
      MISSING=ON,
      TITLE='division', $

The generated Access File (arithmetic_example_syn.acx) follows:

SEGNAME=INPUT_DATA, 
  MODNAME=python/arithmetic_example_multiple_computes.py, 
  FUNCTION=arithmetic, 
  PYTHON_INPUT_SAMPL=python/arithmetic_sample_input.csv, 
  INPUT_HEADER=YES, 
  OUTPUT_HEADER=YES, $

The following WebFOCUS procedure, sales_multiple_computes.fex calls the arithmetic function four times in order to get a value returned for each of the four outputs:

-* sales_multiple_computes.fex
 
TABLE FILE GGSALES
SUM 
  DOLLARS
  UNITS

COMPUTE Addition/D7        = PYTHON(python/arithmetic_example_syn,
                             DOLLARS, UNITS, ADDITION);
COMPUTE Subtraction/D7     = PYTHON(python/arithmetic_example_syn,
                             DOLLARS, UNITS, SUBTRACTION);
COMPUTE Multiplication/D16 = PYTHON(python/arithmetic_example_syn,
                             DOLLARS, UNITS, MULTIPLICATION);
COMPUTE Division/D7.2      = PYTHON(python/arithmetic_example_syn,
                             DOLLARS, UNITS, DIVISION);

WHERE RECORDLIMIT EQ  100 
HEADING
  "Arithmetic Example, Multiple Computes"
  ""
 
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE 
END

The output is shown in the following image.