diff options
author | pukkandan <pukkandan.ytdlp@gmail.com> | 2022-11-13 08:29:49 +0530 |
---|---|---|
committer | pukkandan <pukkandan.ytdlp@gmail.com> | 2022-11-13 08:29:49 +0530 |
commit | 83cc7b8aae1328b0d148b631357f753c61c38a29 (patch) | |
tree | 8fe4a5bc9d97dcb633298c5e1f2f56d7904d19d9 /yt_dlp/utils.py | |
parent | 0a4b2f4180b57f8e82b5d9c078c070ddfac7c727 (diff) | |
download | hypervideo-pre-83cc7b8aae1328b0d148b631357f753c61c38a29.tar.lz hypervideo-pre-83cc7b8aae1328b0d148b631357f753c61c38a29.tar.xz hypervideo-pre-83cc7b8aae1328b0d148b631357f753c61c38a29.zip |
[utils] `classproperty`: Add cache support
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 40313f50e..a6bf897dc 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -5847,14 +5847,23 @@ def cached_method(f): 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): - return self.func(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): |