Esempio n. 1
0
def proxy(obj1, obj2, name, overwrite=False):
    """Redirects a method, property, or trait from object 1 to object 2."""
    if hasattr(obj1, name) and overwrite is False:
        raise ClassProxyError(
            "Cannot proxy the attribute '{name}' from {cls2} because "
            "{cls1} already has this attribute.".format(name=name,
                                                        cls1=obj1.__class__,
                                                        cls2=obj2.__class__))
    attr = getattr(obj2, name)

    # First check if this thing is a trait (see traitlets)
    cls_attr = getattr(obj2.__class__, name)
    if is_trait(cls_attr) or type(attr) == property:
        thing = property(lambda self: getattr(obj2, name))

    elif isinstance(attr, types.MethodType):

        @wraps(attr)
        def thing(self, *args, **kwargs):
            return attr(*args, **kwargs)

    # Anything else appended on the class is just an attribute of the class.
    else:
        thing = attr

    setattr(obj1.__class__, name, thing)
Esempio n. 2
0
def trait_to_QWidget(name, trait, callback=lambda _: None):
    if is_trait(trait):
        label = QtGui.QLabel(name)

        if isinstance(trait, Int):
            return label, mkQSpinBox(trait.default_value, callback,
                                     (trait.min, trait.max))
        elif isinstance(trait, Float):
            return label, mkQDoubleSpinBox(trait.default_value, callback,
                                           (trait.min, trait.max))
        elif isinstance(trait, Complex):
            raise NotImplementedError
        elif isinstance(trait, Bool):
            return label, mkQRadioButton(trait.default_value, callback)
        elif isinstance(trait, Unicode):
            raise NotImplementedError
        elif isinstance(trait, Enum):
            return label, mkQComboBox(trait.default_value, callback,
                                      trait.values)
        # elif isinstance(trait, UseEnum):
        #     # TODO
        #     return label, mkQComboBox(trait.default_value, callback,
        #                               trait.enum_class)
        elif isinstance(trait, Tuple):
            raise NotImplementedError
        else:
            raise InvalidValue('Trait conversion is not supported for: '
                               '{}'.format(trait))
    else:
        raise InvalidType('Trait should be instance of {}'.format(TraitType))
Esempio n. 3
0
def trait_to_type(trait):
    if is_trait(trait):
        if isinstance(trait, Int):
            return int
        elif isinstance(trait, Float):
            return float
        elif isinstance(trait, Complex):
            return complex
        elif isinstance(trait, Bool):
            return bool
        elif isinstance(trait, Unicode):
            raise str
        else:
            raise InvalidValue('Trait conversion is not supported for: '
                               '{}'.format(trait))
    else:
        raise InvalidType('Trait should be instance of {}'.format(TraitType))
Esempio n. 4
0
def trait_to_dtype(name, trait):
    """Convert TraitType to numpy.dtype in format (name, dtype, shape)::

        Int -> numpy.int64
        Float -> numpy.float64
        Complex -> numpy.complex128
        Bool -> numpy.bool_
        Array -> Array.dtype

    Args:
        name (str): Name of the trait
        trait (TraitType): Instance of TraitType

    Returns:
        tuple:
            - Scalar: (str, numpy.dtype)
            - Array: (str, numpy.dtype, shape)
    """
    if is_trait(trait):
        if isinstance(trait, Int):
            return name, np.int64
        elif isinstance(trait, Float):
            return name, np.float64
        elif isinstance(trait, Complex):
            return name, np.complex128
        elif isinstance(trait, Bool):
            return name, np.bool_
        elif isinstance(trait, Unicode):
            raise NotImplementedError
        elif isinstance(trait, Enum):
            raise NotImplementedError
        elif isinstance(trait, Array):
            return name, trait.dtype, trait.default_value.shape
        else:
            raise InvalidValue('Trait conversion is not supported for: '
                               '{}'.format(trait))
    else:
        raise InvalidType('Trait should be instance of {}'.format(TraitType))
Esempio n. 5
0
def trait_to_option(name, trait):
    if is_trait(trait):
        if isinstance(trait, Int):
            return click.Option(param_decls=('--' + name, ),
                                default=trait.default_value,
                                type=click.IntRange(trait.min, trait.max))
        elif isinstance(trait, Float):
            return click.Option(param_decls=('--' + name, ),
                                default=trait.default_value,
                                type=float)
        elif isinstance(trait, Complex):
            return click.Option(param_decls=('--' + name, ),
                                default=trait.default_value,
                                type=complex)
        elif isinstance(trait, Bool):
            return click.Option(param_decls=('--' + name, ),
                                default=trait.default_value,
                                is_flag=True)
        elif isinstance(trait, Unicode):
            return click.Option(param_decls=('--' + name, ),
                                default=trait.default_value,
                                type=str)
        elif isinstance(trait, Enum):
            # FIXME: trait.values should be strings
            return click.Option(param_decls=('--' + name, ),
                                default=str(trait.default_value),
                                type=click.Choice(list(map(str,
                                                           trait.values))))
        elif isinstance(trait, Tuple):
            return click.Option(param_decls=('--' + name, ),
                                default=trait.default_value,
                                type=tuple(
                                    trait_to_type(t) for t in trait._traits))
        else:
            raise InvalidValue('Trait conversion is not supported for: '
                               '{}'.format(trait))
    else:
        raise InvalidType('Trait should be instance of {}'.format(TraitType))