https://www.vistrails.org//index.php?title=VistrailsPackagePythonCalcExample&feed=atom&action=historyVistrailsPackagePythonCalcExample - Revision history2024-03-28T19:35:35ZRevision history for this page on the wikiMediaWiki 1.36.2https://www.vistrails.org//index.php?title=VistrailsPackagePythonCalcExample&diff=195&oldid=prevCscheid at 16:45, 31 January 20072007-01-31T16:45:08Z<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 16:45, 31 January 2007</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l90">Line 90:</td>
<td colspan="2" class="diff-lineno">Line 90:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> # to capture the error and report it to the caller of the evaluation</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> # to capture the error and report it to the caller of the evaluation</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> # function.</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> # function.</div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> raise ModuleError("unrecognized operation: '%s'" % op)</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> raise ModuleError(<ins style="font-weight: bold; text-decoration: none;">self, </ins>"unrecognized operation: '%s'" % op)</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> </div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> </div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> ###############################################################################</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> ###############################################################################</div></td></tr>
</table>Cscheidhttps://www.vistrails.org//index.php?title=VistrailsPackagePythonCalcExample&diff=194&oldid=prevCscheid: New page: Here's a full example of a VisTrails package that implements a simple calculator module. ############################################################################ ## ## Copyright (...2007-01-31T16:44:32Z<p>New page: Here's a full example of a VisTrails package that implements a simple calculator module. ############################################################################ ## ## Copyright (...</p>
<p><b>New page</b></p><div>Here's a full example of a VisTrails package that implements a simple calculator module.<br />
<br />
############################################################################<br />
##<br />
## Copyright (C) 2006-2007 University of Utah. All rights reserved.<br />
##<br />
## This file is part of VisTrails.<br />
##<br />
## This file may be used under the terms of the GNU General Public<br />
## License version 2.0 as published by the Free Software Foundation<br />
## and appearing in the file LICENSE.GPL included in the packaging of<br />
## this file. Please review the following to ensure GNU General Public<br />
## Licensing requirements will be met:<br />
## http://www.opensource.org/licenses/gpl-license.php<br />
##<br />
## If you are unsure which license is appropriate for your use (for<br />
## instance, you are interested in developing a commercial derivative<br />
## of VisTrails), please contact us at vistrails@sci.utah.edu.<br />
##<br />
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE<br />
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.<br />
##<br />
############################################################################<br />
"""This package implements a very simple VisTrails module called<br />
PythonCalc. This is intended as a simple example that can be referred<br />
to by users to create their own packages and modules later.<br />
<br />
If you're interested in developing new modules for VisTrails, you<br />
should also consult the documentation in<br />
core/modules/vistrails_module.py.<br />
"""<br />
<br />
import core.modules<br />
import core.modules.module_registry<br />
from core.modules.vistrails_module import Module, ModuleError<br />
<br />
###############################################################################<br />
# PythonCalc<br />
#<br />
# A VisTrails package is simply a Python class that subclasses from<br />
# Module. For this class to be executable, it must define a method<br />
# compute(self) that will perform the appropriate computations and set<br />
# the results.<br />
#<br />
# Extra helper methods can be defined, as usual. In this case, we're<br />
# using a helper method op(self, v1, v2) that performs the right<br />
# operations.<br />
<br />
class PythonCalc(Module):<br />
"""PythonCalc is a module that performs simple arithmetic operations<br />
on its inputs."""<br />
<br />
# This constructor is strictly unnecessary. However, some modules<br />
# might want to initialize per-object data. When implementing your<br />
# own constructor, remember that it must not take any extra<br />
# parameters.<br />
def __init__(self):<br />
Module.__init__(self)<br />
<br />
# This is the method you should implement in every module that<br />
# will be executed directly. VisTrails does not use the return<br />
# value of this method.<br />
def compute(self):<br />
# getInputFromPort is a method defined in Module that returns<br />
# the value stored at an input port. If there's no value<br />
# stored on the port, the method will return None.<br />
v1 = self.getInputFromPort("value1")<br />
v2 = self.getInputFromPort("value2")<br />
<br />
# You should call setResult to store the appropriate results<br />
# on the ports. In this case, we are only storing a<br />
# floating-point result, so we can use the number types<br />
# directly. For more complicated data, you should<br />
# return an instance of a VisTrails Module. This will be made<br />
# clear in further examples that use these more complicated data.<br />
self.setResult("value", self.op(v1, v2))<br />
<br />
def op(self, v1, v2):<br />
op = self.getInputFromPort("op")<br />
if op == '+':<br />
return v1 + v2<br />
elif op == '-':<br />
return v1 - v2<br />
elif op == '*':<br />
return v1 * v2<br />
elif op == '/':<br />
return v1 / v2<br />
# If a module wants to report an error to VisTrails, it should raise<br />
# ModuleError with a descriptive error. This allows the interpreter<br />
# to capture the error and report it to the caller of the evaluation<br />
# function.<br />
raise ModuleError("unrecognized operation: '%s'" % op)<br />
<br />
###############################################################################<br />
# the function initialize is called for each package, after all<br />
# packages have been loaded. It it used to register the module with<br />
# the VisTrails runtime.<br />
<br />
def initialize(*args, **keywords):<br />
<br />
# We'll first create a local alias for the module_registry so that<br />
# we can refer to it in a shorter way.<br />
reg = core.modules.module_registry<br />
<br />
# VisTrails cannot currently automatically detect your derived<br />
# classes, and the ports that they support as input and<br />
# output. Because of this, you as a module developer need to let<br />
# VisTrails know that you created a new module. This is done by calling<br />
# function addModule:<br />
reg.addModule(PythonCalc)<br />
<br />
# In a similar way, you need to report the ports the module wants<br />
# to make available. This is done by calling addInputPort and<br />
# addOutputPort appropriately. These calls only show how to set up<br />
# one-parameter ports. We'll see in later tutorials how to set up<br />
# multiple-parameter plots.<br />
reg.addInputPort(PythonCalc, "value1",<br />
(core.modules.basic_modules.Float, 'the first argument'))<br />
reg.addInputPort(PythonCalc, "value2",<br />
(core.modules.basic_modules.Float, 'the second argument'))<br />
reg.addInputPort(PythonCalc, "op",<br />
(core.modules.basic_modules.String, 'the operation'))<br />
reg.addOutputPort(PythonCalc, "value",<br />
(core.modules.basic_modules.Float, 'the result'))</div>Cscheid