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

Page.pages simpler calculation #9

Open
ricardo-reis-1970 opened this issue Jul 26, 2021 · 0 comments
Open

Page.pages simpler calculation #9

ricardo-reis-1970 opened this issue Jul 26, 2021 · 0 comments

Comments

@ricardo-reis-1970
Copy link

Issue

The last line of the Page class reads:

self.pages = int(math.ceil(total / float(page_size)))

This is heavy because it is importing the entire math and because it's getting into the woods with cross conversions into float and back to int. Furthermore, math is being imported just for the sake of ceil.

First fix

As a first approach, I'd have gone with:

from math import ceil
...
self.pages = int(ceil(total / float(page_size)))

but this is just a small optimisation on the exact same solution, thinking of those of us that deal with embedded devices and fight for space.

Second fix

I'd like to put it to you that there is a simpler, lighter way to accomplish this:

self.pages = (total // page_size) + 1

This fix is:

  • universal, in the sense that neither total nor page_size are allowed to be negative or zero;
  • lighter, as it keeps calculations to a minimum and within language constructs.

I hope this helps.

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

No branches or pull requests

1 participant