Skip to content
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

Merged
merged 7 commits into from
Apr 30, 2022

Conversation

xdustinface
Copy link
Contributor

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.

altendky
altendky previously approved these changes Apr 28, 2022
Copy link
Contributor

@altendky altendky left a 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.

_source: Sequence[object]
_page_size: int

page_sizes: Tuple[int, ...] = (5, 10, 25, 50, 100)
Copy link
Contributor

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.

Copy link
Contributor Author

@xdustinface xdustinface Apr 28, 2022

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]:
Copy link
Contributor

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.

Copy link
Contributor Author

@xdustinface xdustinface Apr 28, 2022

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

tests/util/test_paginator.py Outdated Show resolved Hide resolved
@xdustinface
Copy link
Contributor Author

@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.

@xdustinface xdustinface requested a review from altendky April 28, 2022 23:05
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if page < 0 or page > self.page_count() - 1:
if 0 <= page < self.page_count():

Copy link
Contributor Author

@xdustinface xdustinface Apr 28, 2022

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?

Copy link
Contributor

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.

Suggested change
if page < 0 or page > self.page_count() - 1:
if page < 0 or self.page_count() <= page:

Copy link
Contributor Author

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)

],
)
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
Copy link
Contributor

Choose a reason for hiding this comment

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

:] maybe?

Suggested change
assert Paginator(list(range(0, length)), page_size).get_page(page) == expected_data
assert Paginator(range(length), page_size).get_page(page) == expected_data

Copy link
Contributor Author

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 :)

@wjblanke wjblanke merged commit 984c442 into Chia-Network:main Apr 30, 2022
wallentx pushed a commit that referenced this pull request May 2, 2022
…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`
xdustinface added a commit that referenced this pull request May 4, 2022
…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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants