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.

[enh] Use islice rather than slice in order to work with generators.

See original GitHub issue

Right now, you select the data using []. itertools.islice stills works on sequences, but also works on generators, which is useful when there is a lot of data to go through.

Specifically, I think replacing this line: https://github.com/uriyyo/fastapi-pagination/blob/b6e287524cffa628e76b87e79d5cab0e6f579035/fastapi_pagination/paginator.py#L17 would be sufficient to add the functionality while changing nothing in the existing API.

The line would become

items=list(it.islice(sequence, raw_params.offset, raw_params.offset + raw_params.limit]),

(after import itertools as it).

What do you think?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
uriyyocommented, Jun 9, 2021

I think we should introduce new paginator that will be able to work with iterables. Also we should add new pages classes that will not contains total field as far as we don’t know how many elements are in a iterables and for example it is not possible to calculate it for infinity generators.

Thats my idea, I am happy to hear yours opinion🙂

1reaction
lingstercommented, Apr 30, 2021

psycop2 is the postgres driver. Happy to take a look or would it be useful to add a dockerfile to the project to help reproduce the error and have a consistent development environment setup?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why would I want to use itertools.islice instead of normal list ...
Using islice () or chain() allows you to iterate over the list(s) in the way you want, without having to use more memory...
Read more >
itertools.islice() goes over all the pre-initial elements even if ...
If L is a big sequence and I do "itertool.islice(L, 1000000, None)", islice will go over a million elements before returning the first...
Read more >
4. Iterators and Generators - Python Cookbook, 3rd ... - O'Reilly
Iterators and generators can't normally be sliced, because no information is known about their length (and they don't implement indexing). The result of...
Read more >
Python Iterators and Generators Disambiguated
3.3.4 Slice an iterator with islice() ! 4 Defining Generators ... 4.4 Generator Expressions Instead of List Comps. 5 Practice Problems.
Read more >
ActivePython 3.6.6 Documentation
Instead of being implemented in C code inside the Python interpreter, ... but using generators and try...finally is actually necessary in order to...
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