-
Notifications
You must be signed in to change notification settings - Fork 822
/
collections.py
254 lines (174 loc) · 8.19 KB
/
collections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Patroni custom object types somewhat like :mod:`collections` module.
Provides a case insensitive :class:`dict` and :class:`set` object types, and `EMPTY_DICT` frozen dictionary object.
"""
from collections import OrderedDict
from copy import deepcopy
from typing import Any, Collection, Dict, Iterator, KeysView, Mapping, MutableMapping, MutableSet, Optional
class CaseInsensitiveSet(MutableSet[str]):
"""A case-insensitive :class:`set`-like object.
Implements all methods and operations of :class:`~typing.MutableSet`. All values are expected to be strings.
The structure remembers the case of the last value set, however, contains testing is case insensitive.
"""
def __init__(self, values: Optional[Collection[str]] = None) -> None:
"""Create a new instance of :class:`CaseInsensitiveSet` with the given *values*.
:param values: values to be added to the set.
"""
self._values: Dict[str, str] = {}
for v in values or ():
self.add(v)
def __repr__(self) -> str:
"""Get a string representation of the set.
Provide a helpful way of recreating the set.
:returns: representation of the set, showing its values.
:Example:
>>> repr(CaseInsensitiveSet(('1', 'test', 'Test', 'TESt', 'test2'))) # doctest: +ELLIPSIS
"<CaseInsensitiveSet('1', 'TESt', 'test2') at ..."
"""
return '<{0}{1} at {2:x}>'.format(type(self).__name__, tuple(self._values.values()), id(self))
def __str__(self) -> str:
"""Get set values for printing.
:returns: set of values in string format.
:Example:
>>> str(CaseInsensitiveSet(('1', 'test', 'Test', 'TESt', 'test2'))) # doctest: +SKIP
"{'TESt', 'test2', '1'}"
"""
return str(set(self._values.values()))
def __contains__(self, value: object) -> bool:
"""Check if set contains *value*.
The check is performed case-insensitively.
:param value: value to be checked.
:returns: ``True`` if *value* is already in the set, ``False`` otherwise.
"""
return isinstance(value, str) and value.lower() in self._values
def __iter__(self) -> Iterator[str]:
"""Iterate over the values in this set.
:yields: values from set.
"""
return iter(self._values.values())
def __len__(self) -> int:
"""Get the length of this set.
:returns: number of values in the set.
:Example:
>>> len(CaseInsensitiveSet(('1', 'test', 'Test', 'TESt', 'test2')))
3
"""
return len(self._values)
def add(self, value: str) -> None:
"""Add *value* to this set.
Search is performed case-insensitively. If *value* is already in the set, overwrite it with *value*, so we
"remember" the last case of *value*.
:param value: value to be added to the set.
"""
self._values[value.lower()] = value
def discard(self, value: str) -> None:
"""Remove *value* from this set.
Search is performed case-insensitively. If *value* is not present in the set, no exception is raised.
:param value: value to be removed from the set.
"""
self._values.pop(value.lower(), None)
def issubset(self, other: 'CaseInsensitiveSet') -> bool:
"""Check if this set is a subset of *other*.
:param other: another set to be compared with this set.
:returns: ``True`` if this set is a subset of *other*, else ``False``.
"""
return self <= other
class CaseInsensitiveDict(MutableMapping[str, Any]):
"""A case-insensitive :class:`dict`-like object.
Implements all methods and operations of :class:`~typing.MutableMapping` as well as :class:`dict`'s
:func:`~dict.copy`. All keys are expected to be strings. The structure remembers the case of the last key to be set,
and :func:`iter`, :func:`dict.keys`, :func:`dict.items`, :func:`dict.iterkeys`, and :func:`dict.iteritems` will
contain case-sensitive keys. However, querying and contains testing is case insensitive.
"""
def __init__(self, data: Optional[Dict[str, Any]] = None) -> None:
"""Create a new instance of :class:`CaseInsensitiveDict` with the given *data*.
:param data: initial dictionary to create a :class:`CaseInsensitiveDict` from.
"""
self._values: OrderedDict[str, Any] = OrderedDict()
self.update(data or {})
def __setitem__(self, key: str, value: Any) -> None:
"""Assign *value* to *key* in this dict.
*key* is searched/stored case-insensitively in the dict. The corresponding value in the dict is a tuple of:
* original *key*;
* *value*.
:param key: key to be created or updated in the dict.
:param value: value for *key*.
"""
self._values[key.lower()] = (key, value)
def __getitem__(self, key: str) -> Any:
"""Get the value corresponding to *key*.
*key* is searched case-insensitively in the dict.
.. note:
If *key* is not present in the dict, :class:`KeyError` will be triggered.
:param key: key to be searched in the dict.
:returns: value corresponding to *key*.
"""
return self._values[key.lower()][1]
def __delitem__(self, key: str) -> None:
"""Remove *key* from this dict.
*key* is searched case-insensitively in the dict.
.. note:
If *key* is not present in the dict, :class:`KeyError` will be triggered.
:param key: key to be removed from the dict.
"""
del self._values[key.lower()]
def __iter__(self) -> Iterator[str]:
"""Iterate over keys of this dict.
:yields: each key present in the dict. Yields each key with its last case that has been stored.
"""
return iter(key for key, _ in self._values.values())
def __len__(self) -> int:
"""Get the length of this dict.
:returns: number of keys in the dict.
:Example:
>>> len(CaseInsensitiveDict({'a': 'b', 'A': 'B', 'c': 'd'}))
2
"""
return len(self._values)
def copy(self) -> 'CaseInsensitiveDict':
"""Create a copy of this dict.
:return: a new dict object with the same keys and values of this dict.
"""
return CaseInsensitiveDict({v[0]: v[1] for v in self._values.values()})
def keys(self) -> KeysView[str]:
"""Return a new view of the dict's keys.
:returns: a set-like object providing a view on the dict's keys
"""
return self._values.keys()
def __repr__(self) -> str:
"""Get a string representation of the dict.
Provide a helpful way of recreating the dict.
:returns: representation of the dict, showing its keys and values.
:Example:
>>> repr(CaseInsensitiveDict({'a': 'b', 'A': 'B', 'c': 'd'})) # doctest: +ELLIPSIS
"<CaseInsensitiveDict{'A': 'B', 'c': 'd'} at ..."
"""
return '<{0}{1} at {2:x}>'.format(type(self).__name__, dict(self.items()), id(self))
class _FrozenDict(Mapping[str, Any]):
"""Frozen dictionary object."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Create a new instance of :class:`_FrozenDict` with given data."""
self.__values: Dict[str, Any] = dict(*args, **kwargs)
def __iter__(self) -> Iterator[str]:
"""Iterate over keys of this dict.
:yields: each key present in the dict. Yields each key with its last case that has been stored.
"""
return iter(self.__values)
def __len__(self) -> int:
"""Get the length of this dict.
:returns: number of keys in the dict.
:Example:
>>> len(_FrozenDict())
0
"""
return len(self.__values)
def __getitem__(self, key: str) -> Any:
"""Get the value corresponding to *key*.
:returns: value corresponding to *key*.
"""
return self.__values[key]
def copy(self) -> Dict[str, Any]:
"""Create a copy of this dict.
:return: a new dict object with the same keys and values of this dict.
"""
return deepcopy(self.__values)
EMPTY_DICT = _FrozenDict()