Skip to content

Instantly share code, notes, and snippets.

@hussaintamboli
Last active December 30, 2021 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hussaintamboli/97833f3df8c64e784dc8 to your computer and use it in GitHub Desktop.
Save hussaintamboli/97833f3df8c64e784dc8 to your computer and use it in GitHub Desktop.
Code snippet for basic Pagination logic in python
START, END, WINDOW = 0, 15, 5
def pages(page):
start = page - WINDOW/2
end = page + WINDOW/2
if start >= START and end <= END:
return range(start, end+1)
if start <= START:
start = START
end = START + WINDOW
if end >= END:
start = END - WINDOW
end = END
return range(start, end)
for i in range(END):
print i, pages(i)
# Output
"""
0 [0, 1, 2, 3, 4]
1 [0, 1, 2, 3, 4]
2 [0, 1, 2, 3, 4]
3 [1, 2, 3, 4, 5]
4 [2, 3, 4, 5, 6]
5 [3, 4, 5, 6, 7]
6 [4, 5, 6, 7, 8]
7 [5, 6, 7, 8, 9]
8 [6, 7, 8, 9, 10]
9 [7, 8, 9, 10, 11]
10 [8, 9, 10, 11, 12]
11 [9, 10, 11, 12, 13]
12 [10, 11, 12, 13, 14]
13 [11, 12, 13, 14, 15]
14 [10, 11, 12, 13, 14]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment