<s> from math import ceil <EOL> class InvalidPage ( Exception ) : <EOL> pass <EOL> class PageNotAnInteger ( InvalidPage ) : <EOL> pass <EOL> class EmptyPage ( InvalidPage ) : <EOL> pass <EOL> class Paginator ( object ) : <EOL> def __init__ ( self , object_list , per_page , orphans = <NUM_LIT:0> , allow_empty_first_page = True ) : <EOL> self . object_list = object_list <EOL> self . per_page = per_page <EOL> self . orphans = orphans <EOL> self . allow_empty_first_page = allow_empty_first_page <EOL> self . _num_pages = self . _count = None <EOL> def validate_number ( self , number ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> number = int ( number ) <EOL> except ValueError : <EOL> raise PageNotAnInteger ( '<STR_LIT>' ) <EOL> if number < <NUM_LIT:1> : <EOL> raise EmptyPage ( '<STR_LIT>' ) <EOL> if number > self . num_pages : <EOL> if number == <NUM_LIT:1> and self . allow_empty_first_page : <EOL> pass <EOL> else : <EOL> raise EmptyPage ( '<STR_LIT>' ) <EOL> return number <EOL> def page ( self , number ) : <EOL> "<STR_LIT>" <EOL> number = self . validate_number ( number ) <EOL> bottom = ( number - <NUM_LIT:1> ) * self . per_page <EOL> top = bottom + self . per_page <EOL> if top + self . orphans >= self . count : <EOL> top = self . count <EOL> return Page ( self . object_list [ bottom : top ] , number , self ) <EOL> def _get_count ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _count is None : <EOL> try : <EOL> self . _count = self . object_list . count ( ) <EOL> except ( AttributeError , TypeError ) : <EOL> self . _count = len ( self . object_list ) <EOL> return self . _count <EOL> count = property ( _get_count ) <EOL> def _get_num_pages ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _num_pages is None : <EOL> if self . count == <NUM_LIT:0> and not self . allow_empty_first_page : <EOL> self . _num_pages = <NUM_LIT:0> <EOL> else : <EOL> hits = max ( <NUM_LIT:1> , self . count - self . orphans ) <EOL> self . _num_pages = int ( ceil ( hits / float ( self . per_page ) ) ) <EOL> return self . _num_pages <EOL> num_pages = property ( _get_num_pages ) <EOL> def _get_page_range ( self ) : <EOL> """<STR_LIT>""" <EOL> return range ( <NUM_LIT:1> , self . num_pages + <NUM_LIT:1> ) <EOL> page_range = property ( _get_page_range ) <EOL> QuerySetPaginator = Paginator <EOL> class Page ( object ) : <EOL> def __init__ ( self , object_list , number , paginator ) : <EOL> self . object_list = object_list <EOL> self . number = number <EOL> self . paginator = paginator <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . number , self . paginator . num_pages ) <EOL> def has_next ( self ) : <EOL> return self . number < self . paginator . num_pages <EOL> def has_previous ( self ) : <EOL> return self . number > <NUM_LIT:1> <EOL> def has_other_pages ( self ) : <EOL> return self . has_previous ( ) or self . has_next ( ) <EOL> def next_page_number ( self ) : <EOL> return self . number + <NUM_LIT:1> <EOL> def previous_page_number ( self ) : <EOL> return self . number - <NUM_LIT:1> <EOL> def start_index ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . paginator . count == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return ( self . paginator . per_page * ( self . number - <NUM_LIT:1> ) ) + <NUM_LIT:1> <EOL> def end_index ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . number == self . paginator . num_pages : <EOL> return self . paginator . count <EOL> return self . number * self . paginator . per_page </s>
