Skip to content

Commit

Permalink
Added Cursor object to help with pagination. "page" based pagination …
Browse files Browse the repository at this point in the history
…working.
  • Loading branch information
joshthecoder committed Sep 29, 2009
1 parent 11fe753 commit f4f9b05
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -6,6 +6,9 @@ during upgrade will be listed here.
+ API
+ Added cursor parameter to API.friends and API.followers methods.
Note: page parameter is being deprecated by twitter on 10/26
+ Cursor
Added the Cursor object to help with pagination within the API.
Please see the pagination tutorial for more details.

1.0.1 -> 1.1
=======================
Expand Down
1 change: 1 addition & 0 deletions tweepy/__init__.py
Expand Up @@ -14,6 +14,7 @@
from . auth import BasicAuthHandler, OAuthHandler
from . streaming import Stream, StreamListener
from . logging import TweepyLogger, DummyLogger, ConsoleLogger, FileLogger
from . cursor import Cursor

# Global, unauthenticated instance of API
api = API()
Expand Down
3 changes: 3 additions & 0 deletions tweepy/binder.py
Expand Up @@ -150,5 +150,8 @@ def _call(api, *args, **kargs):

return out

# Expose extra data in callable object
_call.allowed_param = allowed_param

return _call

75 changes: 75 additions & 0 deletions tweepy/cursor.py
@@ -0,0 +1,75 @@
# Tweepy
# Copyright 2009 Joshua Roesslein
# See LICENSE

class Cursor(object):
"""Pagination helper class"""

def __init__(self, method, *args, **kargs):
if 'cursor' in method.allowed_param:
self.iterator = CursorIterator(method, args, kargs)
elif 'page' in method.allowed_param:
self.iterator = PageIterator(method, args, kargs)
else:
raise TweepError('This method does not perform pagination')

def pages(self, limit=0):
"""Return iterator for pages"""
if limit > 0:
self.iterator.limit = limit
return self.iterator

def items(self, limit=0):
"""Return iterator for items in each page"""
items_yielded = 0
for page in self.iterator:
for item in page:
if limit > 0 and items_yielded == limit:
raise StopIteration
items_yielded += 1
yield item

class BaseIterator(object):

def __init__(self, method, args, kargs):
self.method = method
self.args = args
self.kargs = kargs
self.limit = 0

def next(self):
raise NotImplementedError

def prev(self):
raise NotImplementedError

def __iter__(self):
return self

class CursorIterator(BaseIterator):

def next(self):
return

def prev(self):
return

class PageIterator(BaseIterator):

def __init__(self, method, args, kargs):
BaseIterator.__init__(self, method, args, kargs)
self.current_page = 0

def next(self):
self.current_page += 1
items = self.method(page=self.current_page, *self.args, **self.kargs)
if len(items) == 0 or (self.limit > 0 and self.current_page > self.limit):
raise StopIteration
return items

def prev(self):
if (self.current_page == 0):
raise TweepError('Can not page back more, at first page')
self.current_page -= 1
return self.method(page=self.current_page, *self.args, **self.kargs)

3 changes: 0 additions & 3 deletions tweepyshell.py
Expand Up @@ -18,9 +18,6 @@
exit(1)

api = tweepy.API.new(auth='basic', username=sys.argv[1], password=sys.argv[2])
if api.verify_credentials() is False:
print 'Invalid username and/or password!'
exit(1)

code.interact('<Tweepy shell>', local={'tweepy': tweepy, 'api': api})

0 comments on commit f4f9b05

Please sign in to comment.