PK!J fallback_property/__init__.pyimport logging from typing import Type, TypeVar, Generic, Callable logger = logging.getLogger(__name__) Class = TypeVar("Class") Value = TypeVar("Value") Method = Callable[[Class], Value] class FallbackDescriptor(Generic[Class, Value]): def __init__(self, func: Method, cached: bool = True, logging: bool = False) -> None: """ Initialize the descriptor. Arguments --------- func Fallback function if no value exists. cached Cache the value calculated by `func`. logging Log a warning if fallback function is used. """ self.__doc__ = getattr(func, "__doc__") # keep the docs self.func = func self.cached = cached self.logging = logging self.prop_name = f"__{self.func.__name__}" def __get__(self, obj: Class, cls: Type[Class]) -> Value: """ Get the value. Return either the cached value or call the underlying function and optionally cache its result. """ if not hasattr(obj, self.prop_name): if self.logging: logger.warning("Using `%s` without prefetched value.", self.func) value: Value = self.func(obj) if self.cached: self.__set__(obj, value) else: return value return getattr(obj, self.prop_name) def __set__(self, obj: Class, value: Value) -> None: """ Store value in a private property. """ setattr(obj, self.prop_name, value) def __delete__(self, obj: Class) -> None: """ Clear current value from private property. """ if hasattr(obj, self.prop_name): delattr(obj, self.prop_name) def fallback_property( cached: bool = True, logging: bool = False ) -> Callable[[Method], FallbackDescriptor]: """ Decorate a class method to return a precalculated value instead. This might be useful if you have a function that aggregates values from related objects, which could already be fetched using an annotated queryset. The decorated methods will favor the precalculated value over calling the actual method. NOTE: The annotated value must have the same name as the decorated function! """ def inner(func: Method) -> FallbackDescriptor: return FallbackDescriptor(func, cached=cached, logging=logging) return inner PK! ɷ)fallback_property-0.1.0.dist-info/LICENSECopyright (c) Jonas und der Wolf GmbH. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Jonas und der Wolf GmbH nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PK!HڽTU'fallback_property-0.1.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!Hǂ4*fallback_property-0.1.0.dist-info/METADATAYQs۸~E%M79q㫝L$\QHHR_oR"-g:W=bw 䚜LJE^4 s|UFWd& =_ϣ(LĩH)F:mz - ,$KdԹt Qڑ+2+*3ԪPAn8z U2/K*;3o2dG7؄:y-%kB̜ӊr]T:q㤫L_HA{o˕2BO= Ƃ_eeIi**r'ۄ%>\ \a]m%qn6QjJ۽G38\+Z#%D S$1;~ՕJxF/Z%uiَk9RF?]!:ybPދ;29/IHžGOt *O8p >Tsf-<35GڬBkT߷{`:eW]N!OZy{;}jw?u)>O&n|)L;&^jȏ]e%ʰoサO8Ux LJ!}5_&^ͦL:4(D)Dޚ Y,grKO*'aSmChl/,XFä"`6-nRX7STAY8zpe&TcVnZ(?kB 8Wb O-hۍ!j_R_w~F >PX>ƨ߅@*8y;_vf~m*^M}'NnE9?4hʮ aw6O /" ibNAP{ LK ʼnPK!J fallback_property/__init__.pyPK! ɷ) fallback_property-0.1.0.dist-info/LICENSEPK!HڽTU')fallback_property-0.1.0.dist-info/WHEELPK!Hǂ4*fallback_property-0.1.0.dist-info/METADATAPK!H(fallback_property-0.1.0.dist-info/RECORDPK8