|
7 | 7 | from copy import deepcopy |
8 | 8 | from ..utils.math import bracket |
9 | 9 | from scipy.optimize import brentq |
| 10 | +from functools import cached_property |
10 | 11 |
|
11 | | - |
12 | | -# TODO: When we require Python 3.8+, replace with |
13 | | -# functools.cached_property decorator |
14 | | -class cached_property(property): |
15 | | - """A decorator that converts a function into a lazy property. The |
16 | | - function wrapped is called the first time to retrieve the result |
17 | | - and then that calculated result is used the next time you access |
18 | | - the value:: |
19 | | -
|
20 | | - class Foo(object): |
21 | | -
|
22 | | - @cached_property |
23 | | - def foo(self): |
24 | | - # calculate something important here |
25 | | - return 42 |
26 | | -
|
27 | | - The class has to have a `__dict__` in order for this property to |
28 | | - work. |
29 | | -
|
30 | | - This decorator is adapted slightly from the one in the werkzeug module: |
31 | | - https://tedboy.github.io/flask/_modules/werkzeug/utils.html#cached_property |
32 | | - """ |
33 | | - |
34 | | - def __init__(self, func, name=None, doc=None): |
35 | | - self.__name__ = name or func.__name__ |
36 | | - self.__module__ = func.__module__ |
37 | | - self.__doc__ = doc or func.__doc__ |
38 | | - self.func = func |
39 | | - |
40 | | - def __set__(self, obj, value): |
41 | | - obj.__dict__[self.__name__] = value |
42 | | - |
43 | | - def __get__(self, obj, type=None): |
44 | | - if obj is None: |
45 | | - return self |
46 | | - try: |
47 | | - value = obj.__dict__[self.__name__] |
48 | | - except KeyError: |
49 | | - value = self.func(obj) |
50 | | - obj.__dict__[self.__name__] = value |
51 | | - return value |
| 12 | +cached_property = cached_property # for easier access |
52 | 13 |
|
53 | 14 |
|
54 | 15 | def material_property(func): |
|
0 commit comments