Skip to content

Instantly share code, notes, and snippets.

@carlmjohnson carlmjohnson/paginator.py
Last active Mar 26, 2018

Embed
What would you like to do?
Python for making 1, 2 … 5, 6, 7 … 10, 11 pagination correctly.
def pages(n, max, window=1, ellipsis=None):
s = set(range(window + 2))
s.update(range(n-window, n+window+1))
s.update(range(max-window, max+1))
seq = [i for i in sorted(s) if i > 0 and i <= max]
res = [1]
for i, nxt in zip(seq, seq[1:]):
diff = nxt - i
if diff == 2:
res.append(i+1)
elif diff > 2:
res.append(ellipsis)
res.append(nxt)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.