|
6 | 6 | See https://www.python-ldap.org/ for details.
|
7 | 7 | """
|
8 | 8 |
|
9 |
| -from ldap import __version__ |
10 |
| - |
11 | 9 | from UserDict import IterableUserDict
|
12 | 10 |
|
| 11 | +from ldap import __version__ |
| 12 | + |
13 | 13 |
|
14 | 14 | class cidict(IterableUserDict):
|
15 |
| - """ |
16 |
| - Case-insensitive but case-respecting dictionary. |
17 |
| - """ |
18 |
| - |
19 |
| - def __init__(self,default=None): |
20 |
| - self._keys = {} |
21 |
| - IterableUserDict.__init__(self,{}) |
22 |
| - self.update(default or {}) |
23 |
| - |
24 |
| - def __getitem__(self,key): |
25 |
| - return self.data[key.lower()] |
26 |
| - |
27 |
| - def __setitem__(self,key,value): |
28 |
| - lower_key = key.lower() |
29 |
| - self._keys[lower_key] = key |
30 |
| - self.data[lower_key] = value |
31 |
| - |
32 |
| - def __delitem__(self,key): |
33 |
| - lower_key = key.lower() |
34 |
| - del self._keys[lower_key] |
35 |
| - del self.data[lower_key] |
36 |
| - |
37 |
| - def update(self,dict): |
38 |
| - for key in dict.keys(): |
39 |
| - self[key] = dict[key] |
40 |
| - |
41 |
| - def has_key(self,key): |
42 |
| - return key in self |
43 |
| - |
44 |
| - def __contains__(self,key): |
45 |
| - return IterableUserDict.__contains__(self, key.lower()) |
46 |
| - |
47 |
| - def get(self,key,failobj=None): |
48 |
| - try: |
49 |
| - return self[key] |
50 |
| - except KeyError: |
51 |
| - return failobj |
52 |
| - |
53 |
| - def keys(self): |
54 |
| - return self._keys.values() |
55 |
| - |
56 |
| - def items(self): |
57 |
| - result = [] |
58 |
| - for k in self._keys.values(): |
59 |
| - result.append((k,self[k])) |
| 15 | + """ |
| 16 | + Case-insensitive but case-respecting dictionary. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, default=None): |
| 20 | + self._keys = {} |
| 21 | + IterableUserDict.__init__(self, {}) |
| 22 | + self.update(default or {}) |
| 23 | + |
| 24 | + def __getitem__(self, key): |
| 25 | + return self.data[key.lower()] |
| 26 | + |
| 27 | + def __setitem__(self, key, value): |
| 28 | + lower_key = key.lower() |
| 29 | + self._keys[lower_key] = key |
| 30 | + self.data[lower_key] = value |
| 31 | + |
| 32 | + def __delitem__(self, key): |
| 33 | + lower_key = key.lower() |
| 34 | + del self._keys[lower_key] |
| 35 | + del self.data[lower_key] |
| 36 | + |
| 37 | + def update(self, data): |
| 38 | + for key in data.keys(): |
| 39 | + self[key] = data[key] |
| 40 | + |
| 41 | + def has_key(self, key): |
| 42 | + return key in self |
| 43 | + |
| 44 | + def __contains__(self, key): |
| 45 | + return IterableUserDict.__contains__(self, key.lower()) |
| 46 | + |
| 47 | + def get(self, key, failobj=None): |
| 48 | + try: |
| 49 | + return self[key] |
| 50 | + except KeyError: |
| 51 | + return failobj |
| 52 | + |
| 53 | + def keys(self): |
| 54 | + return self._keys.values() |
| 55 | + |
| 56 | + def items(self): |
| 57 | + result = [] |
| 58 | + for k in self._keys.values(): |
| 59 | + result.append((k, self[k])) |
| 60 | + return result |
| 61 | + |
| 62 | + |
| 63 | +def strlist_minus(a, b): |
| 64 | + """ |
| 65 | + Return list of all items in a which are not in b (a - b). |
| 66 | + a,b are supposed to be lists of case-insensitive strings. |
| 67 | + """ |
| 68 | + temp = cidict() |
| 69 | + for elt in b: |
| 70 | + temp[elt] = elt |
| 71 | + result = [ |
| 72 | + elt |
| 73 | + for elt in a |
| 74 | + if elt not in temp |
| 75 | + ] |
| 76 | + return result |
| 77 | + |
| 78 | + |
| 79 | +def strlist_intersection(a, b): |
| 80 | + """ |
| 81 | + Return intersection of two lists of case-insensitive strings a,b. |
| 82 | + """ |
| 83 | + temp = cidict() |
| 84 | + for elt in a: |
| 85 | + temp[elt] = elt |
| 86 | + result = [ |
| 87 | + temp[elt] |
| 88 | + for elt in b |
| 89 | + if elt in temp |
| 90 | + ] |
60 | 91 | return result
|
61 | 92 |
|
62 | 93 |
|
63 |
| -def strlist_minus(a,b): |
64 |
| - """ |
65 |
| - Return list of all items in a which are not in b (a - b). |
66 |
| - a,b are supposed to be lists of case-insensitive strings. |
67 |
| - """ |
68 |
| - temp = cidict() |
69 |
| - for elt in b: |
70 |
| - temp[elt] = elt |
71 |
| - result = [ |
72 |
| - elt |
73 |
| - for elt in a |
74 |
| - if elt not in temp |
75 |
| - ] |
76 |
| - return result |
77 |
| - |
78 |
| - |
79 |
| -def strlist_intersection(a,b): |
80 |
| - """ |
81 |
| - Return intersection of two lists of case-insensitive strings a,b. |
82 |
| - """ |
83 |
| - temp = cidict() |
84 |
| - for elt in a: |
85 |
| - temp[elt] = elt |
86 |
| - result = [ |
87 |
| - temp[elt] |
88 |
| - for elt in b |
89 |
| - if elt in temp |
90 |
| - ] |
91 |
| - return result |
92 |
| - |
93 |
| - |
94 |
| -def strlist_union(a,b): |
95 |
| - """ |
96 |
| - Return union of two lists of case-insensitive strings a,b. |
97 |
| - """ |
98 |
| - temp = cidict() |
99 |
| - for elt in a: |
100 |
| - temp[elt] = elt |
101 |
| - for elt in b: |
102 |
| - temp[elt] = elt |
103 |
| - return temp.values() |
| 94 | +def strlist_union(a, b): |
| 95 | + """ |
| 96 | + Return union of two lists of case-insensitive strings a,b. |
| 97 | + """ |
| 98 | + temp = cidict() |
| 99 | + for elt in a: |
| 100 | + temp[elt] = elt |
| 101 | + for elt in b: |
| 102 | + temp[elt] = elt |
| 103 | + return temp.values() |
0 commit comments