Exemplo n.º 1
0
def combine_class(class_name: str, *traits, **resolved_conflicts):
    """ This function composes new class out of any number of traits.

    Args:
        class_name: Name of the new class.
        traits: Collection of traits, such as functions, classes or instances.

    Keyword Args:
        name of trait (str): new name

    Example of combining multiple classes to one:

    >>> class One:
    ...     def first(self): return 1
    ...
    >>> class Two:
    ...     def second(self): return 2
    ...
    >>> class Three:
    ...     def third(self): return 3
    ...
    >>> Combination = combine_class("Combination", One, Two, Three)
    >>> instance = Combination()
    >>> instance.first(), instance.second(), instance.third()
    (1, 2, 3)
    >>> instance.__class__.__name__
    'Combination'
    """
    NewClass = type(class_name, (object,), {})
    add_traits(NewClass, *traits)
    return NewClass
Exemplo n.º 2
0
def combine_class(*traits, **resolved_conflicts):
    """
    This function composes new class out of any number of traits.

    >>> class One:
    ...     def first(self): return 1
    ...
    >>> class Two:
    ...     def second(self): return 2
    ...
    >>> class Three:
    ...     def third(self): return 3
    ...
    >>> Combination = combine_class(One, Two, Three)
    >>> instance = Combination()
    >>> instance.first(), instance.second(), instance.third()
    (1, 2, 3)
    """
    NewClass = type("NewClass", (object, ), {})
    add_traits(NewClass, *traits)
    return NewClass
Exemplo n.º 3
0
def combine_class(*traits, **resolved_conflicts):
    """
    This function composes new class out of any number of traits.

    >>> class One:
    ...     def first(self): return 1
    ...
    >>> class Two:
    ...     def second(self): return 2
    ...
    >>> class Three:
    ...     def third(self): return 3
    ...
    >>> Combination = combine_class(One, Two, Three)
    >>> instance = Combination()
    >>> instance.first(), instance.second(), instance.third()
    (1, 2, 3)
    """
    NewClass = type("NewClass", (object,), {})
    add_traits(NewClass, *traits)
    return NewClass
Exemplo n.º 4
0
 def __call__(self, *args, **kwargs):
     add_traits(self._target_object, *args, **kwargs)
Exemplo n.º 5
0
def setproperty(target,
                fget=None,
                fset=None,
                fdel=None,
                source=None,
                name=None):
    """
    Convinience function that dynamically creates property to an object.
    (If you have property created, just use 'add_traits')

    This function has different behavior depending on the target object,
    whether it is an instance or a class. If target is an instance the
    property is being set only for that instance. In case the object is
    a class, the property will be added to it normally to class.

    Args:
        target (object or type): Target object, which can be any instance or class.
        fget (str or function): Getter function or its name
        fset (str or function): Setter function or its name
        fdel (str or function): Deleter function or its name
        source (object or type): Source object in case fget, fset and fdel are strings.

    Keyword args:
        name (str): Name of the property
        name of fget (str): Name of the property

    Example, where new property is added dynamically into instance:

    >>> class Example:
    ...     def __init__(self):
    ...         self.__value = 42
    ...
    ...     def set_value(self, new_value):
    ...         self.__value = new_value
    ...
    ...     def value(self):
    ...         return self.__value
    ...
    ...     def del_value(self):
    ...         self.__value = 42
    ...
    >>> instance = Example()
    >>> setproperty(instance, "value", "set_value", "del_value", name="my_property")
    >>> instance.my_property
    42
    """
    resolutions = {}

    # If some arguments are left out, skip them from test.
    args = [arg for arg in (fget, fset, fdel) if arg]

    # There must be at least one argument
    if not args:
        raise TypeError("Property needs to have at least one function.")

    # Handle case, when all provided arguments are strings.
    elif all(isinstance(arg, str) for arg in args):
        owner = source or target
        resolutions[fget] = name

        new_property = property(getattr(owner, fget or "", None),
                                getattr(owner, fset or "", None),
                                getattr(owner, fdel or "", None))

    # It is also possible to provide functions.
    elif all(inspect.isroutine(arg) for arg in args):
        resolutions[fget.__name__] = name
        new_property = property(fget, fset, fdel)

    # Other conditions are not supported.
    else:
        raise TypeError("Unsupported setup for property functions!")

    add_traits(target, new_property, **resolutions)
Exemplo n.º 6
0
 def __call__(self, *args, **kwargs):
     add_traits(self._target_object, *args, **kwargs)
Exemplo n.º 7
0
def setproperty(target, fget=None, fset=None, fdel=None, source=None, name=None):
    """
    Convinience function that dynamically creates property to an object.
    (If you have property created, just use 'add_traits')

    This function has different behavior depending on the target object,
    whether it is an instance or a class. If target is an instance the
    property is being set only for that instance. In case the object is
    a class, the property will be added to it normally to class.

    Args:
        target (object or type): Target object, which can be any instance or class.
        fget (str or function): Getter function or its name
        fset (str or function): Setter function or its name
        fdel (str or function): Deleter function or its name
        source (object or type): Source object in case fget, fset and fdel are strings.

    Keyword args:
        name (str): Name of the property
        name of fget (str): Name of the property

    Example, where new property is added dynamically into instance:

    >>> class Example:
    ...     def __init__(self):
    ...         self.__value = 42
    ...
    ...     def set_value(self, new_value):
    ...         self.__value = new_value
    ...
    ...     def value(self):
    ...         return self.__value
    ...
    ...     def del_value(self):
    ...         self.__value = 42
    ...
    >>> instance = Example()
    >>> setproperty(instance, "value", "set_value", "del_value", name="my_property")
    >>> instance.my_property
    42
    """
    resolutions = {}

    # If some arguments are left out, skip them from test.
    args = [arg for arg in (fget, fset, fdel) if arg]

    # There must be at least one argument
    if not args:
        raise TypeError("Property needs to have at least one function.")

    # Handle case, when all provided arguments are strings.
    elif all(isinstance(arg, str) for arg in args):
        owner = source or target
        resolutions[fget] = name

        new_property = property(getattr(owner, fget or "", None),
                                getattr(owner, fset or "", None),
                                getattr(owner, fdel or "", None))

    # It is also possible to provide functions.
    elif all(inspect.isroutine(arg) for arg in args):
        resolutions[fget.__name__] = name
        new_property = property(fget, fset, fdel)

    # Other conditions are not supported.
    else:
        raise TypeError("Unsupported setup for property functions!")

    add_traits(target, new_property, **resolutions)