Commit 2f4781c0 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Implemented "cursor" based Cursor iterator.

parent caec131b
......@@ -2,6 +2,8 @@
# Copyright 2009 Joshua Roesslein
# See LICENSE
from . error import TweepError
class Cursor(object):
"""Pagination helper class"""
......@@ -48,11 +50,29 @@ class BaseIterator(object):
class CursorIterator(BaseIterator):
def __init__(self, method, args, kargs):
BaseIterator.__init__(self, method, args, kargs)
self.next_cursor = -1
self.prev_cursor = 0
self.count = 0
def next(self):
return
if self.next_cursor == 0 or (self.limit and self.count == self.limit):
raise StopIteration
data, self.next_cursor, self.prev_cursor = self.method(
cursor=self.next_cursor, *self.args, **self.kargs
)
self.count += 1
return data
def prev(self):
return
if self.prev_cursor == 0:
raise TweepError('Can not page back more, at first page')
data, self.next_cursor, self.prev_cursor = self.method(
cursor=self.prev_cursor, *self.args, **self.kargs
)
self.count -= 1
return data
class PageIterator(BaseIterator):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment