<s> """<STR_LIT>""" <EOL> class ListMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> _minlength = <NUM_LIT:0> <EOL> _maxlength = None <EOL> _IndexError = IndexError <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_single_internal = self . _get_single_external <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _set_single = self . _set_single_rebuild <EOL> self . _assign_extended_slice = self . _assign_extended_slice_rebuild <EOL> super ( ListMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> def __getitem__ ( self , index ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( index , slice ) : <EOL> return [ self . _get_single_external ( i ) for i in xrange ( * index . indices ( len ( self ) ) ) ] <EOL> else : <EOL> index = self . _checkindex ( index ) <EOL> return self . _get_single_external ( index ) <EOL> def __delitem__ ( self , index ) : <EOL> "<STR_LIT>" <EOL> if not isinstance ( index , ( int , long , slice ) ) : <EOL> raise TypeError ( "<STR_LIT>" % index ) <EOL> origLen = len ( self ) <EOL> if isinstance ( index , ( int , long ) ) : <EOL> index = self . _checkindex ( index ) <EOL> indexRange = [ index ] <EOL> else : <EOL> indexRange = range ( * index . indices ( origLen ) ) <EOL> newLen = origLen - len ( indexRange ) <EOL> newItems = ( self . _get_single_internal ( i ) <EOL> for i in xrange ( origLen ) <EOL> if i not in indexRange ) <EOL> self . _rebuild ( newLen , newItems ) <EOL> def __setitem__ ( self , index , val ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( index , slice ) : <EOL> self . _set_slice ( index , val ) <EOL> else : <EOL> index = self . _checkindex ( index ) <EOL> self . _check_allowed ( ( val , ) ) <EOL> self . _set_single ( index , val ) <EOL> def __iter__ ( self ) : <EOL> "<STR_LIT>" <EOL> for i in xrange ( len ( self ) ) : <EOL> yield self [ i ] <EOL> def __add__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> return self . __class__ ( list ( self ) + list ( other ) ) <EOL> def __radd__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> return other . __class__ ( list ( other ) + list ( self ) ) <EOL> def __iadd__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> self . extend ( list ( other ) ) <EOL> return self <EOL> def __mul__ ( self , n ) : <EOL> '<STR_LIT>' <EOL> return self . __class__ ( list ( self ) * n ) <EOL> def __rmul__ ( self , n ) : <EOL> '<STR_LIT>' <EOL> return self . __class__ ( list ( self ) * n ) <EOL> def __imul__ ( self , n ) : <EOL> '<STR_LIT>' <EOL> if n <= <NUM_LIT:0> : <EOL> del self [ : ] <EOL> else : <EOL> cache = list ( self ) <EOL> for i in range ( n - <NUM_LIT:1> ) : <EOL> self . extend ( cache ) <EOL> return self <EOL> def __cmp__ ( self , other ) : <EOL> '<STR_LIT>' <EOL> slen = len ( self ) <EOL> for i in range ( slen ) : <EOL> try : <EOL> c = cmp ( self [ i ] , other [ i ] ) <EOL> except IndexError : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> if c : return c <EOL> return cmp ( slen , len ( other ) ) <EOL> def count ( self , val ) : <EOL> "<STR_LIT>" <EOL> count = <NUM_LIT:0> <EOL> for i in self : <EOL> if val == i : count += <NUM_LIT:1> <EOL> return count <EOL> def index ( self , val ) : <EOL> "<STR_LIT>" <EOL> for i in xrange ( <NUM_LIT:0> , len ( self ) ) : <EOL> if self [ i ] == val : return i <EOL> raise ValueError ( '<STR_LIT>' % str ( val ) ) <EOL> def append ( self , val ) : <EOL> "<STR_LIT>" <EOL> self [ len ( self ) : ] = [ val ] <EOL> def extend ( self , vals ) : <EOL> "<STR_LIT>" <EOL> self [ len ( self ) : ] = vals <EOL> def insert ( self , index , val ) : <EOL> "<STR_LIT>" <EOL> if not isinstance ( index , ( int , long ) ) : <EOL> raise TypeError ( "<STR_LIT>" % index ) <EOL> self [ index : index ] = [ val ] <EOL> def pop ( self , index = - <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> result = self [ index ] <EOL> del self [ index ] <EOL> return result <EOL> def remove ( self , val ) : <EOL> "<STR_LIT>" <EOL> del self [ self . index ( val ) ] <EOL> def reverse ( self ) : <EOL> "<STR_LIT>" <EOL> self [ : ] = self [ - <NUM_LIT:1> : : - <NUM_LIT:1> ] <EOL> def sort ( self , cmp = cmp , key = None , reverse = False ) : <EOL> "<STR_LIT>" <EOL> if key : <EOL> temp = [ ( key ( v ) , v ) for v in self ] <EOL> temp . sort ( cmp = cmp , key = lambda x : x [ <NUM_LIT:0> ] , reverse = reverse ) <EOL> self [ : ] = [ v [ <NUM_LIT:1> ] for v in temp ] <EOL> else : <EOL> temp = list ( self ) <EOL> temp . sort ( cmp = cmp , reverse = reverse ) <EOL> self [ : ] = temp <EOL> def _rebuild ( self , newLen , newItems ) : <EOL> if newLen < self . _minlength : <EOL> raise ValueError ( '<STR_LIT>' % self . _minlength ) <EOL> if self . _maxlength is not None and newLen > self . _maxlength : <EOL> raise ValueError ( '<STR_LIT>' % self . _maxlength ) <EOL> self . _set_list ( newLen , newItems ) <EOL> def _set_single_rebuild ( self , index , value ) : <EOL> self . _set_slice ( slice ( index , index + <NUM_LIT:1> , <NUM_LIT:1> ) , [ value ] ) <EOL> def _checkindex ( self , index , correct = True ) : <EOL> length = len ( self ) <EOL> if <NUM_LIT:0> <= index < length : <EOL> return index <EOL> if correct and - length <= index < <NUM_LIT:0> : <EOL> return index + length <EOL> raise self . _IndexError ( '<STR_LIT>' % str ( index ) ) <EOL> def _check_allowed ( self , items ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if False in [ isinstance ( val , self . _allowed ) for val in items ] : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def _set_slice ( self , index , values ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> iter ( values ) <EOL> except TypeError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . _check_allowed ( values ) <EOL> origLen = len ( self ) <EOL> valueList = list ( values ) <EOL> start , stop , step = index . indices ( origLen ) <EOL> if index . step is None : <EOL> self . _assign_simple_slice ( start , stop , valueList ) <EOL> else : <EOL> self . _assign_extended_slice ( start , stop , step , valueList ) <EOL> def _assign_extended_slice_rebuild ( self , start , stop , step , valueList ) : <EOL> '<STR_LIT>' <EOL> indexList = range ( start , stop , step ) <EOL> if len ( valueList ) != len ( indexList ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( len ( valueList ) , len ( indexList ) ) ) <EOL> newLen = len ( self ) <EOL> newVals = dict ( zip ( indexList , valueList ) ) <EOL> def newItems ( ) : <EOL> for i in xrange ( newLen ) : <EOL> if i in newVals : <EOL> yield newVals [ i ] <EOL> else : <EOL> yield self . _get_single_internal ( i ) <EOL> self . _rebuild ( newLen , newItems ( ) ) <EOL> def _assign_extended_slice ( self , start , stop , step , valueList ) : <EOL> '<STR_LIT>' <EOL> indexList = range ( start , stop , step ) <EOL> if len ( valueList ) != len ( indexList ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( len ( valueList ) , len ( indexList ) ) ) <EOL> for i , val in zip ( indexList , valueList ) : <EOL> self . _set_single ( i , val ) <EOL> def _assign_simple_slice ( self , start , stop , valueList ) : <EOL> '<STR_LIT>' <EOL> origLen = len ( self ) <EOL> stop = max ( start , stop ) <EOL> newLen = origLen - stop + start + len ( valueList ) <EOL> def newItems ( ) : <EOL> for i in xrange ( origLen + <NUM_LIT:1> ) : <EOL> if i == start : <EOL> for val in valueList : <EOL> yield val <EOL> if i < origLen : <EOL> if i < start or i >= stop : <EOL> yield self . _get_single_internal ( i ) <EOL> self . _rebuild ( newLen , newItems ( ) ) </s>
