question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow easy paging for list operations

See original GitHub issue

Taking the comment on https://github.com/GoogleCloudPlatform/gcloud-python/pull/889/files#r31042158 over here.

Requiring explicit paging for topics is really ugly, there must be a better way than:

all_topics = []

topics, token = client.list_topics()
all_topics.extend(topics)
while token is not None:
  topics, token = client.list_topics(page_token=token)
  all_topics.extend(topics)

If our concern is “people could make a boatload of requests and they don’t realize it”, we could always limit this like we do with the recursive deleting in storage?

topics = client.list_topics(limit=100)

We can do a bunch of this with the page_size parameter, but that might mean that I have to wait for everything to come back before starting any work, which seems kind of ridiculous.

It’d be really nice if that limit and page-size stuff was both there, so it’s easy to do things like “I want the first 5000 topics, and I want to pull them from the server in chunks of 50”:

for topic in client.list_topics(page_size=50, limit=5000):
  push_work_to_other_system(topic)

To add a bit more context, I’d like to toss out: what if we made all of our list operations return iterators?

The use cases I see here are…

  1. I want everything, give it all to me (for topic in list_topics())
  2. I want up to N things, stop giving them to me then (for topic in list_topics(limit=100))
  3. I don’t know how many I want, I’ll know when I want to stop though… (for topics in list_topics(): if topic.name == 'foo': break)
  4. Combination of the previous two (I don’t know when I want to stop, but don’t let me go on forever, kill it at say… 1000)
  5. I want to pick up where I left off, I saved a token somewhere (sort of like offset)! (for topic in list_topics(page_token=token, limit=100))

The “let’s just always return page, page_token” thing doesn’t really make all of those use-cases all that fun… But if we always return iterators, they are all easy.

Further, let’s say I have a weird case where I just want one page worth of stuff… list_topics().get_current_page() could return what you want, no?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:32 (32 by maintainers)

github_iconTop GitHub Comments

2reactions
rimeycommented, Apr 12, 2016

Here’s my input, for what it’s worth.

I agree that it is important to provide a simple interface that doesn’t require the user to deal with page tokens. If we don’t, people will often choose to fetch just the first page. I know I’ve done that.

Whether it is better to return a list or an iterator will depend on the API, as will whether it makes sense to offer a limit parameter.

Default limits are rarely going to be appropriate. A default limit on PubSub topics would surely be bad, for instance. Failing to override such a default with limit=None would almost always be an insidious bug.

If listing PubSub topics returns an iterator, I actually don’t think it makes sense to offer a limit parameter at all. I can’t think of a use-case for it. If the user really did want to retrieve some limited number of arbitrary topics, they could do that by terminating the iteration early.

1reaction
tseavercommented, Oct 3, 2016

My $0.02 on the strategy:

  • Rename the current paging methods, from list_foo -> list_foo_paged.
  • Add sibling list_foo methods which just return an iterator already wrapped around list_foo_paged. These methods do not carry params for all the possible iterator properties to list_foo: instead, users who care can configure them before beginning to iterate.
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - how to apply paging on a list - Stack Overflow
You can page a list with LINQ, like this: IList<demodto> GetPage(IList<demodto> list, int page, int pageSize) { return list.
Read more >
Efficiently Paging Through Large Amounts of Data (C#)
Default Paging can be implemented by simply checking the Enable Paging option in the data Web control s smart tag; however, whenever viewing ......
Read more >
REST API Design: Filtering, Sorting, and Pagination - Moesif
URL parameters is the easiest way to add basic filtering to REST APIs. ... that returns a list of entities will need to...
Read more >
Paging 3 — Easier Way to Pagination | by Metin Özcura
Most apps display a large list of data, however, users generally see only a small part of it, so fetching all of the...
Read more >
Paging in Operating System - GeeksforGeeks
Let us look some important terminologies: Logical Address or Virtual Address (represented in bits): An address generated by the CPU; Logical ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found