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
import core.modules.module_registry
from core.modules.vistrails_module import Module, ModuleError

version = "0.9.0"
name = "PythonCalc"
identifier = "edu.utah.sci.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

    reg.add_module(PythonCalc)
    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'))
    reg.add_input_port(PythonCalc, "op",
                       (core.modules.basic_modules.String, 'the operation'))
    reg.add_output_port(PythonCalc, "value",
                       (core.modules.basic_modules.Float, 'the result'))

This Page