Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Iterator.pages and simplify items iteration #2594

Merged
merged 8 commits into from
Oct 25, 2016
Prev Previous commit
Next Next commit
Using the pages iterator in the main iterator.
NOTE: There is a current mismatch between incrementing
Iterator.num_results in Iterator.__iter__ vs. incrementing
it in Iterator.pages (there it won't be incremented, so this
will need to be addressed in a subsequent commit).
  • Loading branch information
dhermes committed Oct 25, 2016
commit 1d38abbfc4069e30cafc3c75f01a50b69d95cc7e
29 changes: 7 additions & 22 deletions core/google/cloud/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,13 @@ def page(self):
return self._page

def __iter__(self):
"""The :class:`Iterator` is an iterator.

:rtype: :class:`Iterator`
:returns: Current instance.
:raises ValueError: If the iterator has already been started.
"""
if self._started:
raise ValueError('Iterator has already started', self)
self._started = True
return self
"""Iterator for each item returned."""
# NOTE: We don't check if the iterator has started since the pages
# iterator already does this.
for page in self.pages:
for item in page:
self.num_results += 1
yield item

def update_page(self, require_empty=True):
"""Move to the next page in the result set.
Expand Down Expand Up @@ -364,18 +361,6 @@ def update_page(self, require_empty=True):
msg = _PAGE_ERR_TEMPLATE % (self._page, self.page.remaining)
raise ValueError(msg)

def next(self):
"""Get the next item from the request."""
self.update_page(require_empty=False)
if self.page is None:
raise StopIteration
item = six.next(self.page)
self.num_results += 1
return item

# Alias needed for Python 2/3 support.
__next__ = next

def _has_next_page(self):
"""Determines whether or not there are more pages with results.

Expand Down