]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/requests/structures.py
5 Data structures that power Requests.
8 from collections
import OrderedDict
10 from .compat
import Mapping
, MutableMapping
13 class CaseInsensitiveDict(MutableMapping
):
14 """A case-insensitive ``dict``-like object.
16 Implements all methods and operations of
17 ``MutableMapping`` as well as dict's ``copy``. Also
18 provides ``lower_items``.
20 All keys are expected to be strings. The structure remembers the
21 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 contains
24 testing is case insensitive::
26 cid = CaseInsensitiveDict()
27 cid['Accept'] = 'application/json'
28 cid['aCCEPT'] == 'application/json' # True
29 list(cid) == ['Accept'] # True
31 For example, ``headers['content-encoding']`` will return the
32 value of a ``'Content-Encoding'`` response header, regardless
33 of how the header name was originally stored.
35 If the constructor, ``.update``, or equality comparison
36 operations are given keys that have equal ``.lower()``s, the
37 behavior is undefined.
40 def __init__(self
, data
=None, **kwargs
):
41 self
._store
= OrderedDict()
44 self
.update(data
, **kwargs
)
46 def __setitem__(self
, key
, value
):
47 # Use the lowercased key for lookups, but store the actual
48 # key alongside the value.
49 self
._store
[key
.lower()] = (key
, value
)
51 def __getitem__(self
, key
):
52 return self
._store
[key
.lower()][1]
54 def __delitem__(self
, key
):
55 del self
._store
[key
.lower()]
58 return (casedkey
for casedkey
, mappedvalue
in self
._store
.values())
61 return len(self
._store
)
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())
67 def __eq__(self
, other
):
68 if isinstance(other
, Mapping
):
69 other
= CaseInsensitiveDict(other
)
72 # Compare insensitively
73 return dict(self
.lower_items()) == dict(other
.lower_items())
77 return CaseInsensitiveDict(self
._store
.values())
80 return str(dict(self
.items()))
83 class LookupDict(dict):
84 """Dictionary lookup object."""
86 def __init__(self
, name
=None):
91 return f
"<lookup '{self.name}'>"
93 def __getitem__(self
, key
):
94 # We allow fall-through here, so values default to None
96 return self
.__dict
__.get(key
, None)
98 def get(self
, key
, default
=None):
99 return self
.__dict
__.get(key
, default
)