The following Python script, named arithmetic_example.py contains a function named adder that adds two numbers and conforms to the requirements described in this section.
# arithmetic_example.py import csv def adder(csvin, csvout): with open(csvin, 'r', newline='') as file_in,\ open(csvout, 'w', newline='') as file_out: fieldnames = ['addition'] 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'] writer.writerow({'addition': addition})
A Python script must conform to the following requirements in order to be compatible with WebFOCUS.
The global variables csvin and csvout are defined by WebFOCUS for reading and writing .csv files.
newline=''
If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n line endings on write, an extra \r will be added.
quoting=csv.QUOTE_NONNUMERIC
This indicates that non-numeric (alphanumeric, text, string, and date) values are enclosed in double quotation marks and that numeric values are not. When csvin is read, all WebFOCUS numeric values will be automatically converted to Python floating-point numbers. If the WebFOCUS COMPUTE defines the returned field as integer, the decimal point and any decimal places will be truncated. In the csvin file, if a non-numeric field contains the double quotation character, it will be doubled by WebFOCUS. Python will correctly parse this because the Dialect.doublequote format parameter of the Python csv module defaults to True.
For example, in the following Master File, the output argument is called ADDITION:
SEGMENT=OUTPUT_DATA, SEGTYPE=U, PARENT=INPUT_DATA, $
FIELDNAME=ADDITION, ALIAS=addition, USAGE=D7.1,
ACTUAL=STRING, MISSING=ON, TITLE='Addition', $
Therefore, the last (output) argument name in the call to PYTHON must be ADDITION:
COMPUTE Anyname/I5 = PYTHON(synonym_name, anyarg1, anyarg2, ADDITION);
The output written to csvout must be a sequence, for example, a list, even for a single field.
For a list containing a single field, the correct syntax is:
writer.writerow([result])
The following syntax is incorrect and will return incorrect values for strings and raise an exception for numeric fields:
writer.writerow(result)
Note: Because the Python script will be imported, the following Python programming idiom will be ignored.
if __name__ == '__main__':
However, including it may be useful for testing outside of WebFOCUS.
fieldnames = ['addition'] reader = csv.DictReader(file_in, quoting=csv.QUOTE_NONNUMERIC) writer = csv.DictWriter(file_out, quoting=csv.QUOTE_NONNUMERIC, fieldnames=fieldnames) writer.writeheader()
The recommendation is to use header records in the input and output .csv files. If you use sample data without a header, the field names in the generated metadata will be of the form FIELD_1 through FIELD_n.
The Adapter for Python also comes with a set or predefined statistical Python functions that you can easily invoke in WebFOCUS.
WebFOCUS | |
Feedback |