Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Save an extra fetch by storing the data fetched for page 1 #57

Merged
merged 2 commits into from
Jun 9, 2015

Conversation

leo-dor
Copy link
Contributor

@leo-dor leo-dor commented Jun 6, 2015

Small change to pagination mechanism - when loading pagination info, data for page 1 is loaded but currently not stored. When asking for page 1, the data is loaded - again. Example script:

import discogs_client
d = discogs_client.Client('ExampleApplication/0.1')
d.verbose = True

a = d.artist(251558)
print "Iterating over releases..."
for r in a.releases:
    pass

Output without this change shows an extra fetch:
Iterating over releases...
GET https://api.discogs.com/artists/251558
GET https://api.discogs.com/artists/251558/releases?per_page=50&page=1
GET https://api.discogs.com/artists/251558/releases?per_page=50&page=1
GET https://api.discogs.com/artists/251558/releases?per_page=50&page=2
...

After the fix, page 1 is no longer loaded twice:
Iterating over releases...
GET https://api.discogs.com/artists/251558
GET https://api.discogs.com/artists/251558/releases?per_page=50&page=1
GET https://api.discogs.com/artists/251558/releases?per_page=50&page=2
...

@leo-dor leo-dor changed the title When fetching pagination info, store the data fetched for page 1 Save an extra fetch by storing the data fetched for page 1 Jun 6, 2015
@@ -291,6 +291,9 @@ def _invalidate(self):

def _load_pagination_info(self):
data = self.client._get(self._url_for_page(1))
self._pages[1] = [
self._transform(item) for item in data[self._list_key]
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind unindenting these last 2 lines? We indent by 4 spaces and generally like to keep the ending parenthesis/bracket/etc. aligned with where it opens. i.e.,

self._pages[1] = [
    self._transform(item) for item in data[self._list_key]
]

@leo-dor
Copy link
Contributor Author

leo-dor commented Jun 9, 2015

Fixed the spacing - sorry, that was a sloppy copy-paste.

@rodneykeeling
Copy link
Contributor

Awesome, thanks! Looks good to me! Thanks for the optimization, @leo-dor! 👍

rodneykeeling added a commit that referenced this pull request Jun 9, 2015
Save an extra fetch by storing the data fetched for page 1
@rodneykeeling rodneykeeling merged commit 7ab0391 into discogs:master Jun 9, 2015
@leo-dor leo-dor deleted the pagination branch June 9, 2015 18:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants