Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
    def validate(self, value):
        """ Validates a value. """

        if value is None:
            if not self.allow_none:
                raise TraitError

        elif self.default_value == value:
            pass

        else:
            value = adapt(value, self.klass, Undefined)
            if value is Undefined:
                raise TraitError

        return value
    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
Esempio n. 5
0
    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