Skip to content

accessors

soft_prop

soft_prop(wraps)

Create an over-writable property

Example
class MyClass

    @soft_prop
    def a_soft_prop(self):
        return 'red'

    @property
    def a_hard_prop(self):
        return 'red'

my_instance = MyClass()
my_instance.a_hard_prop = 'blue'  # raises error
print(my_instance.a_hard_prop)  # still prints red
my_instance.a_soft_prop = 'blue'  # allowed!
print(my_instance.softy)  # prints blue
Source code in cassini/accessors.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def soft_prop(wraps: Callable[[Any], V]) -> V:
    """
    Create an over-writable property

    Example
    -------

    ```python

    class MyClass

        @soft_prop
        def a_soft_prop(self):
            return 'red'

        @property
        def a_hard_prop(self):
            return 'red'

    my_instance = MyClass()
    my_instance.a_hard_prop = 'blue'  # raises error
    print(my_instance.a_hard_prop)  # still prints red
    my_instance.a_soft_prop = 'blue'  # allowed!
    print(my_instance.softy)  # prints blue
    ```
    """
    return cast(V, functools.wraps(wraps)(_SoftProp(wraps)))  # type: ignore[arg-type]

cached_prop

cached_prop(wrapped)

Decorator for turning functions/ methods into _CachedProps.

Example
class MyClass:

    def __init__(self):
        self.count = 0

    @cached_prop
    def count_once(self):
        self.count += 1
        return count

my_instance = MyClass
print(my_instance.count_once)  # prints 1
print(my_instance.count_once)  # ... still prints 1!
Source code in cassini/accessors.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def cached_prop(wrapped: Callable[[Any], V]) -> V:
    """
    Decorator for turning functions/ methods into `_CachedProp`s.

    Example
    -------

    ```python
    class MyClass:

        def __init__(self):
            self.count = 0

        @cached_prop
        def count_once(self):
            self.count += 1
            return count

    my_instance = MyClass
    print(my_instance.count_once)  # prints 1
    print(my_instance.count_once)  # ... still prints 1!
    ```
    """
    return cast(V, functools.wraps(wrapped)(_CachedProp(wrapped)))  # type: ignore[arg-type]

cached_class_prop

cached_class_prop(wrapped)

Decorator for turning functions/ methods into _CachedClassProps.

First argument of wrapped will be self.__class__ rather than self.

Example
class MyClass:

    count = 0

    @cached_class_prop
    def count_once(cls):
        cls.count += 1
        return count

print(MyClass.count_once)  # prints 1
print(MyClass.count_once)  # ... still prints 1!
Source code in cassini/accessors.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def cached_class_prop(wrapped: Callable[[Any], V]) -> V:
    """
    Decorator for turning functions/ methods into `_CachedClassProp`s.

    First argument of wrapped will be `self.__class__` rather than `self`.

    Example
    -------

    ```python
    class MyClass:

        count = 0

        @cached_class_prop
        def count_once(cls):
            cls.count += 1
            return count

    print(MyClass.count_once)  # prints 1
    print(MyClass.count_once)  # ... still prints 1!
    ```
    """
    return cast(V, functools.wraps(wrapped)(_CachedClassProp(wrapped)))  # type: ignore[arg-type]