Creating SQs from Vanilla Python Functions

To create an SQ in TTPython, we “SQify” a function by putting @SQify in the line above the function definition. When the SQ has the tokens it needs to run, it will provide the values in those tokens as arguments to the supplied function. There are a few restrictions we impose on the functions that are SQified.

However, the first argument will always be reference to the SQ object that encapsulates all the elements of the TTSQ. This allows for two essential mechanisms: state and output.

The TTSQ object passed in will have an instance variable called ‘state’ that can be used in any way the user likes to let information persist across invocations of the SQ. They may use this to check if state variables have been initialized, if they have been invoked on out-of-order tokens, or otherwise. The state is entirely managed by the programmer.

Rather than using ‘return’, sending outputs from the SQ is explicit and flexible. The SQ object provided to the function will have a callable “send_token” function that will take a port number and a value to tokenize (i.e., return) as inputs, at which point the graph interpreter will handle all the rest of tokenizing and sending to recipient SQs. This way, the programmer can send tokens whenever they want (and potentially multiple) without waiting for everything to complete before otherwise returning a handful of values.

The last (known) restriction is that any function to be SQified that requires the use of an imported library must import that library within the function itself, not at the top of the python file.

@ticktalkpython.SQ.SQify(function)

Decorator for transforming vanilla Python functions into SQ templates that take in and return TTTokens

Parameters:

function (function) – the function to be @SQify-ed

@ticktalkpython.SQ.STREAMify(function)

Decorator for turning a vanilla python function into one that will produce a stream of values. The main difference between this decorator and SQify is that it forces the firing rule to be TTFiringRuleType.TimedRetrigger, which will cause the SQ to run periodically according to meta-parameters provided when calling within the @GRAPHify decorated function. The same function will be rerun for each iteration of the stream. A good use case is sampling a sensor.

To specify the clock domain, periodicity, phase, and data validity interval, use keyword arguments TTClock, TTPeriod, TTPhase, and TTDataValidityInterval with constant valued (or clock variable-named) input to those keyword arguments when calling this in the @GRAPHify -ed function.

Parameters:

function (function) – The function to be STREAMify -ed