day15 Python 分页算法

""" --> 页码分页算法
每页10个内容 page_num = 10
第1页 第2页 第3页
[0-10] [10-20] [20-30]

每页5个 page_num = 5
第1页 第2页 第3页
[0-5] [5-10] [10-15]

每页n个, page_num = n
第x页
[(n-1)*n : x*n]
"""


class Page(object):

def __init__(self, lst, page_num):
self.lst = lst
self.page_num = page_num # page_num 是每个显示的元素个数

@property
def total_page_num(self):
"""
计算最大的页码
:return: 最大页码
"""
if len(self.lst) % self.page_num == 0:
return len(self.lst) // self.page_num
else:
return len(self.lst) // self.page_num + 1

def start(self):
"""
:return: 返回起始页
"""
return self.lst[0:self.page_num]

def index(self, index):
"""
:param index: 传递的页码
:return: 返回对应页码的内容
"""
if isinstance(index, int) and index > 0:
return self.lst[(index - 1) * self.page_num: index * self.page_num]
else:
return []

def end(self):
"""
:return: 返回尾页
"""
return self.lst[(self.total_page_num - 1) * self.page_num: self.total_page_num * self.page_num]


lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]

page = Page(lst, 5)
print("start -->", page.start())
print("index -->", page.index(4))
print("end -->", page.end())

 

  

猜你喜欢

转载自www.cnblogs.com/fanghongbo/p/9931470.html
今日推荐