<s> """<STR_LIT>""" <EOL> from collections import namedtuple , Sequence <EOL> CONTINUATION = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:c>' ] ) <EOL> class IndexPage ( Sequence , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , bucket , index , startkey , endkey , return_terms , <EOL> max_results , term_regex ) : <EOL> self . client = client <EOL> self . bucket = bucket <EOL> self . index = index <EOL> self . startkey = startkey <EOL> self . endkey = endkey <EOL> self . return_terms = return_terms <EOL> self . max_results = max_results <EOL> self . results = None <EOL> self . stream = False <EOL> self . term_regex = term_regex <EOL> continuation = None <EOL> """<STR_LIT>""" <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . results is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> for result in self . results : <EOL> if self . stream and isinstance ( result , CONTINUATION ) : <EOL> self . continuation = result . c <EOL> else : <EOL> yield self . _inject_term ( result ) <EOL> finally : <EOL> if self . stream : <EOL> self . results . close ( ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_results ( ) : <EOL> return len ( self . results ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_results ( ) : <EOL> return self . results [ index ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , list ) and self . _has_results ( ) : <EOL> return self . _inject_term ( self . results ) == other <EOL> elif isinstance ( other , IndexPage ) : <EOL> return other . __dict__ == self . __dict__ <EOL> else : <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return not self . __eq__ ( other ) <EOL> def has_next_page ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . continuation is not None <EOL> def next_page ( self , timeout = None , stream = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . continuation : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if stream is not None : <EOL> self . stream = stream <EOL> args = { '<STR_LIT>' : self . bucket , <EOL> '<STR_LIT:index>' : self . index , <EOL> '<STR_LIT>' : self . startkey , <EOL> '<STR_LIT>' : self . endkey , <EOL> '<STR_LIT>' : self . return_terms , <EOL> '<STR_LIT>' : self . max_results , <EOL> '<STR_LIT>' : self . continuation , <EOL> '<STR_LIT>' : timeout , <EOL> '<STR_LIT>' : self . term_regex } <EOL> if self . stream : <EOL> return self . client . stream_index ( ** args ) <EOL> else : <EOL> return self . client . get_index ( ** args ) <EOL> def _has_results ( self ) : <EOL> """<STR_LIT>""" <EOL> return not ( self . stream or self . results is None ) <EOL> def _should_inject_term ( self , term ) : <EOL> """<STR_LIT>""" <EOL> return self . return_terms and not self . endkey <EOL> def _inject_term ( self , result ) : <EOL> """<STR_LIT>""" <EOL> if self . _should_inject_term ( result ) : <EOL> if type ( result ) is list : <EOL> return [ ( self . startkey , r ) for r in result ] <EOL> else : <EOL> return ( self . startkey , result ) <EOL> else : <EOL> return result <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . __class__ . __name__ , self . __dict__ ) <EOL> def close ( self ) : <EOL> if self . stream : <EOL> self . results . close ( ) </s>
