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'))