Skip to content

Commit

Permalink
Fix returns for simple Python functions for optional ws. Refs #5701
Browse files Browse the repository at this point in the history
Also add two extra tests for optional workspace properties.
  • Loading branch information
martyngigg committed Aug 8, 2012
1 parent 24a4122 commit c3f80d6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ namespace Mantid
.def("__init__", make_constructor(&createPropertyWithLockFlag,
default_call_policies(),
args("name","defaultValue", "direction", "optional", "locking", "validator")))
.def("isOptional", &TypedWorkspaceProperty::isOptional, "Returns true if the property has been marked as optional")
;

}
Expand Down
12 changes: 10 additions & 2 deletions Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,27 @@ def ignore_property(name, ignore_regex):
# Parent algorithms store their workspaces in the ADS
# Child algorithms store their workspaces in the property
if not algm_obj.isChild() and _is_workspace_property(prop):
retvals.append(_ads[prop.valueAsStr])
value = prop.valueAsStr
if value == '':
if not prop.isOptional():
raise RuntimeError("Logical error. Output workspace property '%s' on '%s' is mandatory but has no value."
"Please contact development team." % (name, algm_obj.name()))
else:
retvals.append(_ads[value])
else:
if not hasattr(prop, 'value'):
print ('Unknown property type encountered. "%s" on "%s" is not understood by '
'Python. Return value skipped. Please contact development team' % (name, algm_obj.name()))
else:
retvals.append(prop.value)

nvals = len(retvals)
nlhs = lhs[0]
if nlhs > 1 and nvals != nlhs:
# There is a discrepancy in the number are unpacking variables
# Let's not have the more cryptic unpacking error raised
raise RuntimeError("%s is trying to return %d output(s) but you have provided %d variable(s). These numbers must match." % (func_name, nvals, nlhs))
raise RuntimeError("%s is trying to return %d output(s) but you have provided %d variable(s). "
"These numbers must match." % (func_name, nvals, nlhs))
if nvals > 1:
return tuple(retvals) # Create a tuple
elif nvals == 1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import sys
from mantid.api import AlgorithmFactory, mtd
from mantid.api import AlgorithmFactory, mtd, ITableWorkspace
import mantid.simpleapi as simpleapi

class SimpleAPITest(unittest.TestCase):
Expand Down Expand Up @@ -98,6 +98,34 @@ def test_function_uses_OutputWorkspace_keyword_over_lhs_var_name_if_provided(sel
wkspace = simpleapi.CreateWorkspace(data,data,OutputWorkspace=wsname,NSpec=1,UnitX='Wavelength')
self.assertTrue( wsname in mtd )

def test_function_returns_only_mandatory_workspace_when_optional_output_is_not_given(self):
_demo = simpleapi.CreateMDHistoWorkspace(SignalInput='1,2,3,4,5',ErrorInput='1,1,1,1,1',
Dimensionality='1',Extents='-1,1',NumberOfBins='5',Names='A',Units='U')
wsname = 'test_function_returns_only_mandatory_workspace_when_optional_output_is_not_given'
query = simpleapi.QueryMDWorkspace(InputWorkspace=_demo,OutputWorkspace=wsname,MaximumRows='500',
Normalisation='volume')

self.assertTrue( isinstance(query, ITableWorkspace) )
self.assertTrue( wsname in mtd )

def test_function_returns_both_mandatory_and_optional_workspaces_when_optional_output_is_given(self):
_demo = simpleapi.CreateMDWorkspace(Dimensions='2',EventType='MDEvent',Extents='1,10,1,10',Names='a,b',
Units='MomentumTransfer,MomentumTransfer',SplitInto='4')
wsname = 'test_function_returns_only_mandatory_workspace_when_optional_output_is_not_given'
wsname_box = wsname + '_box'
query = simpleapi.QueryMDWorkspace(InputWorkspace=_demo,OutputWorkspace=wsname,MaximumRows='500',
Normalisation='volume',BoxDataTable=wsname_box)

self.assertTrue( wsname in mtd )
self.assertTrue( wsname_box in mtd )

self.assertTrue( type(query) == tuple )
self.assertEquals( 2, len(query) )

self.assertTrue( isinstance(query[0], ITableWorkspace) )
self.assertTrue( isinstance(query[1], ITableWorkspace) )


def test_that_dialog_call_raises_runtime_error(self):
try:
simpleapi.LoadEventNexusDialog()
Expand Down

0 comments on commit c3f80d6

Please sign in to comment.