Skip to content

Decorators

  • UTILITY DECORATORS

enum_class_prop

A decorator for creating a static property for Enum classes.

Source code in src/utils/decorators.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
class enum_class_prop:
    """A decorator for creating a static property for Enum classes."""

    def __init__(self, method: Callable):
        """
        Initializes the property.
        @param method: Class method being decorated.
        """
        self._method = method
        self._name = method.__name__

    def __get__(self, instance, owner):
        """
        Computes and caches the value of a property when accessed.
        @param instance: Instance of the class where descriptor is accessed.
        @param owner: The class that the descriptor exists on.
        @return: The cached value.
        """
        value = self._method(owner)
        setattr(owner, self._name, value)
        return value

__get__(instance, owner)

Computes and caches the value of a property when accessed. @param instance: Instance of the class where descriptor is accessed. @param owner: The class that the descriptor exists on. @return: The cached value.

Source code in src/utils/decorators.py
103
104
105
106
107
108
109
110
111
112
def __get__(self, instance, owner):
    """
    Computes and caches the value of a property when accessed.
    @param instance: Instance of the class where descriptor is accessed.
    @param owner: The class that the descriptor exists on.
    @return: The cached value.
    """
    value = self._method(owner)
    setattr(owner, self._name, value)
    return value

__init__(method)

Initializes the property. @param method: Class method being decorated.

Source code in src/utils/decorators.py
 95
 96
 97
 98
 99
100
101
def __init__(self, method: Callable):
    """
    Initializes the property.
    @param method: Class method being decorated.
    """
    self._method = method
    self._name = method.__name__

auto_prop(func)

Property decorator wrapper that automatically creates a setter.

Source code in src/utils/decorators.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def auto_prop(func: Callable) -> property:
    """Property decorator wrapper that automatically creates a setter."""
    attr_type = func.__annotations__.get('return', str) if (
        hasattr(func, '__annotations__')) else str
    auto_name = f"_{func.__name__}"

    def getter(self) -> attr_type:
        """Getter for retrieving the value of the implied attribute."""
        return getattr(self, auto_name)

    def setter(self, value: attr_type) -> None:
        """Setter for changing the value of the implied attribute."""
        setattr(self, auto_name, value)

    # Return complete property
    return property(getter, setter, doc=func.__doc__)

auto_prop_cached(func)

Property decorator wrapper automatically creates a setter and caches the value.

Source code in src/utils/decorators.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def auto_prop_cached(func: Callable) -> property:
    """Property decorator wrapper automatically creates a setter and caches the value."""
    attr_type = func.__annotations__.get('return', str) if (
        hasattr(func, '__annotations__')) else str
    cache_name = f"_{func.__name__}"

    def getter(self) -> attr_type:
        """Wrapper for getting cached value. If value doesn't exist, initialize it."""
        try:
            return getattr(self, cache_name)
        except AttributeError:
            value = func(self)
            setattr(self, cache_name, value)
            return value

    def setter(self, value: attr_type) -> None:
        """Setter for invalidating the property cache and caching a new value."""
        setattr(self, cache_name, value)

    def deleter(self) -> None:
        """Deleter for invalidating the property cache."""
        if hasattr(self, cache_name):
            delattr(self, cache_name)

    # Return complete property
    return property(getter, setter, deleter, getattr(func, '__doc__', ""))

choose_class_route(condition)

A decorator that routes a method call to the current class or its parent based on a bool condition. @param condition: Route to self if True, otherwise route to self's superclass. @return: The wrapped function.

Source code in src/utils/decorators.py
58
59
60
61
62
63
64
65
66
67
68
69
70
def choose_class_route(condition: bool) -> Callable:
    """
    A decorator that routes a method call to the current class or its parent based on a bool condition.
    @param condition: Route to self if True, otherwise route to self's superclass.
    @return: The wrapped function.
    """
    def decorator(func):
        def wrapper(self, *args, **kwargs):
            if condition:
                return func(self, *args, **kwargs)
            return getattr(super(self.__class__, self), func.__name__)(*args, **kwargs)
        return wrapper
    return decorator

suppress_and_return(return_val)

If an exception occurs within decorated function, suppress it and return given value. @param return_val: Value to return if exception is encountered.

Source code in src/utils/decorators.py
73
74
75
76
77
78
79
80
81
82
83
84
def suppress_and_return(return_val: Any) -> Callable:
    """
    If an exception occurs within decorated function, suppress it and return given value.
    @param return_val: Value to return if exception is encountered.
    """
    def decorator(func):
        def wrapper(self, *args, **kwargs):
            with suppress(Exception):
                return func(self, *args, **kwargs)
            return return_val
        return wrapper
    return decorator