1
1
# Size of a single page in a paginated query.
2
+ from abc import ABC , abstractmethod
3
+ from labelbox .orm .db_object import DbObject
4
+ from typing import Any , Dict , List , Optional
5
+ from labelbox import Client
6
+
2
7
_PAGE_SIZE = 100
3
8
4
9
@@ -12,13 +17,13 @@ class PaginatedCollection:
12
17
"""
13
18
14
19
def __init__ (self ,
15
- client ,
16
- query ,
17
- params ,
18
- dereferencing ,
19
- obj_class ,
20
- cursor_path = None ,
21
- beta = False ):
20
+ client : Client ,
21
+ query : str ,
22
+ params : Dict [ str , str ] ,
23
+ dereferencing : Dict [ str , Any ] ,
24
+ obj_class : DbObject ,
25
+ cursor_path : Optional [ Dict [ str , Any ]] = None ,
26
+ beta : bool = False ):
22
27
""" Creates a PaginatedCollection.
23
28
24
29
Args:
@@ -43,13 +48,10 @@ def __init__(self,
43
48
self .beta = beta
44
49
45
50
self ._fetched_all = False
46
- self ._data = []
51
+ self ._data : List [ Dict [ str , Any ]] = []
47
52
self ._data_ind = 0
48
-
49
- if cursor_path :
50
- self .paginator = _CursorPagination (client , cursor_path )
51
- else :
52
- self .paginator = _OffsetPagination (client )
53
+ self .cursor_path = _CursorPagination (
54
+ client , cursor_path ) if cursor_path else _OffsetPagination (client )
53
55
54
56
def __iter__ (self ):
55
57
self ._data_ind = 0
@@ -81,39 +83,53 @@ def __next__(self):
81
83
return rval
82
84
83
85
84
- class _CursorPagination :
86
+ class _Pagination (ABC ):
87
+
88
+ @abstractmethod
89
+ def fetched_all (self , n_items : int , results : List [Dict [str , Any ]]) -> bool :
90
+ ...
91
+
92
+ @abstractmethod
93
+ def fetch_results (self , query : str , params : Dict [str , Any ],
94
+ beta : bool ) -> Dict [str , Any ]:
95
+ ...
96
+
97
+
98
+ class _CursorPagination (_Pagination ):
85
99
86
- def __init__ (self , client , cursor_path ):
100
+ def __init__ (self , client : Client , cursor_path : Dict [ str , Any ] ):
87
101
self .client = client
88
102
self .cursor_path = cursor_path
89
- self .next_cursor = None
103
+ self .next_cursor : Optional [ str ] = None
90
104
91
- def get_next_cursor (self , results ):
105
+ def get_next_cursor (self , results ) -> Optional [ str ] :
92
106
for path in self .cursor_path :
93
107
results = results [path ]
94
108
return results
95
109
96
- def fetched_all (self , n_items , results ) :
110
+ def fetched_all (self , n_items : int , results : List [ Dict [ str , Any ]]) -> bool :
97
111
self .next_cursor = self .get_next_cursor (results )
98
- return self .next_cursor is None
112
+ return bool ( self .next_cursor is None )
99
113
100
- def fetch_results (self , query , params , beta ):
114
+ def fetch_results (self , query : str , params : Dict [str , Any ],
115
+ beta : bool ) -> Dict [str , Any ]:
101
116
params .update ({'from' : self .next_cursor , 'first' : _PAGE_SIZE })
102
117
return self .client .execute (query , params , beta = beta )
103
118
104
119
105
- class _OffsetPagination :
120
+ class _OffsetPagination ( _Pagination ) :
106
121
107
122
def __init__ (self , client ):
108
123
self .client = client
109
124
self ._fetched_pages = 0
110
125
111
- def fetched_all (self , n_items , results ) :
126
+ def fetched_all (self , n_items : int , results : List [ Dict [ str , Any ]]) -> bool :
112
127
self ._fetched_pages += 1
113
128
if n_items < _PAGE_SIZE :
114
129
return True
115
130
return False
116
131
117
- def fetch_results (self , query , params , beta ):
132
+ def fetch_results (self , query : str , params : Dict [str , Any ],
133
+ beta : bool ) -> Dict [str , Any ]:
118
134
query = query % (self ._fetched_pages * _PAGE_SIZE , _PAGE_SIZE )
119
135
return self .client .execute (query , params , beta = beta )
0 commit comments