1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import core.modules.module_registry
from core.modules.vistrails_module import Module, ModuleError

version = "0.9.0"
name = "PythonCalc"
identifier = "org.vistrails.vistrails.pythoncalc"

class PythonCalc(Module):
    """PythonCalc is a module that performs simple arithmetic operations on its inputs."""

    def compute(self):
        v1 = self.getInputFromPort("value1")
        v2 = self.getInputFromPort("value2")
        result = self.op(v1, v2)
        self.setResult("value", result)

    def op(self, v1, v2):
        op = self.getInputFromPort("op")
        if op == '+': return v1 + v2
        elif op == '-': return v1 - v2
        elif op == '*': return v1 * v2
        elif op == '/': return v1 / v2
        else: raise ModuleError(self, "unrecognized operation: '%s'" % op)

###############################################################################

def initialize(*args, **keywords):

    # We'll first create a local alias for the module registry so that
    # we can refer to it in a shorter way.
    reg = core.modules.module_registry.registry

    # VisTrails cannot currently automatically detect your derived
    # classes, and the ports that they support as input and
    # output. Because of this, you as a module developer need to let
    # VisTrails know that you created a new module. This is done by calling
    # function addModule:
    reg.add_module(PythonCalc)

    # In a similar way, you need to report the ports the module wants
    # to make available. This is done by calling addInputPort and
    # addOutputPort appropriately. These calls only show how to set up
    # one-parameter ports. We'll see in later tutorials how to set up
    # multiple-parameter plots.
    reg.add_input_port(PythonCalc, "value1",
                       (core.modules.basic_modules.Float, 'the first argument'))
    reg.add_input_port(PythonCalc, "value2",
                       (core.modules.basic_modules.Float, 'the second argument'))
    # We declare this port as an enum, so that the user may only select one of
    # the listed values
    reg.add_input_port(PythonCalc, "op",
                       (core.modules.basic_modules.String, 'the operation'),
                       entry_types=['enum'], values=["['+', '-', '*', '/']"])
    reg.add_output_port(PythonCalc, "value",
                       (core.modules.basic_modules.Float, 'the result'))

This Page