<s> from copy import copy <EOL> from itertools import islice <EOL> import collections <EOL> Mapping = collections . Mapping <EOL> def mapping_iterator ( iterable ) : <EOL> if isinstance ( iterable , Mapping ) : <EOL> return iterable . items ( ) <EOL> else : <EOL> return iterable or ( ) <EOL> def inverse_mapping ( iterable ) : <EOL> if isinstance ( iterable , Mapping ) : <EOL> iterable = iterable . items ( ) <EOL> return ( ( value , key ) for key , value in iterable ) <EOL> def isgenerator ( value ) : <EOL> return hasattr ( value , '<STR_LIT>' ) and not hasattr ( value , '<STR_LIT>' ) <EOL> def aslist ( value ) : <EOL> if isinstance ( value , list ) : <EOL> return value <EOL> if isgenerator ( value ) or isinstance ( value , ( tuple , set , frozenset ) ) : <EOL> return list ( value ) <EOL> else : <EOL> return [ value ] <EOL> class MultiValueDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None ) : <EOL> super ( ) . __init__ ( ) <EOL> if data : <EOL> self . update ( data ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> l = super ( ) . __getitem__ ( key ) <EOL> return l [ <NUM_LIT:0> ] if len ( l ) == <NUM_LIT:1> else l <EOL> def __setitem__ ( self , key , value ) : <EOL> if key in self : <EOL> l = super ( ) . __getitem__ ( key ) <EOL> if value not in l : <EOL> l . append ( value ) <EOL> else : <EOL> super ( ) . __setitem__ ( key , [ value ] ) <EOL> def __copy__ ( self ) : <EOL> return self . __class__ ( ( ( k , v [ : ] ) for k , v in self . lists ( ) ) ) <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> return default <EOL> def pop ( self , key , * arg ) : <EOL> if key in self : <EOL> l = super ( ) . pop ( key ) <EOL> return l [ <NUM_LIT:0> ] if len ( l ) == <NUM_LIT:1> else l <EOL> else : <EOL> return super ( ) . pop ( key , * arg ) <EOL> def getlist ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return super ( ) . __getitem__ ( key ) <EOL> def setlist ( self , key , _list ) : <EOL> if key in self : <EOL> self . getlist ( key ) . extend ( _list ) <EOL> else : <EOL> _list = aslist ( _list ) <EOL> super ( ) . __setitem__ ( key , _list ) <EOL> def setdefault ( self , key , default = None ) : <EOL> if key not in self : <EOL> self [ key ] = default <EOL> return self [ key ] <EOL> def extend ( self , key , values ) : <EOL> """<STR_LIT>""" <EOL> for value in values : <EOL> self [ key ] = value <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( ( key , self [ key ] ) for key in self ) <EOL> def lists ( self ) : <EOL> """<STR_LIT>""" <EOL> return super ( ) . items ( ) <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self [ key ] for key in self . keys ( ) ] <EOL> def copy ( self ) : <EOL> return copy ( self ) <EOL> def update ( self , elem ) : <EOL> if isinstance ( elem , Mapping ) : <EOL> elem = elem . items ( ) <EOL> for key , val in elem : <EOL> self . extend ( key , aslist ( val ) ) <EOL> class AttributeDictionary ( collections . Mapping ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , * iterable , ** kwargs ) : <EOL> if iterable : <EOL> if len ( iterable ) > <NUM_LIT:1> : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( self . __class__ . __name__ , len ( iterable ) ) ) <EOL> self . update ( iterable [ <NUM_LIT:0> ] ) <EOL> if kwargs : <EOL> self . update ( kwargs ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . __dict__ ) <EOL> def __str__ ( self ) : <EOL> return str ( self . __dict__ ) <EOL> def __contains__ ( self , name ) : <EOL> return name in self . __dict__ <EOL> def __len__ ( self ) : <EOL> return len ( self . __dict__ ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . __dict__ ) <EOL> def __getattr__ ( self , name ) : <EOL> return self . __dict__ . get ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> self . __dict__ [ name ] = value <EOL> def __setitem__ ( self , name , value ) : <EOL> self . __dict__ [ name ] = value <EOL> def __getitem__ ( self , name ) : <EOL> return self . __dict__ [ name ] <EOL> def __getstate__ ( self ) : <EOL> return self . __dict__ . copy ( ) <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> def update ( self , iterable ) : <EOL> for name , value in mapping_iterator ( iterable ) : <EOL> setattr ( self , name , value ) <EOL> def all ( self ) : <EOL> return self . __dict__ <EOL> def pop ( self , name , default = None ) : <EOL> return self . __dict__ . pop ( name , default ) <EOL> def values ( self ) : <EOL> return self . __dict__ . values ( ) <EOL> def items ( self ) : <EOL> return self . __dict__ . items ( ) <EOL> def copy ( self ) : <EOL> return self . __class__ ( self ) <EOL> class FrozenDict ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , * iterable , ** kwargs ) : <EOL> update = super ( ) . update <EOL> if iterable : <EOL> if len ( iterable ) > <NUM_LIT:1> : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( self . __class__ . __name__ , len ( iterable ) ) ) <EOL> update ( iterable [ <NUM_LIT:0> ] ) <EOL> if kwargs : <EOL> update ( kwargs ) <EOL> def __setitem__ ( self , key , value ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> def update ( self , iterable ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> def pop ( self , key ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> def __gt__ ( self , other ) : <EOL> if hasattr ( other , '<STR_LIT>' ) : <EOL> return len ( self ) > len ( other ) <EOL> else : <EOL> return False <EOL> def __lt__ ( self , other ) : <EOL> if hasattr ( other , '<STR_LIT>' ) : <EOL> return len ( self ) < len ( other ) <EOL> else : <EOL> return False <EOL> class Dict ( dict ) : <EOL> def mget ( self , fields ) : <EOL> return [ self . get ( f ) for f in fields ] <EOL> def flat ( self ) : <EOL> result = [ ] <EOL> [ result . extend ( pair ) for pair in self . items ( ) ] <EOL> return result <EOL> class Deque ( collections . deque ) : <EOL> def insert_before ( self , pivot , value ) : <EOL> l = list ( self ) <EOL> try : <EOL> index = l . index ( pivot ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> l . insert ( index , value ) <EOL> self . clear ( ) <EOL> self . extend ( l ) <EOL> def insert_after ( self , pivot , value ) : <EOL> l = list ( self ) <EOL> try : <EOL> index = l . index ( pivot ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> l . insert ( index + <NUM_LIT:1> , value ) <EOL> self . clear ( ) <EOL> self . extend ( l ) <EOL> def remove ( self , elem , count = <NUM_LIT:1> ) : <EOL> rev = False <EOL> if count : <EOL> if count < <NUM_LIT:0> : <EOL> rev = True <EOL> count = - count <EOL> l = list ( reversed ( self ) ) <EOL> else : <EOL> l = list ( self ) <EOL> while count : <EOL> try : <EOL> l . remove ( elem ) <EOL> count -= <NUM_LIT:1> <EOL> except ValueError : <EOL> break <EOL> else : <EOL> l = [ v for v in self if v != elem ] <EOL> removed = len ( self ) - len ( l ) <EOL> if removed : <EOL> self . clear ( ) <EOL> self . extend ( reversed ( l ) if rev else l ) <EOL> return removed <EOL> def trim ( self , start , end ) : <EOL> slice = list ( islice ( self , start , end ) ) <EOL> self . clear ( ) <EOL> self . extend ( slice ) <EOL> def merge_prefix ( deque , size ) : <EOL> """<STR_LIT>""" <EOL> if len ( deque ) == <NUM_LIT:1> and len ( deque [ <NUM_LIT:0> ] ) <= size : <EOL> return <EOL> prefix = [ ] <EOL> remaining = size <EOL> while deque and remaining > <NUM_LIT:0> : <EOL> chunk = deque . popleft ( ) <EOL> if len ( chunk ) > remaining : <EOL> deque . appendleft ( chunk [ remaining : ] ) <EOL> chunk = chunk [ : remaining ] <EOL> prefix . append ( chunk ) <EOL> remaining -= len ( chunk ) <EOL> if prefix : <EOL> deque . appendleft ( b'<STR_LIT>' . join ( prefix ) ) <EOL> elif not deque : <EOL> deque . appendleft ( b'<STR_LIT>' ) <EOL> def recursive_update ( target , mapping ) : <EOL> for key , value in mapping . items ( ) : <EOL> if value is not None : <EOL> if key in target : <EOL> cont = target [ key ] <EOL> if isinstance ( value , Mapping ) and isinstance ( cont , Mapping ) : <EOL> recursive_update ( cont , value ) <EOL> else : <EOL> target [ key ] = value <EOL> else : <EOL> target [ key ] = value </s>
