| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- # This Python file uses the following encoding: utf-8
- # @author runhey
- # github https://github.com/runhey
- from functools import wraps
- from typing import Callable, Generic, TypeVar
- T = TypeVar("T")
- class cached_property(Generic[T]):
- """
- cached-property from https://github.com/pydanny/cached-property
- Add typing support
- A property that is only computed once per instance and then replaces itself
- with an ordinary attribute. Deleting the attribute resets the property.
- Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
- """
- def __init__(self, func: Callable[..., T]):
- self.func = func
- def __get__(self, obj, cls) -> T:
- if obj is None:
- return self
- value = obj.__dict__[self.func.__name__] = self.func(obj)
- return value
- def del_cached_property(obj, name):
- """
- Delete a cached property safely.
- Args:
- obj:
- name (str):
- """
- try:
- del obj.__dict__[name]
- except KeyError:
- pass
- def has_cached_property(obj, name):
- """
- Check if a property is cached.
- Args:
- obj:
- name (str):
- """
- return name in obj.__dict__
|