1
1
import time
2
- from typing import Any , Callable , Dict , Iterable , Iterator , TypeVar , Optional
2
+ from typing import Any , Callable , Dict , Iterable , Iterator , Optional , TypeVar
3
3
4
4
from pynamodb .constants import (CAMEL_COUNT , ITEMS , LAST_EVALUATED_KEY , SCANNED_COUNT ,
5
5
CONSUMED_CAPACITY , TOTAL , CAPACITY_UNITS )
@@ -90,8 +90,8 @@ def __init__(
90
90
self ._operation = operation
91
91
self ._args = args
92
92
self ._kwargs = kwargs
93
- self ._first_iteration = True
94
93
self ._last_evaluated_key = kwargs .get ('exclusive_start_key' )
94
+ self ._is_last_page = False
95
95
self ._total_scanned_count = 0
96
96
self ._rate_limiter = None
97
97
if rate_limit :
@@ -102,18 +102,17 @@ def __iter__(self) -> Iterator[_T]:
102
102
return self
103
103
104
104
def __next__ (self ) -> _T :
105
- if self ._last_evaluated_key is None and not self . _first_iteration :
105
+ if self ._is_last_page :
106
106
raise StopIteration ()
107
107
108
- self ._first_iteration = False
109
-
110
108
self ._kwargs ['exclusive_start_key' ] = self ._last_evaluated_key
111
109
112
110
if self ._rate_limiter :
113
111
self ._rate_limiter .acquire ()
114
112
self ._kwargs ['return_consumed_capacity' ] = TOTAL
115
113
page = self ._operation (* self ._args , settings = self ._settings , ** self ._kwargs )
116
114
self ._last_evaluated_key = page .get (LAST_EVALUATED_KEY )
115
+ self ._is_last_page = self ._last_evaluated_key is None
117
116
self ._total_scanned_count += page [SCANNED_COUNT ]
118
117
119
118
if self ._rate_limiter :
@@ -170,10 +169,11 @@ def __init__(
170
169
settings : OperationSettings = OperationSettings .default ,
171
170
) -> None :
172
171
self .page_iter : PageIterator = PageIterator (operation , args , kwargs , rate_limit , settings )
173
- self ._first_iteration = True
174
172
self ._map_fn = map_fn
175
173
self ._limit = limit
176
174
self ._total_count = 0
175
+ self ._index = 0
176
+ self ._count = 0
177
177
178
178
def _get_next_page (self ) -> None :
179
179
page = next (self .page_iter )
@@ -189,10 +189,6 @@ def __next__(self) -> _T:
189
189
if self ._limit == 0 :
190
190
raise StopIteration
191
191
192
- if self ._first_iteration :
193
- self ._first_iteration = False
194
- self ._get_next_page ()
195
-
196
192
while self ._index == self ._count :
197
193
self ._get_next_page ()
198
194
@@ -209,7 +205,7 @@ def next(self) -> _T:
209
205
210
206
@property
211
207
def last_evaluated_key (self ) -> Optional [Dict [str , Dict [str , Any ]]]:
212
- if self ._first_iteration or self . _index == self ._count :
208
+ if self ._index == self ._count :
213
209
# Not started iterating yet: return `exclusive_start_key` if set, otherwise expect None; or,
214
210
# Entire page has been consumed: last_evaluated_key is whatever DynamoDB returned
215
211
# It may correspond to the current item, or it may correspond to an item evaluated but not returned.
0 commit comments