[PYTHON] Page processing class

A class for page processing such as "Goooooooooogle". If you pass the total number of items, the number of items per page, and the number of pages to be displayed at one time in the constructor, and pass the page currently displayed by the content method, the minimum number of pages is returned in min and the maximum number of pages is returned in max. come. I want to write more elegantly.

#coding:utf-8
'''
Created on 2012/09/23

@author: fumix
'''

class Pager():
    '''
Page processing class
    '''


    def __init__(self,total,page,width):
        '''
        Constructor
        '''
        self.total = total
        self.page = page
        self.width = width
    
    def content(self,current):
        #Maximum number of pages
        max_pagecount = self.total / self.page + 1

        #Maximum number of pages<When the number of displayed pages
        if max_pagecount < self.width:
            min = 1
            max = max_pagecount

        #Maximum number of pages<Current page+When half the number of displayed pages
        elif max_pagecount < current + self.width / 2 :
            min = current - self.width / 2
            max = max_pagecount

        #Current page-Half the number of displayed pages<When 1
        elif current - self.width / 2 < 1:
            min = 1
            max = self.width
        
        #other than that
        else:
            min = current - self.width / 2
            max = current + self.width / 2
        return {'min':min,'max':max}
def _test():
    page = Pager(532, 20, 10)
    print page.content(22)

if __name__ == '__main__':
    _test()

Recommended Posts

Page processing class
class
class
Arithmetic processing with Chinese numeral class
Page fragments
Class variables
Class variables
Class inheritance
Abstract class
Summary of pickle and unpickle processing of user-defined class