-
Notifications
You must be signed in to change notification settings - Fork 2k
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
util: Implement Paginator
class as interface to access a list by pages
#11247
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the looping tests be switched to parametrization to get individual results? The others I would make separate tests for each assert as well so all tests happen and are available as results.
chia/util/paginator.py
Outdated
_source: Sequence[object] | ||
_page_size: int | ||
|
||
page_sizes: Tuple[int, ...] = (5, 10, 25, 50, 100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this restriction improves the class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See 7f9f53d
def page_count(self) -> int: | ||
return max(1, ceil(len(self._source) / self._page_size)) | ||
|
||
def get_page(self, page: int) -> Sequence[object]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want 1-based paging instead of 0-based? I don't know what is normal for HTTP APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know either. But i changed it to 0-based now because i was also not super convinced about the decision, see a948633
84405ad
to
34a41c1
Compare
@altendky See last few commits, improved tests (kind of mixed in commits with the other two changes), addressed the comments and rebased to get the correct workflows. |
chia/util/paginator.py
Outdated
return max(1, ceil(len(self._source) / self._page_size)) | ||
|
||
def get_page(self, page: int) -> Sequence[object]: | ||
if page < 0 or page > self.page_count() - 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if page < 0 or page > self.page_count() - 1: | |
if 0 <= page < self.page_count(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You wanna raise for valid values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uh... right... i like 'em in order. (remember when you had an approval and gave me more chances to complain? :]
) but really, whatever here.
if page < 0 or page > self.page_count() - 1: | |
if page < 0 or self.page_count() <= page: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I almost like it, but what about 04d9b60 😄 (swap the order)
tests/util/test_paginator.py
Outdated
], | ||
) | ||
def test_get_page_valid(length: int, page: int, page_size: int, expected_data: List[int]) -> None: | ||
assert Paginator(list(range(0, length)), page_size).get_page(page) == expected_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:]
maybe?
assert Paginator(list(range(0, length)), page_size).get_page(page) == expected_data | |
assert Paginator(range(length), page_size).get_page(page) == expected_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected_data
is no range though :)
…ges (#11247) * util: Implement `Paginator` class as interface to access a list by pages * Be less restrictive about page sizes and refactor tests * Make the pages based of 0 instead of 1 and some more test refactoring * More tests * Adjust workflows after rebase * Introduce `Paginator.create` * `<=` instead of `- 1`
…ges (#11247) * util: Implement `Paginator` class as interface to access a list by pages * Be less restrictive about page sizes and refactor tests * Make the pages based of 0 instead of 1 and some more test refactoring * More tests * Adjust workflows after rebase * Introduce `Paginator.create` * `<=` instead of `- 1`
Preparation for a follow up PR to provide paginated plot RPC endpoints. Im fine waiting with the merge until that other PR is ready, just opening this to already get it reviewed.