def checkpoint(self): """ Make a shallow copy of the context. Technically, this is actually a fairly deep copy. All of the object structure should be replicated, but the actual dictionary storage will be shallowly copied:: copy = context.shallow_copy() copy[key] is context[key] for key in context.keys() These semantics are useful for saving out checkpointed versions of the context for implementing an undo/redo stack. They may not be useful for other purposes. Returns ------- copy : IContext """ copy = self.clone_traits() new_subcontexts = [] for context in self.subcontexts: checkpointable_subcontext = adapt(context, ICheckpointable) new_subcontexts.append(checkpointable_subcontext.checkpoint()) copy.subcontexts = new_subcontexts return copy
def execute(self, context, globals=None, inputs=None, outputs=None): icontext = adapt(context, IContext) if globals is None: globals = {} exec self.code in globals, icontext
def _adaptee_changed(self, context): """ Handles being bound to a IContext object. """ self.data_context_name = context.name values = [] contexts = [] for name in list(context.keys()): value = context[name] try: adapt(value, IContext) except AdaptationError: # Is not a subcontext. values.append(name) else: # Is a subcontext. contexts.append(name) self.data_context_values = values self.data_contexts = contexts
def _adaptee_changed ( self, context ): """ Handles being bound to a IContext object. """ self.data_context_name = context.name values = [] contexts = [] for name in context.keys(): value = context[ name ] try: adapt( value, IContext ) except AdaptationError: # Is not a subcontext. values.append( name ) else: # Is a subcontext. contexts.append( name ) self.data_context_values = values self.data_contexts = contexts
def execute(self, context, globals=None, inputs=None, outputs=None): icontext = adapt(context, IContext) if inputs is None: inputs = [] if outputs is None: outputs = [] if globals is None: globals = {} exec self.code in globals, icontext return set(inputs), set(outputs)
def __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if 'context' in locals_val and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return
def __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if locals_val.has_key('context') and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return
def _unpack_drag_event ( self, event ): """ Inspect object and see if it can be dropped here. If so, return the droppable object. Otherwise, return None. """ # fixme: poor man's interface checking for IBasicFunctionInfo. # fixme: This will not work for a LocalFunctionInfo, and it should... # fixme: This was for plotting... #elif isinstance( event.obj, BlockVariable ): # result = event.obj # If it supports the IMinimalFunctionInfo interface, we're good. try: result = adapt(event.obj, IMinimalFunctionInfo) except NotImplementedError: result = None return result
def _unpack_drag_event(self, event): """ Inspect object and see if it can be dropped here. If so, return the droppable object. Otherwise, return None. """ # fixme: poor man's interface checking for IBasicFunctionInfo. # fixme: This will not work for a LocalFunctionInfo, and it should... # fixme: This was for plotting... #elif isinstance( event.obj, BlockVariable ): # result = event.obj # If it supports the IMinimalFunctionInfo interface, we're good. try: result = adapt(event.obj, IMinimalFunctionInfo) except NotImplementedError: result = None return result
def execute(self, context, globals=None, inputs=None, outputs=None): """ Execute code in context, optionally restricting on inputs or outputs if supplied Parameters ---------- context : Dict-like globals : Dict-like, optional inputs : List of strings, options outputs : List of strings, optional Returns ------- inputs : set the inputs to the restricted block outpus : set the outputs of the restricted block """ icontext = adapt(context, IContext) if globals is None: globals = {} if inputs is None: inputs = set() if outputs is None: outputs = set() # filter out and inputs or outputs that are not in the code in_and_out = self._block.inputs.union(self._block.outputs) filtered_inputs = set(inputs).intersection(in_and_out) filtered_outputs = set(outputs).intersection(in_and_out) #If called with no inputs or outputs the full block executes if filtered_inputs or filtered_outputs: block = self._block.restrict(inputs=filtered_inputs, outputs=filtered_outputs) else: block = self._block block.execute(icontext, global_context=globals) return block.inputs, block.outputs
def _create_variable(self, key, value): """ Create variables from key, value applied here. Parameters: ----------- key : Str value : Float/Str/Array/IContext Returns: -------- result: List(Tuple) tuple is ordered as (key, value, datatype) """ if isinstance(value, int) or isinstance(value, float): return [(key, value, 'scalar')] if isinstance(value, basestring): return [(key, value, 'str')] if isinstance(value, numpy.ndarray): return [(key, value, 'array')] try: value = adapt(value, IContext) variables = [] for sub_key in value.keys(): sub_value = value[sub_key] sub_key = "%s[%r]" % (key, sub_key) variable_list = self._create_variable(sub_key, sub_value) if variable_list: variables += variable_list return variables except AdaptationFailure: pass return None