@@ -1212,6 +1212,50 @@ def save_inputs_to_json(self, json_file):
1212
1212
json .dump (inputs , fhandle , indent = 4 , ensure_ascii = False )
1213
1213
1214
1214
1215
+ class SimpleInterface (BaseInterface ):
1216
+ """ An interface pattern that allows outputs to be set in a dictionary
1217
+ called ``_results`` that is automatically interpreted by
1218
+ ``_list_outputs()`` to find the outputs.
1219
+
1220
+ When implementing ``_run_interface``, set outputs with::
1221
+
1222
+ self._results[out_name] = out_value
1223
+
1224
+ This can be a way to upgrade a ``Function`` interface to do type checking.
1225
+
1226
+ Examples
1227
+ --------
1228
+ >>> def double(x):
1229
+ ... return 2 * x
1230
+ ...
1231
+ >>> class DoubleInputSpec(BaseInterfaceInputSpec):
1232
+ ... x = traits.Float(mandatory=True)
1233
+ ...
1234
+ >>> class DoubleOutputSpec(TraitedSpec):
1235
+ ... doubled = traits.Float()
1236
+ ...
1237
+ >>> class Double(SimpleInterface):
1238
+ ... input_spec = DoubleInputSpec
1239
+ ... output_spec = DoubleOutputSpec
1240
+ ...
1241
+ ... def _run_interface(self, runtime):
1242
+ ... self._results['doubled'] = double(self.inputs.x)
1243
+ ... return runtime
1244
+
1245
+ >>> dbl = Double()
1246
+ >>> dbl.inputs.x = 2
1247
+ >>> dbl.run().outputs.doubled
1248
+ 4.0
1249
+ """
1250
+ def __init__ (self , from_file = None , resource_monitor = None , ** inputs ):
1251
+ super (SimpleInterface , self ).__init__ (
1252
+ from_file = from_file , resource_monitor = resource_monitor , ** inputs )
1253
+ self ._results = {}
1254
+
1255
+ def _list_outputs (self ):
1256
+ return self ._results
1257
+
1258
+
1215
1259
class Stream (object ):
1216
1260
"""Function to capture stdout and stderr streams with timestamps
1217
1261
0 commit comments