<s> try : <EOL> from collections . abc import Mapping <EOL> except ImportError : <EOL> from collections import Mapping <EOL> class KeyValueList ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterable = ( ) , ** kwargs ) : <EOL> list . __init__ ( self ) <EOL> self . extend ( iterable ) <EOL> self . extend ( kwargs ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . __class__ . __name__ , list . __repr__ ( self ) ) <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> got = list . __getitem__ ( self , index ) <EOL> except TypeError : <EOL> try : <EOL> return next ( self . get ( index ) ) <EOL> except StopIteration : <EOL> return None <EOL> else : <EOL> if isinstance ( index , slice ) : <EOL> return KeyValueList ( got ) <EOL> else : <EOL> return got <EOL> def __getslice__ ( self , start , end ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return KeyValueList ( list . __getslice__ ( self , start , end ) ) <EOL> except AttributeError : <EOL> return KeyValueList ( list . __getitem__ ( self , slice ( start , end ) ) ) <EOL> def __setitem__ ( self , index , item ) : <EOL> """<STR_LIT>""" <EOL> list . __setitem__ ( self , index , item ) <EOL> def __setslice__ ( self , start , stop , items ) : <EOL> """<STR_LIT>""" <EOL> list . __setslice__ ( self , start , stop , items ) <EOL> def __delitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> list . __delitem__ ( self , index ) <EOL> def __delslice__ ( self , start , end ) : <EOL> """<STR_LIT>""" <EOL> list . __delslice__ ( self , start , end ) <EOL> def __contains__ ( self , item ) : <EOL> """<STR_LIT>""" <EOL> key , value = item <EOL> return self . has_item ( key , value ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return list . __iter__ ( self ) <EOL> def append ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> list . append ( self , ( key , value ) ) <EOL> def extend ( self , iterable ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( iterable , Mapping ) : <EOL> list . extend ( self , iterable . items ( ) ) <EOL> else : <EOL> try : <EOL> list . extend ( self , ( ( k , v ) for k , v in iterable ) ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def insert ( self , index , key , value ) : <EOL> """<STR_LIT>""" <EOL> list . insert ( self , index , ( key , value ) ) <EOL> def has_item ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> for k , v in self : <EOL> if k == key and v == value : <EOL> return True <EOL> return False <EOL> def has_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> for k , v in self : <EOL> if k == key : <EOL> return True <EOL> return False <EOL> def has_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> for k , v in self : <EOL> if v == value : <EOL> return True <EOL> return False <EOL> def get ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return ( v for k , v in self if k == key ) <EOL> def put ( self , key , * values ) : <EOL> """<STR_LIT>""" <EOL> new_values = list ( values ) <EOL> self [ : ] = [ <EOL> ( k , value ) if k != key else ( k , new_values . pop ( <NUM_LIT:0> ) ) <EOL> for k , value in self <EOL> if k != key or new_values <EOL> ] <EOL> self . extend ( [ ( key , value ) for value in new_values ] ) <EOL> def remove ( self , key ) : <EOL> """<STR_LIT>""" <EOL> length = len ( self ) <EOL> self [ : ] = ( ( k , v ) for k , v in self if k != key ) <EOL> if len ( self ) == length : <EOL> raise ValueError ( "<STR_LIT>" . format ( repr ( key ) ) ) <EOL> def pop ( self , index = None ) : <EOL> """<STR_LIT>""" <EOL> if index is None : <EOL> return list . pop ( self ) <EOL> else : <EOL> return list . pop ( self , index ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> del self [ : ] <EOL> def sort ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> list . sort ( self , * args , ** kwargs ) <EOL> def reverse ( self ) : <EOL> """<STR_LIT>""" <EOL> list . reverse ( self ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return KeyValueList ( self ) <EOL> def iterkeys ( self , collect = False ) : <EOL> """<STR_LIT>""" <EOL> if collect : <EOL> keys = [ ] <EOL> for k , v in self : <EOL> if k not in keys : <EOL> keys . append ( k ) <EOL> yield k <EOL> else : <EOL> for k , v in self : <EOL> yield k <EOL> def itervalues ( self , collect = False ) : <EOL> """<STR_LIT>""" <EOL> if collect : <EOL> keys , values = [ ] , [ ] <EOL> for k , v in self : <EOL> try : <EOL> index = keys . index ( k ) <EOL> except ValueError : <EOL> keys . append ( k ) <EOL> values . append ( [ v ] ) <EOL> else : <EOL> values [ index ] . append ( ( v ) ) <EOL> for value in values : <EOL> yield value <EOL> else : <EOL> for k , v in self : <EOL> yield v <EOL> def iteritems ( self , collect = False ) : <EOL> """<STR_LIT>""" <EOL> if collect : <EOL> keys , items = [ ] , [ ] <EOL> for k , v in self : <EOL> try : <EOL> index = keys . index ( k ) <EOL> except ValueError : <EOL> keys . append ( k ) <EOL> items . append ( ( k , [ v ] ) ) <EOL> else : <EOL> items [ index ] [ <NUM_LIT:1> ] . append ( ( v ) ) <EOL> for item in items : <EOL> yield item <EOL> else : <EOL> for item in self : <EOL> yield item </s>
