From 40adbd9d5ca3f1cdd9e45ba4a3fcae92f1f6d6cd Mon Sep 17 00:00:00 2001 From: Joshua Roesslein Date: Sun, 9 Jun 2013 22:35:06 -0700 Subject: [PATCH] Fix a few bugs with the new ID cursors. - stop once end of collection reached (empty result). - don't raise ValueError if ResultSet is empty when trying to get max/min ID of the set (max() and min() don't accept empty lists) --- tweepy/cursor.py | 4 ++++ tweepy/models.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tweepy/cursor.py b/tweepy/cursor.py index 680b9e8..edda0ba 100644 --- a/tweepy/cursor.py +++ b/tweepy/cursor.py @@ -91,6 +91,8 @@ class IdIterator(BaseIterator): # to avoid requesting duplicate items. max_id = self.since_id - 1 if self.max_id else None data = self.method(max_id = max_id, *self.args, **self.kargs) + if len(data) == 0: + raise StopIteration self.max_id = data.max_id self.since_id = data.since_id return data @@ -99,6 +101,8 @@ class IdIterator(BaseIterator): """Fetch a set of items with IDs greater than current set.""" since_id = self.max_id data = self.method(since_id = since_id, *self.args, **self.kargs) + if len(data) == 0: + raise StopIteration self.max_id = data.max_id self.since_id = data.since_id return data diff --git a/tweepy/models.py b/tweepy/models.py index 7171c40..fcccbf1 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -16,11 +16,17 @@ class ResultSet(list): @property def max_id(self): - return self._max_id or max(self.ids()) + if self._max_id: + return self._max_id + ids = self.ids() + return max(ids) if ids else None @property def since_id(self): - return self._since_id or min(self.ids()) + if self._since_id: + return self._since_id + ids = self.ids() + return min(ids) if ids else None def ids(self): return [item.id for item in self if hasattr(item, 'id')] -- 2.25.1