Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
structures.py100 linesDownload Raw Back to requests
1"""2requests.structures3~~~~~~~~~~~~~~~~~~~4 5Data structures that power Requests.6"""7 8from collections import OrderedDict9 10from .compat import Mapping, MutableMapping11 12 13class CaseInsensitiveDict(MutableMapping):14    """A case-insensitive ``dict``-like object.15 16    Implements all methods and operations of17    ``MutableMapping`` as well as dict's ``copy``. Also18    provides ``lower_items``.19 20    All keys are expected to be strings. The structure remembers the21    case of the last key to be set, and ``iter(instance)``,22    ``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()``23    will contain case-sensitive keys. However, querying and contains24    testing is case insensitive::25 26        cid = CaseInsensitiveDict()27        cid['Accept'] = 'application/json'28        cid['aCCEPT'] == 'application/json'  # True29        list(cid) == ['Accept']  # True30 31    For example, ``headers['content-encoding']`` will return the32    value of a ``'Content-Encoding'`` response header, regardless33    of how the header name was originally stored.34 35    If the constructor, ``.update``, or equality comparison36    operations are given keys that have equal ``.lower()``s, the37    behavior is undefined.38    """39 40    def __init__(self, data=None, **kwargs):41        self._store = OrderedDict()42        if data is None:43            data = {}44        self.update(data, **kwargs)45 46    def __setitem__(self, key, value):47        # Use the lowercased key for lookups, but store the actual48        # key alongside the value.49        self._store[key.lower()] = (key, value)50 51    def __getitem__(self, key):52        return self._store[key.lower()][1]53 54    def __delitem__(self, key):55        del self._store[key.lower()]56 57    def __iter__(self):58        return (casedkey for casedkey, mappedvalue in self._store.values())59 60    def __len__(self):61        return len(self._store)62 63    def lower_items(self):64        """Like iteritems(), but with all lowercase keys."""65        return ((lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items())66 67    def __eq__(self, other):68        if isinstance(other, Mapping):69            other = CaseInsensitiveDict(other)70        else:71            return NotImplemented72        # Compare insensitively73        return dict(self.lower_items()) == dict(other.lower_items())74 75    # Copy is required76    def copy(self):77        return CaseInsensitiveDict(self._store.values())78 79    def __repr__(self):80        return str(dict(self.items()))81 82 83class LookupDict(dict):84    """Dictionary lookup object."""85 86    def __init__(self, name=None):87        self.name = name88        super().__init__()89 90    def __repr__(self):91        return f"<lookup '{self.name}'>"92 93    def __getitem__(self, key):94        # We allow fall-through here, so values default to None95 96        return self.__dict__.get(key, None)97 98    def get(self, key, default=None):99        return self.__dict__.get(key, default)100 
codekingpro/portable-devtools · Team Ai