mirror of
https://github.com/ytdl-org/youtube-dl.git
synced 2024-11-18 16:26:23 +00:00
[utils] Add classproperty()
decorator from yt-dlp
This commit is contained in:
parent
3a31e52d27
commit
e6c95bd192
1 changed files with 30 additions and 0 deletions
|
@ -6510,3 +6510,33 @@ def join_nonempty(*values, **kwargs):
|
||||||
if from_dict is not None:
|
if from_dict is not None:
|
||||||
values = (traverse_obj(from_dict, variadic(v)) for v in values)
|
values = (traverse_obj(from_dict, variadic(v)) for v in values)
|
||||||
return delim.join(map(compat_str, filter(None, values)))
|
return delim.join(map(compat_str, filter(None, values)))
|
||||||
|
|
||||||
|
|
||||||
|
# from yt-dlp
|
||||||
|
class classproperty(object):
|
||||||
|
"""property access for class methods with optional caching"""
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
if 'func' in kwargs:
|
||||||
|
func = kwargs.pop('func')
|
||||||
|
elif len(args) > 0:
|
||||||
|
func = args[0]
|
||||||
|
args = args[1:]
|
||||||
|
else:
|
||||||
|
func = None
|
||||||
|
if not func:
|
||||||
|
return functools.partial(cls, *args, **kwargs)
|
||||||
|
return super(classproperty, cls).__new__(cls)
|
||||||
|
|
||||||
|
def __init__(self, func, **kwargs):
|
||||||
|
# kw-only arg
|
||||||
|
cache = kwargs.get('cache', False)
|
||||||
|
functools.update_wrapper(self, func)
|
||||||
|
self.func = func
|
||||||
|
self._cache = {} if cache else None
|
||||||
|
|
||||||
|
def __get__(self, n, 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]
|
||||||
|
|
Loading…
Reference in a new issue