Skip to content
Josh Blum edited this page Sep 8, 2017 · 14 revisions

Pothos Python toolkit

The Pothos Python toolkit is a component of the Pothos data-flow framework. This toolkit brings Python language support to the Pothos ecosystem, with support for:

  • Calling into Python from the Pothos Proxy API
  • Calling into the Pothos Proxy API from Python
  • Pythonic wrappers for making Blocks in Python

Requirements

  • Python development environment (version 2.7 and up)
  • Numpy to support array math in custom Blocks

Python block API

The Python API for creating Blocks is almost identical to the C++ API.

  • For more details about a particular API feature, visit the Blocks coding guide.
  • For Python examples of the Block API, see this project's Test blocks module.
    • The test blocks demonstrate stream buffers, signals, slots, labels, and messages.

A simple example

The following example shows how to make a simple project (3 files) that builds and installs a block written in Python. The block will be accessible through the Pothos block registry and available in the graphical design tool.

A simple adder block (MyAdder.py):

import Pothos

"""/*
|PothosDoc My Adder

Add two input streams, produce an output stream.

|category /Math

|param dtype[Data Type] The input and output data type.
|default "float32"
|widget DTypeChooser(float=1,cfloat=1,int=1,cint=1,dim=1)
|preview disable

|factory /my_package/adder(dtype)
*/"""
class Adder(Pothos.Block):
    def __init__(self, dtype):
        Pothos.Block.__init__(self)
        self.setupInput(0, dtype)
        self.setupInput(1, dtype)
        self.setupOutput(0, dtype)

    def work(self):
        #how many elements to process?
        n = self.workInfo().minElements

        #grab the input and output ports
        inPort0 = self.input(0)
        inPort1 = self.input(1)
        outPort = self.output(0)

        #grab the input and output buffers
        in0 = inPort0.buffer()
        in1 = inPort1.buffer()
        out = outPort.buffer()

        #perform arithmetic
        out[:n] = in0[:n] + in1[:n]

        #produce and consume elements
        inPort0.consume(n)
        inPort1.consume(n)
        outPort.produce(n)

Make this directory into a module (__init__.py):

from . MyAdder import Adder

The build script for the block (CMakeLists.txt):

########################################################################
# Project setup
########################################################################
cmake_minimum_required(VERSION 2.8.9)
project(MyBlocks)
enable_language(CXX)

find_package(Pothos CONFIG REQUIRED)
include(PothosPythonUtil)

########################################################################
## Build and install
########################################################################
POTHOS_PYTHON_UTIL(
    TARGET MyAdderBlock
    SOURCES
        __init__.py
        MyAdder.py
    FACTORIES
        "/my_package/adder:Adder"
    DESTINATION MyPackage
    ENABLE_DOCS
)