[enh] Use islice rather than slice in order to work with generators.
See original GitHub issueRight 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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 containstotal
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🙂
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?