]> jfr.im git - yt-dlp.git/commitdiff
[utils] Add `__getitem__` for `PagedList`
authorpukkandan <redacted>
Mon, 17 May 2021 13:44:20 +0000 (19:14 +0530)
committerpukkandan <redacted>
Fri, 28 May 2021 20:42:08 +0000 (02:12 +0530)
yt_dlp/utils.py

index 02a12307a7f3a8ac05d70e66c8f6be2682876e5e..40b9c4cf38ca71f30ae7756b57f3ea58be48091d 100644 (file)
@@ -4000,6 +4000,15 @@ def __len__(self):
         # This is only useful for tests
         return len(self.getslice())
 
+    def getslice(self, start, end):
+        raise NotImplementedError('This method must be implemented by subclasses')
+
+    def __getitem__(self, idx):
+        if not isinstance(idx, int) or idx < 0:
+            raise TypeError('indices must be non-negative integers')
+        entries = self.getslice(idx, idx + 1)
+        return entries[0] if entries else None
+
 
 class OnDemandPagedList(PagedList):
     def __init__(self, pagefunc, pagesize, use_cache=True):