requests structures data structures that power requests a caseinsensitive dictlike object implements all methods and operations of mutablemapping as well as dict s copy also provides loweritems all keys are expected to be strings the structure remembers the case of the last key to be set and iterinstance keys items iterkeys and iteritems will contain casesensitive keys however querying and contains testing is case insensitive cid caseinsensitivedict cid accept applicationjson cid accept applicationjson true listcid accept true for example headers contentencoding will return the value of a contentencoding response header regardless of how the header name was originally stored if the constructor update or equality comparison operations are given keys that have equal lowers the behavior is undefined use the lowercased key for lookups but store the actual key alongside the value like iteritems but with all lowercase keys return lowerkey keyval1 for lowerkey keyval in self store items def eqself other if isinstanceother mapping other caseinsensitivedictother else return notimplemented compare insensitively return dictself loweritems dictother loweritems copy is required def copyself return caseinsensitivedictself store values def reprself return strdictself items class lookupdictdict we allow fallthrough here so values default to none a case insensitive dict like object implements all methods and operations of mutablemapping as well as dict s copy also provides lower_items all keys are expected to be strings the structure remembers the case of the last key to be set and iter instance keys items iterkeys and iteritems will contain case sensitive keys however querying and contains testing is case insensitive cid caseinsensitivedict cid accept application json cid accept application json true list cid accept true for example headers content encoding will return the value of a content encoding response header regardless of how the header name was originally stored if the constructor update or equality comparison operations are given keys that have equal lower s the behavior is undefined use the lowercased key for lookups but store the actual key alongside the value like iteritems but with all lowercase keys compare insensitively copy is required dictionary lookup object we allow fall through here so values default to none
from collections import OrderedDict

from .compat import Mapping, MutableMapping


class CaseInsensitiveDict(MutableMapping):
    

    def __init__(self, data=None, **kwargs):
        self._store = OrderedDict()
        if data is None:
            data = {}
        self.update(data, **kwargs)

    def __setitem__(self, key, value):
        
        
        self._store[key.lower()] = (key, value)

    def __getitem__(self, key):
        return self._store[key.lower()][1]

    def __delitem__(self, key):
        del self._store[key.lower()]

    def __iter__(self):
        return (casedkey for casedkey, mappedvalue in self._store.values())

    def __len__(self):
        return len(self._store)

    def lower_items(self):
        
        return ((lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items())

    def __eq__(self, other):
        if isinstance(other, Mapping):
            other = CaseInsensitiveDict(other)
        else:
            return NotImplemented
        
        return dict(self.lower_items()) == dict(other.lower_items())

    
    def copy(self):
        return CaseInsensitiveDict(self._store.values())

    def __repr__(self):
        return str(dict(self.items()))


class LookupDict(dict):
    

    def __init__(self, name=None):
        self.name = name
        super().__init__()

    def __repr__(self):
        return f"<lookup '{self.name}'>"

    def __getitem__(self, key):
        

        return self.__dict__.get(key, None)

    def get(self, key, default=None):
        return self.__dict__.get(key, default)