Created
December 2, 2010 22:05
-
-
Save proudlygeek/726170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split_pages(currentpage, totalpages): | |
""" | |
Splits a certain number of pages into several | |
lists; for example, if the current page is 31 on a total of 115 | |
pages the return tuple is: | |
([1, 2, 3, 4], [27, 28, 29, 30, 31, 32, 33, 34], [112, 113, 114, 115]) | |
""" | |
# Check if currentpage is less than the total | |
if currentpage > totalpages: | |
raise NameError('The actual page value {0} is more than {1}.'.format(currentpage, totalpages)) | |
# Creates an ordered list of *totalpages* dimension | |
pag = [number+1 for number in range(totalpages)] | |
# If the total size is less than 18 then there's no need to split | |
if len(pag) < 18: | |
return pag | |
else: | |
if (currentpage in pag[:10]): | |
return (pag[:currentpage+3], pag[len(pag)-4:]) | |
elif (currentpage in pag[len(pag)-9:]): | |
return pag[:4], pag[currentpage-5:] | |
else: | |
return pag[:4],pag[currentpage-5:currentpage+3], pag[len(pag)-4:] | |
def unpack_pages(pages): | |
""" | |
Turns a tuple of several lists obtained from the split_pages function | |
(see above) into a plain list; | |
for example, the following tuple: | |
([1, 2, 3, 4], [27, 28, 29, 30, 31, 32, 33, 34], [112, 113, 114, 115]) | |
returns this list: | |
[1, 2, 3, 4, '...', 27, 28, 29, 30, 31, 32, 33, 34, '...', 112, 113, 114, 115] | |
""" | |
# Duck Typing: If it is a plain tuple there's no need to unpack | |
try: | |
pages[0][0] | |
except TypeError: | |
return pages | |
plainlist = [] | |
for page in pages: | |
for item in page: | |
plainlist.append(item) | |
if item != pages[-1][-1]: plainlist.append("...") | |
return plainlist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment