This repository has been archived by the owner on Apr 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
itypes.py
224 lines (174 loc) · 5.52 KB
/
itypes.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
# coding: utf-8
try:
from collections.abc import Mapping, Sequence
except ImportError: # support for python 2.x
from collections import Mapping, Sequence
__version__ = '1.2.0'
def to_mutable(instance):
if isinstance(instance, Dict):
return {
key: to_mutable(value)
for key, value in instance.items()
}
elif isinstance(instance, List):
return [
to_mutable(value)
for value in instance
]
return instance
def to_immutable(value):
if isinstance(value, dict):
return Dict(value)
elif isinstance(value, list):
return List(value)
return value
def _to_hashable(instance):
if isinstance(instance, Dict):
items = sorted(instance.items(), key=lambda item: item[0])
return (
(key, _to_hashable(value))
for key, value in items
)
elif isinstance(instance, List):
return [
_to_hashable(value)
for value in instance
]
return instance
def _set_in(node, keys, value):
if not keys:
return value
elif len(keys) == 1:
return node.set(keys[0], value)
key = keys[0]
child = node[key]
if not isinstance(child, (Dict, List)):
msg = "Expected a container type at key '%s', but got '%s'"
raise KeyError(msg % type(child))
child = child.set_in(keys[1:], value)
return node.set(key, child)
def _delete_in(node, keys):
if not keys:
return
elif len(keys) == 1:
return node.delete(keys[0])
key = keys[0]
child = node[key]
if not isinstance(child, (Dict, List)):
msg = "Expected a container type at key '%s', but got '%s'"
raise KeyError(msg % type(child))
child = child.delete_in(keys[1:])
return node.set(key, child)
def _get_in(node, keys, default=None):
if not keys:
return default
key = keys[0]
try:
child = node[key]
except (KeyError, IndexError):
return default
if len(keys) == 1:
return child
return child.get_in(keys[1:], default=default)
class Object(object):
def __setattr__(self, key, value):
if key.startswith('_'):
return object.__setattr__(self, key, value)
msg = "'%s' object doesn't support property assignment."
raise TypeError(msg % self.__class__.__name__)
class Dict(Mapping):
def __init__(self, *args, **kwargs):
self._data = {
key: to_immutable(value)
for key, value in dict(*args, **kwargs).items()
}
def __setattr__(self, key, value):
if key.startswith('_'):
return object.__setattr__(self, key, value)
msg = "'%s' object doesn't support property assignment."
raise TypeError(msg % self.__class__.__name__)
def __getitem__(self, key):
return self._data[key]
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self._data == other._data
return self._data == other
def __hash__(self):
return hash(_to_hashable(self))
def __repr__(self):
return "%s(%s)" % (
self.__class__.__name__,
to_mutable(self)
)
def __str__(self):
return str(self._data)
def set(self, key, value):
data = dict(self._data)
data[key] = value
if hasattr(self, 'clone'):
return self.clone(data)
return type(self)(data)
def delete(self, key):
data = dict(self._data)
data.pop(key)
if hasattr(self, 'clone'):
return self.clone(data)
return type(self)(data)
def get_in(self, keys, default=None):
return _get_in(self, keys, default=default)
def set_in(self, keys, value):
return _set_in(self, keys, value)
def delete_in(self, keys):
return _delete_in(self, keys)
class List(Sequence):
def __init__(self, *args):
self._data = [
to_immutable(value)
for value in list(*args)
]
def __setattr__(self, key, value):
if key == '_data':
return object.__setattr__(self, key, value)
msg = "'%s' object doesn't support property assignment."
raise TypeError(msg % self.__class__.__name__)
def __getitem__(self, key):
return self._data[key]
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self._data == other._data
return self._data == other
def __hash__(self):
return hash(_to_hashable(self))
def __repr__(self):
return "%s(%s)" % (
self.__class__.__name__,
to_mutable(self)
)
def __str__(self):
return str(self._data)
def set(self, key, value):
data = list(self._data)
data[key] = value
if hasattr(self, 'clone'):
return self.clone(data)
return type(self)(data)
def delete(self, key):
data = list(self._data)
data.pop(key)
if hasattr(self, 'clone'):
return self.clone(data)
return type(self)(data)
def get_in(self, keys, default=None):
return _get_in(self, keys, default=default)
def set_in(self, keys, value):
return _set_in(self, keys, value)
def delete_in(self, keys):
return _delete_in(self, keys)