Skip to content

Examples of Processes With Python Node

Process With Python Node

$ cat python.strm

// Name process as python
name python

// read from the python.csv file
in python.csv -> input

// create a computational python node
node pyusernode(input)(output) lang=python

// write out the results
out output -> pyoutput.csv

$ vor create process python

$ vor run python

The fields that match in both name and type from the input queue are directly copied to the output queues before the worker function is called, and the non-matching fields are initialized to missing. In order to access or modify the queue fields in the user class, you can type self.<Queuename> <Fieldname> in pyusernodeU.py in python/src/python/queues/ directory as follows:

self.Output.Value2 = input.Value*2

instead of

self.Output.Value2 = input.Value*fx

If the user wants to populate the output class using a dictionary, they can do so by creating and initializing a dictionary and using the dictionary to populate an instance of the output class.

outputOutput = dict()

outputOutput['Value2'] = input.Value*fx

self.Output.Post(Output.Output(**outputOutput))

Process With Python Node Using Matrix Map

Place the Matrix Map file you intend to use in your process in the input/matrices/ directory. The example below uses the exchangerates.csv Matrix Map, and the dictionary.csv and tables.csv file very similar to the previous example.

name,type,descr,genFormat,arraylen
id,char,,sequenceN
class1,char,,state
class2,char,,city
value,num,,uniform 0 1000 2
fx,num, Only added variable for this example
name,descr,type,inherit
input,Input table1,,,
id
class1
class2,
value

output,Output table1,,input
$ cat python_mm.strm

// Name process as python_mm
name python_mm

// read from the python.csv file
in python.csv -> input

// create a computational python node
node py_mm_usernode(input)(output) lang=python

// write out the results
out output -> py_mm_output.csv
vor create process python_mm.strm

In the src/python/ directory modify py_mm_usernodeU.py file to consume and use the matrix map. First, add the following code to the _init_ block to read in the map.

        self.hh = handle
        self.fx = handle.GetMatrixMap("exchangerates")

Next, add the following code to the _worker_ block, which will fetch the USD/EUR exchange rate value from the provided map and populate the fx column in the output table.

        if "USD" in self.fx:
            if "EUR" in self.fx["USD"]:
                fx = self.fx["USD"]["EUR"]
        self.Output.Fx = fx
vor run python_mm