@@ -1212,6 +1212,41 @@ 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
+
1218
+ When implementing `_run_interface`, set outputs with::
1219
+
1220
+ self._results[out_name] = out_value
1221
+
1222
+ This can be a way to upgrade a ``Function`` interface to do type checking:
1223
+
1224
+ >>> def double(x):
1225
+ ... return 2 * x
1226
+
1227
+ >>> class DoubleInputSpec(BaseInterfaceInputSpec):
1228
+ ... x = traits.Float(mandatory=True)
1229
+
1230
+ >>> class DoubleOutputSpec(TraitedSpec):
1231
+ ... doubled = traits.Float()
1232
+
1233
+ >>> class Double(SimpleInterface):
1234
+ ... input_spec = DoubleInputSpec
1235
+ ... output_spec = DoubleOutputSpec
1236
+ ...
1237
+ ... def _run_interface(self, runtime):
1238
+ ... self._results['doubled'] = double(self.inputs.x)
1239
+ ... return runtime
1240
+ """
1241
+ def __init__ (self , from_file = None , resource_monitor = None , ** inputs ):
1242
+ super (SimpleInterface , self ).__init__ (
1243
+ from_file = from_file , resource_monitor = resource_monitor , ** inputs )
1244
+ self ._results = {}
1245
+
1246
+ def _list_outputs (self ):
1247
+ return self ._results
1248
+
1249
+
1215
1250
class Stream (object ):
1216
1251
"""Function to capture stdout and stderr streams with timestamps
1217
1252
0 commit comments