<s> from six import iteritems , u <EOL> from . utils import AttrDict , AttrList <EOL> class SuggestResponse ( AttrDict ) : <EOL> def success ( self ) : <EOL> return not self . _shards . failed <EOL> class Response ( AttrDict ) : <EOL> def __init__ ( self , response , callbacks = None ) : <EOL> super ( AttrDict , self ) . __setattr__ ( '<STR_LIT>' , callbacks or { } ) <EOL> super ( Response , self ) . __init__ ( response ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . hits ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . hits [ key ] <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . hits ) <EOL> __bool__ = __nonzero__ <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . hits <EOL> def __len__ ( self ) : <EOL> return len ( self . hits ) <EOL> def success ( self ) : <EOL> return self . _shards . total == self . _shards . successful and not self . timed_out <EOL> def _get_result ( self , hit ) : <EOL> dt = hit [ '<STR_LIT>' ] <EOL> for t in hit . get ( '<STR_LIT>' , ( ) ) : <EOL> hit [ '<STR_LIT>' ] [ t ] = self . __class__ ( hit [ '<STR_LIT>' ] [ t ] , self . _callbacks ) <EOL> return self . _callbacks . get ( dt , Result ) ( hit ) <EOL> @ property <EOL> def hits ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> h = self . _d_ [ '<STR_LIT>' ] <EOL> try : <EOL> hits = AttrList ( map ( self . _get_result , h [ '<STR_LIT>' ] ) ) <EOL> except AttributeError as e : <EOL> raise TypeError ( "<STR_LIT>" , e ) <EOL> super ( AttrDict , self ) . __setattr__ ( '<STR_LIT>' , hits ) <EOL> for k in h : <EOL> setattr ( self . _hits , k , h [ k ] ) <EOL> return self . _hits <EOL> class ResultMeta ( AttrDict ) : <EOL> def __init__ ( self , document , exclude = ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> d = dict ( ( k [ <NUM_LIT:1> : ] if k . startswith ( '<STR_LIT:_>' ) else k , v ) for ( k , v ) in iteritems ( document ) if k not in exclude ) <EOL> if '<STR_LIT:type>' in d : <EOL> d [ '<STR_LIT>' ] = d . pop ( '<STR_LIT:type>' ) <EOL> super ( ResultMeta , self ) . __init__ ( d ) <EOL> class Result ( AttrDict ) : <EOL> def __init__ ( self , document ) : <EOL> data = { } <EOL> if '<STR_LIT>' in document : <EOL> data = document [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in document : <EOL> data . update ( document [ '<STR_LIT>' ] ) <EOL> super ( Result , self ) . __init__ ( data ) <EOL> super ( AttrDict , self ) . __setattr__ ( '<STR_LIT>' , ResultMeta ( document ) ) <EOL> def __dir__ ( self ) : <EOL> return super ( Result , self ) . __dir__ ( ) + [ '<STR_LIT>' ] <EOL> def __repr__ ( self ) : <EOL> return u ( '<STR_LIT>' ) % ( <EOL> self . meta . index , self . meta . doc_type , self . meta . id , super ( Result , self ) . __repr__ ( ) ) </s>
