Click here to Skip to main content
15,892,643 members
Articles / All Topics

Building ANKHOR Operators in Python

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Dec 2014CPOL2 min read 5.2K   2  
How to build 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.

Image 1

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:

Image 2

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:

Python
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”.

Image 3

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.

Image 4

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.

Author

Dr. Ulrich Sigmund

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --