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

fix: allow parameters in result set reload & fetch #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions stackexchange/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, json, site, skip_ext = False):
transfer = self.transfer if hasattr(self, 'transfer') else ()
fields = ([A + (None,) for A in alias if len(A) == 2] +
[(k, k, None) for k in transfer if isinstance(k, str)] +
[A for A in alias if len(A) == 3] +
[A for A in alias if len(A) == 3] +
[(k[0],) + k for k in transfer if not isinstance(k, str)])

for dest, key, transform in fields:
Expand Down Expand Up @@ -184,7 +184,7 @@ def __init__(self, code = UNKNOWN, name = None, message = None):
self.code = code
self.name = name
self.message = message

def __str__(self):
if self.code == self.UNKNOWN:
return 'unrecognised error'
Expand Down Expand Up @@ -216,10 +216,10 @@ def __new__(cls, items, build_info, has_more = True, page = 1, pagesize = None):

return instance

def reload(self):
def reload(self, **kw):
"""Refreshes the data in the resultset with fresh API data. Note that this doesn't work with extended resultsets."""
# kind of a cheat, but oh well
return self.fetch_page(self.page)
return self.fetch_page(self.page, **kw)

def fetch_page(self, page, **kw):
"""Returns a new resultset containing data from the specified page of the results. It re-uses all parameters that were passed in
Expand Down Expand Up @@ -249,9 +249,12 @@ def extend_next(self):
"""Returns a new resultset containing data from this resultset AND from the next page."""
return self.fetch_extended(self.page + 1)

def fetch(self):
# Do nothing, but allow multiple fetch calls
return self
def fetch(self, **kw):
"""Return self if parameters are the same; otherwise reload from API"""
if kw == self.build_info[4]:
return self
return self.reload(**kw)


def __iter__(self):
return self.next()
Expand Down Expand Up @@ -356,7 +359,7 @@ def paginated_to_resultset(site, json, typ, collection, params):
# no longer variable in v2.x, having been replaced by a generic field
# 'items'. To perhaps be removed completely at some later point.
items = []

# create strongly-typed objects from the JSON items
for json_item in json['items']:
json_item['_params_'] = params[-1] # convenient access to the kw hash
Expand Down