Toolkits > com.ibm.streamsx.topology 2.1.0 > com.ibm.streamsx.topology.python > Creating SPL Operators from Python code > @spl.source
Decorator to create a stateful or stateless SPL source operator from a Python iterable class or function. A source operator has a single output port. The decorated Python class or function is used to create an iterator. Once the SPL operator has been notified of all ports ready then it iterates over the iterator. For each value returned by the iterator zero or more SPL tuples are submitted to the output port. It a value is None then no tuple is submitted. Otherwise the value is converted into zero or more SPL tuples which are submitted to the output port.
When a Python tuple is submitted as an SPL tuple then if there are less values in the Python tuple than attributes in the SPL tuple's schema then the remaining SPL attributes are set to their default value.
When the iterator completes, a window mark followed by a final mark are submitted to the output port and no more tuples are submitted to the output port.
When a Python iterable class is decorated with @spl.source a stateful SPL source operator is created.
When the SPL operator is initialized __init__ is called to create an instance of the class which is an iterable. Then iter(instance) is called to create the iterator from the iterable.
If the class has instance fields then they are the state of the operator and are private to each invocation of the operator and maintained across calls to its __call__ function.
Example decorated class creating a SPL source operator with parameters that produces a finite sequence as its output stream.
@spl.source class Range: def __init__(self, stop, start=0): self.start = start self.stop = stop def __iter__(self): return zip(range(self.start, self.stop))
When a Python function is decorated with @spl.source a stateless SPL source operator is created.
When the SPL operator is initialized the decorated function is called and iter(value) is called on the return value to create the iterator.
Example decorated class creating a SPL source operator with parameters that produces a finite sequence of 0-199 as its output stream.
@spl.source def Range() return zip(range(200))
When a Python value from the iteration does not provide values for all the attributes of the output port, then any unset values are set to the default value for the type, zero, empty string or empty collection.