def __init__(self, fields=None, mandatory_inputs=True, **inputs): super(IdentityInterface, self).__init__(**inputs) if fields is None or not fields: raise Exception('Identity Interface fields must be a non-empty list') self._fields = fields self._mandatory_inputs = mandatory_inputs add_traits(self.inputs, fields)
def __init__(self, input_names, output_names, function=None, **inputs): """ Parameters ---------- input_names: single str or list names corresponding to function inputs output_names: single str or list names corresponding to function outputs. has to match the number of outputs """ super(Function, self).__init__(**inputs) if function: if hasattr(function, '__call__'): try: self.inputs.function_str = getsource(function) except IOError: raise Exception('Interface Function does not accept ' \ 'function objects defined interactively in a python session') elif isinstance(function, str): self.inputs.function_str = function else: raise Exception('Unknown type of function') self.inputs.on_trait_change(self._set_function_string, 'function_str') self._input_names = filename_to_list(input_names) self._output_names = filename_to_list(output_names) add_traits(self.inputs, [name for name in self._input_names]) self._out = {} for name in self._output_names: self._out[name] = None
def __init__(self, format_string=None, **inputs): super(Rename, self).__init__(**inputs) if format_string is not None: self.inputs.format_string = format_string self.fmt_fields = re.findall(r"%\((.+?)\)", format_string) add_traits(self.inputs, self.fmt_fields) else: self.fmt_fields = []
def __init__(self, fields=None, mandatory_inputs=True, **inputs): super(IdentityInterface, self).__init__(**inputs) if fields is None or not fields: raise ValueError('Identity Interface fields must be a non-empty list') # Each input must be in the fields. for in_field in inputs: if in_field not in fields: raise ValueError('Identity Interface input is not in the fields: %s' % in_field) self._fields = fields self._mandatory_inputs = mandatory_inputs add_traits(self.inputs, fields) # Adding any traits wipes out all input values set in superclass initialization, # even it the trait is not in the add_traits argument. The work-around is to reset # the values after adding the traits. self.inputs.set(**inputs)
def __init__(self, allowed_entities=None, out_path_base=None, **inputs): self._allowed_entities = allowed_entities or [] self._metadata = {} self._static_traits = self.input_spec.class_editable_traits() + self._allowed_entities for dynamic_input in set(inputs) - set(self._static_traits): self._metadata[dynamic_input] = inputs.pop(dynamic_input) super(DerivativesDataSink, self).__init__(**inputs) if self._allowed_entities: add_traits(self.inputs, self._allowed_entities) for k in set(self._allowed_entities).intersection(list(inputs.keys())): setattr(self.inputs, k, inputs[k]) self._results['out_file'] = [] if out_path_base: self.out_path_base = out_path_base
def __init__(self, input_names, output_names, function=None, imports=None, **inputs): """ Parameters ---------- input_names: single str or list names corresponding to function inputs output_names: single str or list names corresponding to function outputs. has to match the number of outputs function : callable callable python object. must be able to execute in an isolated namespace (possibly in concert with the ``imports`` parameter) imports : list of strings list of import statements that allow the function to execute in an otherwise empty namespace """ super(Function, self).__init__(**inputs) if function: if hasattr(function, '__call__'): try: self.inputs.function_str = getsource(function) except IOError: raise Exception('Interface Function does not accept ' \ 'function objects defined interactively ' \ 'in a python session') elif isinstance(function, str): self.inputs.function_str = dumps(function) else: raise Exception('Unknown type of function') self.inputs.on_trait_change(self._set_function_string, 'function_str') self._input_names = filename_to_list(input_names) self._output_names = filename_to_list(output_names) add_traits(self.inputs, [name for name in self._input_names]) self.imports = imports self._out = {} for name in self._output_names: self._out[name] = None
def _set_function_string(self, obj, name, old, new): if name == 'function_str': if self.as_module: module = inspect.getmodule(new).__name__ full_name = "%s.%s" % (module, new.__name__) self.inputs.function_str = full_name elif hasattr(new, '__call__'): function_source = getsource(new) fninfo = new.__code__ elif isinstance(new, (str, bytes)): function_source = new fninfo = create_function_from_source(new, self.imports).__code__ self.inputs.trait_set( trait_change_notify=False, **{ '%s' % name: function_source }) # Update input traits input_names = fninfo.co_varnames[:fninfo.co_argcount] new_names = set(input_names) - set(self._input_names) add_traits(self.inputs, list(new_names)) self._input_names.extend(new_names)
def _add_output_traits(self, base): """Add the dynamic output fields""" out_fields = list(self._templates.keys()) + list(self._infields) return add_traits(base, out_fields)
def __init__(self, fields=[], **inputs): super().__init__(**inputs) self.fields = fields add_traits(self.inputs, [*self.fields])
def __init__(self, numinputs=0, **inputs): super(Merge, self).__init__(**inputs) self._numinputs = numinputs add_traits(self.inputs, ['in%d' % (i + 1) for i in range(numinputs)])
def __init__(self, input_names=None, output_names='out', function=None, imports=None, as_module=False, **inputs): """ Parameters ---------- input_names: single str or list or None names corresponding to function inputs if ``None``, derive input names from function argument names output_names: single str or list names corresponding to function outputs (default: 'out'). if list of length > 1, has to match the number of outputs function : callable callable python object. must be able to execute in an isolated namespace (possibly in concert with the ``imports`` parameter) imports : list of strings list of import statements that allow the function to execute in an otherwise empty namespace """ super(Function, self).__init__(**inputs) if function: if as_module: module = inspect.getmodule(function).__name__ full_name = "%s.%s" % (module, function.__name__) self.inputs.function_str = full_name self.inputs.function_str_hash = hashlib.md5( getsource(function).encode('utf-8')).hexdigest() elif hasattr(function, '__call__'): try: self.inputs.function_str = getsource(function) self.inputs.function_str_hash = '' except IOError: raise Exception('Interface Function does not accept ' 'function objects defined interactively ' 'in a python session') else: if input_names is None: fninfo = function.__code__ elif isinstance(function, (str, bytes)): self.inputs.function_str = function self.inputs.function_str_hash = '' if input_names is None: fninfo = create_function_from_source(function, imports).__code__ else: raise Exception('Unknown type of function') if input_names is None: input_names = fninfo.co_varnames[:fninfo.co_argcount] self.as_module = as_module self.inputs.on_trait_change(self._set_function_string, 'function_str') self._input_names = ensure_list(input_names) self._output_names = ensure_list(output_names) add_traits(self.inputs, [name for name in self._input_names]) self.imports = imports self._out = {} for name in self._output_names: self._out[name] = None
def _outputs(self): base = super(ReadSidecarJSON, self)._outputs() if self._fields: base = add_traits(base, self._fields) return base
def __init__(self, input_names=None, output_names='out', function=None, imports=None, as_module=False, **inputs): """ Parameters ---------- input_names: single str or list or None names corresponding to function inputs if ``None``, derive input names from function argument names output_names: single str or list names corresponding to function outputs (default: 'out'). if list of length > 1, has to match the number of outputs function : callable callable python object. must be able to execute in an isolated namespace (possibly in concert with the ``imports`` parameter) imports : list of strings list of import statements that allow the function to execute in an otherwise empty namespace """ super(Function, self).__init__(**inputs) if function: if as_module: module = inspect.getmodule(function).__name__ full_name = "%s.%s" % (module, function.__name__) self.inputs.function_str = full_name elif hasattr(function, '__call__'): try: self.inputs.function_str = getsource(function) except IOError: raise Exception('Interface Function does not accept ' 'function objects defined interactively ' 'in a python session') else: if input_names is None: fninfo = function.__code__ elif isinstance(function, (str, bytes)): self.inputs.function_str = function if input_names is None: fninfo = create_function_from_source(function, imports).__code__ else: raise Exception('Unknown type of function') if input_names is None: input_names = fninfo.co_varnames[:fninfo.co_argcount] self.as_module = as_module self.inputs.on_trait_change(self._set_function_string, 'function_str') self._input_names = ensure_list(input_names) self._output_names = ensure_list(output_names) add_traits(self.inputs, [name for name in self._input_names]) self.imports = imports self._out = {} for name in self._output_names: self._out[name] = None
def _add_output_traits(self, base): return add_traits(base, self._get_outfields())
def _add_output_traits(self, base): return add_traits(base, self._fields)
def _outputs(self): base = super(KeySelect, self)._outputs() base = add_traits(base, self._fields) return base
def _add_output_traits(self, base): return add_traits(base, self._attrs)