예제 #1
0
        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()
        checkpointable_subcontext = adapt(self.subcontext, ICheckpointable)
        copy.subcontext = checkpointable_subcontext.checkpoint()
        return copy


declareAdapter(
    lambda x: DataContext(subcontext=x),
    [ICheckpointable, IListenableContext, IPersistableContext,
        IRestrictedContext],
    forProtocols=[IContext],
)


예제 #2
0
        # HasTraits.clone() semantics force a shallow copy of these traits. If
        # you store mutable objects like lists and dicts using Any traits, then
        # HasTraits.clone() will pass along the reference instead of making
        # a shallow copy. Modifications to that trait in the original will
        # propagate to the copy. This is not desirable.
        #
        # However, if the trait is declared with copy='shallow' metadata, then
        # HasTraits.clone_traits() will do the right thing. This is preferred.
        #
        # Subcontexts of either DataContext or MultiContext should be adaptable
        # to the ICheckpointable interface. An adapter for dicts is provided
        # here.


class CheckPointableDictAdapter(object):
    """ Adapt a dictionary to the ICheckpointable interface.
    """
    def __init__(self, dict):
        self.dict = dict

    def checkpoint(self):
        return self.dict.copy()

declareAdapter(
    CheckPointableDictAdapter,
    [ICheckpointable],
    forTypes=[dict],
)