mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-21 20:46:36 -05:00
[utils] classproperty
: Add cache support
This commit is contained in:
parent
0a4b2f4180
commit
83cc7b8aae
1 changed files with 12 additions and 3 deletions
|
@ -5847,14 +5847,23 @@ def wrapper(self, *args, **kwargs):
|
|||
|
||||
|
||||
class classproperty:
|
||||
"""property access for class methods"""
|
||||
"""property access for class methods with optional caching"""
|
||||
def __new__(cls, func=None, *args, **kwargs):
|
||||
if not func:
|
||||
return functools.partial(cls, *args, **kwargs)
|
||||
return super().__new__(cls)
|
||||
|
||||
def __init__(self, func):
|
||||
def __init__(self, func, *, cache=False):
|
||||
functools.update_wrapper(self, func)
|
||||
self.func = func
|
||||
self._cache = {} if cache else None
|
||||
|
||||
def __get__(self, _, cls):
|
||||
if self._cache is None:
|
||||
return self.func(cls)
|
||||
elif cls not in self._cache:
|
||||
self._cache[cls] = self.func(cls)
|
||||
return self._cache[cls]
|
||||
|
||||
|
||||
class Namespace(types.SimpleNamespace):
|
||||
|
|
Loading…
Reference in a new issue