Upload cid.py
Browse files
cid.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class CaseInsensitiveDict(dict):
|
2 |
+
def __init__(self, *args, **kwargs):
|
3 |
+
self._keystore = {}
|
4 |
+
d = dict(*args, **kwargs)
|
5 |
+
for k in list(d.keys()):
|
6 |
+
self._keystore[self._get_lower(k)] = k
|
7 |
+
return super(CaseInsensitiveDict,self).__init__(*args,**kwargs)
|
8 |
+
|
9 |
+
def __setitem__(self, k, v):
|
10 |
+
self._keystore[self._get_lower(k)] = k
|
11 |
+
return super(CaseInsensitiveDict, self).__setitem__(k, v)
|
12 |
+
|
13 |
+
def __getitem__(self, k):
|
14 |
+
return super(CaseInsensitiveDict,
|
15 |
+
self).__getitem__(self._keystore[self._get_lower(k)])
|
16 |
+
@staticmethod
|
17 |
+
def _get_lower(k):
|
18 |
+
if isinstance(k,str):
|
19 |
+
return k.lower()
|
20 |
+
else:
|
21 |
+
return k
|