Skip to content

Page.pages simpler calculation #9

Open
@ricardo-reis-1970

Description

@ricardo-reis-1970

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions