Permalink
Browse files

StackExchangeResultset is now a *lot* more useful - can fetch new pages.

  • Loading branch information...
lucjon committed May 24, 2010
1 parent ae175e1 commit 69cbc91e449ca647a7b7ccf6fdc52931a468ae95
Showing with 23 additions and 3 deletions.
  1. +23 −3 stackexchange.py
View
@@ -21,9 +21,29 @@ def __str__(self):
return 'Received HTTP error \'%d\'.' % self.urlerror.code
class StackExchangeResultset(tuple):
def __new__(cls, items, page, pagesize):
cls.page, cls.pagesize = page, pagesize
def __new__(cls, items, page, pagesize, build_info):
cls.page, cls.pagesize, cls.build_info = page, pagesize, build_info
return tuple.__new__(cls, items)
def reload(self):
# kind of a cheat, but oh well
return self.fetch_page(self.page)
def fetch_page(self, page):
new_params = list(self.build_info)
new_params[4] = new_params[4].copy()
new_params[4]['page'] = page
return new_params[0].build(*new_params[1:])
def fetch_extended(self, page):
next = self.fetch_page(page)
return self + next
def fetch_next(self):
return self.fetch_page(self.page + 1)
def extend_next(self):
return self.fetch_extended(self.page + 1)
class Enumeration(object):
@classmethod
@@ -345,7 +365,7 @@ def build(self, url, typ, collection, kw={}):
json_item['_params_'] = kw # convenient access to the kw hash
items.append(typ(json_item, self))
return StackExchangeResultset(items, page, pagesize)
return StackExchangeResultset(items, page, pagesize, (self, url, typ, collection, kw))
else:
# this isn't a paginated resultset (unlikely, but possible - eg badges)
return tuple([typ(x, self) for x in json[collection]])

0 comments on commit 69cbc91

Please sign in to comment.