<s> from django . utils import six <EOL> from django . utils . encoding import smart_str <EOL> if six . PY3 : <EOL> from urllib . parse import urlparse , parse_qsl , urlunparse , urlencode <EOL> else : <EOL> from urlparse import urlparse , parse_qsl , urlunparse <EOL> from urllib import urlencode <EOL> class QueryString ( object ) : <EOL> def __init__ ( self , request = None ) : <EOL> self . params = { } <EOL> if request : <EOL> for key in request . GET . keys ( ) : <EOL> self [ key ] = request . GET . getlist ( key ) <EOL> def __contains__ ( self , param ) : <EOL> if isinstance ( param , tuple ) : <EOL> return param [ <NUM_LIT:1> ] in self . params . get ( param [ <NUM_LIT:0> ] , set ( ) ) <EOL> else : <EOL> return param in self . params <EOL> def add_param ( self , key , value ) : <EOL> self [ key ] = list ( self . get_param ( key , [ ] ) ) + [ value ] <EOL> def remove_param ( self , key , value = None ) : <EOL> if key not in self . params : <EOL> raise ValueError ( "<STR_LIT>" . format ( key ) ) <EOL> if value is not None : <EOL> if value not in self . params [ key ] : <EOL> raise ValueError ( "<STR_LIT>" . format ( key , value ) ) <EOL> self . params [ key ] . remove ( value ) <EOL> else : <EOL> del self . params [ key ] <EOL> def set_param ( self , key , value ) : <EOL> self [ key ] = value <EOL> def get_param ( self , param , default = None ) : <EOL> if param in self : <EOL> return self [ param ] <EOL> return default <EOL> def __getitem__ ( self , key ) : <EOL> return frozenset ( self . params [ key ] ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if not isinstance ( value , ( tuple , list , set ) ) : <EOL> value = [ value ] <EOL> self . params [ key ] = set ( [ smart_str ( el ) for el in value if el ] ) <EOL> def __iter__ ( self ) : <EOL> for key in self . params . keys ( ) : <EOL> yield key , self [ key ] <EOL> def __repr__ ( self ) : <EOL> params = [ ] <EOL> for key , values in self : <EOL> for value in values : <EOL> value = smart_str ( value ) <EOL> params . append ( ( key , value ) ) <EOL> return urlencode ( params ) <EOL> def __le__ ( self , other ) : <EOL> for key in self . params . keys ( ) : <EOL> if key not in other . params : <EOL> return False <EOL> if not self . params [ key ] <= other . params [ key ] : <EOL> return False <EOL> return True <EOL> def __ge__ ( self , other ) : <EOL> for key in other . params . keys ( ) : <EOL> if key not in self . params : <EOL> return False <EOL> if not self . params [ key ] >= other . params [ key ] : <EOL> return False <EOL> return True <EOL> def __eq__ ( self , other ) : <EOL> if set ( self . params . keys ( ) ) != set ( other . params . keys ( ) ) : <EOL> return False <EOL> for key in self . params . keys ( ) : <EOL> if not self . params [ key ] == other . params [ key ] : <EOL> return False <EOL> return True <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . params ) <EOL> def clone ( self ) : <EOL> clone = QueryString ( ) <EOL> clone . params = { key : set ( value ) for key , value in self } <EOL> return clone </s>
