Modeling Guide

List of BaseOperator Methods

When subclassing BaseOperator, you will need to use a set of methods to set callbacks, send messages, etc. The list below summarizes the BaseOperator methods you might need.

This method should be overridden by a subclass. It should return the OperatorInfo of this operator. OperatorInfo defines some key features of the operator such as port names and other things.
This method can be overridden by a subclass. This method will be called after the user definedconfigurations have already been set. This method will be called for all graph's operators before any operator has been started.
This method can be overridden by a subclass. It should contain code to be executed before the operator's callbacks start being called. The code here should be non-blocking. If it is blocking, the callbacks you have registered will never be called. You are allowed to use the method self._send_message inself._prestart. However, bare in mind that other operators will only receive this data sent when their _prestart have already finished. This is because their callback will only be active after their _prestart have finished
This method can be overridden by a subclass. It should contain code to be executed after the operator's main loop is finished.
_send_message(self, port, message)
Puts an item into an output queue.
  • port (str): name of output port

  • message (...): item to be put

_set_port_callback(self, ports, callback)
This method associate input 'ports' to the 'callback'. The 'callback' will only be called when there are messages available in all ports in 'ports'. If this method is called multiple times for the same group of ports then the old 'callback' will be replaced by a new one. Different ports group cannot overlap.
  • ports (str|list[str]): input ports to be associated with the callback. 'ports' can be a list of strings with the name of each port to be associated or a string if you want to associate the callback just to a single port.
  • callback (func[...]): a callback function with the same number of arguments as elements in 'ports'. Also the arguments will passed to 'callback' in the same order of their corresponding ports in the 'ports' list.
_remove_port_callback(self, callback)
Remove the 'callback' function. If it doesn't exist, the method will exit quietly.
  • callback: Callback function to be removed.

_add_periodic_callback(self, callback, milliseconds=0)
Multiple distinct periodic callbacks can be added. If an already added callback is added again, the period 'milliseconds' will be replaced by the new one. If you want two callbacks with identical behavior to be run simultaneously then you will need to create two different functions with identical body.
  • callback (func): Callback function to be called every 'milliseconds'.

  • milliseconds (int|float): Period in milliseconds between calls of 'callback'. When not specified it is assumed that the period is zero.

_change_period(self, callback, milliseconds)
  • callback (func): Callback for which the period will be changed. If callback is not present on the registry, nothing will happen.

  • milliseconds (int\float): New period in milliseconds.

_remove_periodic_callback(self, callback)
  • callback (func): Callback function to be removed.