|
| 1 | +# from: https://github.com/stefanholek/lazy |
| 2 | +# |
| 3 | +# Copyright (c) 2011-2017 Stefan H. Holek |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions |
| 8 | +# are met: |
| 9 | +# |
| 10 | +# 1. Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 13 | +# notice, this list of conditions and the following disclaimer in the |
| 14 | +# documentation and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 17 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 20 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 22 | +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 23 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | +# SUCH DAMAGE. |
| 27 | + |
| 28 | + |
| 29 | +"""Decorator to create lazy attributes.""" |
| 30 | + |
| 31 | +from __future__ import annotations |
| 32 | + |
| 33 | +import functools |
| 34 | + |
| 35 | + |
| 36 | +class lazy: |
| 37 | + """ |
| 38 | + Lazy descriptor. |
| 39 | +
|
| 40 | + Used as a decorator to create lazy attributes. Lazy attributes |
| 41 | + are evaluated on first use. |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__(self, func): |
| 45 | + self.__func = func |
| 46 | + functools.wraps(self.__func)(self) |
| 47 | + |
| 48 | + def __get__(self, inst, inst_cls): |
| 49 | + if inst is None: |
| 50 | + return self |
| 51 | + |
| 52 | + if not hasattr(inst, "__dict__"): |
| 53 | + raise AttributeError( |
| 54 | + f"'{inst_cls.__name__}' object has no attribute '__dict__'" |
| 55 | + ) |
| 56 | + |
| 57 | + name = self.__name__ |
| 58 | + if name.startswith("__") and not name.endswith("__"): |
| 59 | + name = f"_{inst_cls.__name__}{name}" |
| 60 | + |
| 61 | + value = self.__func(inst) |
| 62 | + inst.__dict__[name] = value |
| 63 | + return value |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def invalidate(cls, inst, name): |
| 67 | + """ |
| 68 | + Invalidate a lazy attribute. |
| 69 | +
|
| 70 | + This obviously violates the lazy contract. A subclass of lazy |
| 71 | + may however have a contract where invalidation is appropriate. |
| 72 | + """ |
| 73 | + # These commands from the original are not currently used by larch and |
| 74 | + # are omitted for speed. They are left here in case they are needed |
| 75 | + # for future compatibility. |
| 76 | + # |
| 77 | + # inst_cls = inst.__class__ |
| 78 | + # |
| 79 | + # if not hasattr(inst, '__dict__'): |
| 80 | + # raise AttributeError("'%s' object has no attribute '__dict__'" % (inst_cls.__name__,)) |
| 81 | + # |
| 82 | + # if name.startswith('__') and not name.endswith('__'): |
| 83 | + # name = '_%s%s' % (inst_cls.__name__, name) |
| 84 | + # |
| 85 | + # if not isinstance(getattr(inst_cls, name), cls): |
| 86 | + # raise AttributeError("'%s.%s' is not a %s attribute" |
| 87 | + # % (inst_cls.__name__, name, cls.__name__)) |
| 88 | + |
| 89 | + if name in inst.__dict__: |
| 90 | + del inst.__dict__[name] |
0 commit comments