Skip to content
Newer
Older
100644 67 lines (47 sloc) 1.72 KB
5172d24 @kennethreitz New CaseInsensitiveDict
authored
1 # -*- coding: utf-8 -*-
d3a46c8 @kennethreitz preliminary multidicts
authored
2
5172d24 @kennethreitz New CaseInsensitiveDict
authored
3 """
4 requests.structures
5 ~~~~~~~~~~~~~~~~~~~
d3a46c8 @kennethreitz preliminary multidicts
authored
6
055f7c1 @tvaught Some Doc and doctoring fixes by the Speling Poleece.
tvaught authored
7 Data structures that power Requests.
d3a46c8 @kennethreitz preliminary multidicts
authored
8
5172d24 @kennethreitz New CaseInsensitiveDict
authored
9 """
d3a46c8 @kennethreitz preliminary multidicts
authored
10
ec926a4 @kennethreitz test fix
authored
11
5962c0e @JNRowe Subclass dict for CaseInsensitiveDict.
JNRowe authored
12 class CaseInsensitiveDict(dict):
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
13 """Case-insensitive Dictionary
6e13e73 @kennethreitz Big API update
authored
14
15 For example, ``headers['content-encoding']`` will return the
16 value of a ``'Content-Encoding'`` response header."""
d3a46c8 @kennethreitz preliminary multidicts
authored
17
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
18 @property
19 def lower_keys(self):
20 if not hasattr(self, '_lower_keys') or not self._lower_keys:
b0e2e4f @kennethreitz blah
authored
21 self._lower_keys = dict((k.lower(), k) for k in list(self.keys()))
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
22 return self._lower_keys
d3a46c8 @kennethreitz preliminary multidicts
authored
23
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
24 def _clear_lower_keys(self):
25 if hasattr(self, '_lower_keys'):
26 self._lower_keys.clear()
d3a46c8 @kennethreitz preliminary multidicts
authored
27
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
28 def __setitem__(self, key, value):
29 dict.__setitem__(self, key, value)
30 self._clear_lower_keys()
31
32 def __delitem__(self, key):
4b7c5da @mwielgoszewski Expose case-insensitive key access when deleting a key from CaseInsen…
mwielgoszewski authored
33 dict.__delitem__(self, self.lower_keys.get(key.lower(), key))
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
34 self._lower_keys.clear()
d3a46c8 @kennethreitz preliminary multidicts
authored
35
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
36 def __contains__(self, key):
37 return key.lower() in self.lower_keys
d3a46c8 @kennethreitz preliminary multidicts
authored
38
5172d24 @kennethreitz New CaseInsensitiveDict
authored
39 def __getitem__(self, key):
5962c0e @JNRowe Subclass dict for CaseInsensitiveDict.
JNRowe authored
40 # We allow fall-through here, so values default to None
41 if key in self:
04c456b structures: little bit more general, lower-key-caching version of Cas…
Gulácsi Tamás authored
42 return dict.__getitem__(self, self.lower_keys[key.lower()])
43
44 def get(self, key, default=None):
45 if key in self:
46 return self[key]
47 else:
48 return default
6cead64 @kennethreitz supporting codes class
authored
49
50 class LookupDict(dict):
51 """Dictionary lookup object."""
52
53 def __init__(self, name=None):
54 self.name = name
55 super(LookupDict, self).__init__()
56
57 def __repr__(self):
58 return '<lookup \'%s\'>' % (self.name)
59
60 def __getitem__(self, key):
61 # We allow fall-through here, so values default to None
62
63 return self.__dict__.get(key, None)
64
65 def get(self, key, default=None):
b0e2e4f @kennethreitz blah
authored
66 return self.__dict__.get(key, default)
Something went wrong with that request. Please try again.