From 2762dbb17e8556140f9fff0c0aa3373c521f5e09 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Fri, 20 May 2022 20:55:21 +0530 Subject: [PATCH] [compat] Add `functools.cached_property` --- yt_dlp/compat/functools.py | 12 ++++++++++++ yt_dlp/downloader/common.py | 3 ++- yt_dlp/downloader/external.py | 3 ++- yt_dlp/extractor/common.py | 4 ++-- yt_dlp/extractor/youtube.py | 6 +++--- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/yt_dlp/compat/functools.py b/yt_dlp/compat/functools.py index 36c983642d..c3c4d8f486 100644 --- a/yt_dlp/compat/functools.py +++ b/yt_dlp/compat/functools.py @@ -10,3 +10,15 @@ cache # >= 3.9 except NameError: cache = lru_cache(maxsize=None) + +try: + cached_property # >= 3.8 +except NameError: + class cached_property: + def __init__(self, func): + update_wrapper(self, func) + self.func = func + + def __get__(self, instance, _): + setattr(instance, self.func.__name__, self.func(instance)) + return getattr(instance, self.func.__name__) diff --git a/yt_dlp/downloader/common.py b/yt_dlp/downloader/common.py index 276675532f..d74692130d 100644 --- a/yt_dlp/downloader/common.py +++ b/yt_dlp/downloader/common.py @@ -11,6 +11,7 @@ MultilinePrinter, QuietMultilinePrinter, ) +from ..compat import functools from ..utils import ( NUMBER_RE, LockingUnsupportedError, @@ -102,7 +103,7 @@ def to_screen(self, *args, **kargs): __to_screen = to_screen - @property + @functools.cached_property def FD_NAME(self): return re.sub(r'(?