Building ANKHOR Operators in Python

There are basic ways that an ANKHOR operator is created:

  • Built-in : The operator is part of the ANKHOR basic set of operators
  • Macro : The operator is composed of other operators and combined into an operator class
  • Plug-in : The operator is implemented in an external library called a plugin

The third option is used for operators written in Python. We have created an ANKHOR plugin and a supporting library that allows you to execute python scripts from within an ANKHOR flowsheet.

The operator has four inputs and one output. The script to execute is provided via the “command” input and the mode of execution selected by one of three enum values by the “mode” input:

  • single : A single python statement
  • eval : A python expression
  • file : A complete python script.

The arguments for the execution are provided to the “args” input in the form of a Tag-List:

This sample shows the simple invocation of a python expression “x+y” and the two input parameters “x” and “y” are provided using the Tag-List.

We will not implement a somewhat more complex operator, a prime number generator using a sieve implementation:

def eratosthenes2(n):
    multiples = set()
    primes = set()
    for i in range(2, n+1):
        if i not in multiples:
            primes.add(i)
            multiples.update(range(i*i, n+1, i))
    return primes
 
primes = sorted(list(eratosthenes2(num)))

The code is based on a Wikipedia sample. This is a complete script, so we have to set the mode to “file”. The input is provided with the variable “num”, and the output can be found in the variable “primes”.

The parameters to and from the python script are marshaled with “jointags” and “forktags” as tag lists. The result is a list of prime numbers starting at two and up to 1000. We can now wrap this operator into a macro and export it as an operator class.

The marshalling and plugin invocation is handled inside the macro operator, and it behaves as if it would have been a basic ANKHOR operator right from the start.

The Python plugin is part of the ExternOperators package (together with a similar plugin for R). The package has to be installed using the “New / Install Library” menu entry from the ribbon bar.

The Python plugin currently supports the 32Bit version 3.2.x of Python, so you will have to install this first. The environment variable $PYTHONHOME has to be set for the plugin to find the correct version of Python. A reboot may be required at this point.

Download FlowSheet-Package (ZIP archive)

Wir nutzen Cookies auf unserer Website. Einige von ihnen sind essenziell für den Betrieb der Seite, während andere uns helfen, diese Website und die Nutzererfahrung zu verbessern (Tracking Cookies). Sie können selbst entscheiden, ob Sie die Cookies zulassen möchten. Bitte beachten Sie, dass bei einer Ablehnung womöglich nicht mehr alle Funktionalitäten der Seite zur Verfügung stehen.