forked from LeoLuo0320/DeliverCoffee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
339 lines (273 loc) · 8.16 KB
/
utils.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import inspect
import collections
import json
import operator
class DictTree(collections.Mapping):
"""Nested dict.
Values can be accessed as attributes as well as items:
>>> d = DictTree({'a': 1, 'sub': {'x': 10}}, b=2, **{'c': 3})
>>> d.a
1
>>> d['b']
2
>>> d.e = 5
>>> d['f'] = 6
>>> d.sub.x
10
>>> d.sub.y = 11
>>> d[['sub', 'y']]
11
>>> d[['sub', 'z']] = 12
>>> d |= {'g': 7}
>>> d += {'a': 100}
>>> d + DictTree(sub=DictTree(z=200))
DictTree(a=101, sub=DictTree(x=10, y=11, z=212), b=2, c=3, e=5, f=6, g=7)
@DynamicAttrs
"""
def __init__(self, *maps, **attr):
"""
Args:
*maps (Mapping): Deep copy values from these mappings.
**attr: Extra attributes.
"""
super(DictTree, self).__init__()
for m in maps:
for k, v in m.items():
if isinstance(v, collections.Mapping):
self[k] = DictTree(v)
else:
self[k] = v
self.update(attr)
def __getitem__(self, k):
"""
Args:
k: Item key. May be list for deep access.
Returns:
Item value.
"""
if isinstance(k, list):
if len(k) == 0:
return self
elif len(k) == 1:
return self[k[0]]
else:
return self[k[0]][k[1:]]
else:
return vars(self)[k]
def setdefault(self, k, v):
"""
Args:
k: Item key. May be list for deep access, which creates nested `DictTree`s as needed.
v: Item value. This value is set if item doesn't exist.
Returns:
Item value.
Raises:
KeyError: Item key is reserved.
"""
if isinstance(k, list):
if len(k) == 0:
return self
elif len(k) == 1:
return self.setdefault(k[0], v)
else:
return self.setdefault(k[0], DictTree()).setdefault(k[1:], v)
elif k in FORBIDDEN:
raise KeyError('key "{}" is reserved'.format(k))
else:
return vars(self).setdefault(k, v)
def __setitem__(self, k, v):
"""
Args:
k: Item key. May be list for deep access, which creates nested `DictTree`s as needed.
v: Item value.
Raises:
KeyError: Item key is empty list or reserved.
"""
if isinstance(k, list):
if len(k) == 0:
raise KeyError("cannot assign to root")
elif len(k) == 1:
self[k[0]] = v
else:
self.setdefault(k[0], DictTree())[k[1:]] = v
elif k in FORBIDDEN:
raise KeyError('key "{}" is reserved'.format(k))
else:
vars(self)[k] = v
def __setattr__(self, k, v):
self[k] = v
def __delitem__(self, k):
"""
Args:
k: Item key. May be list for deep access.
"""
if isinstance(k, list):
if len(k) == 0:
raise KeyError("cannot delete root")
elif len(k) == 1:
self.__delitem__(k[0])
else:
self[k[0]].__delitem__(k[1:])
else:
vars(self).__delitem__(k)
def __len__(self):
"""
Returns:
int: Number of top-level elements.
"""
return len(vars(self))
def __str__(self):
return ", ".join("{}={}".format(k, repr(v)) for k, v in self.items())
def __repr__(self):
return 'DictTree({})'.format(str(self))
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, DictTree):
return vars(o)
else:
return super(DictTree.JSONEncoder, self).default(o)
class JSONDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
super(DictTree.JSONDecoder, self).__init__(object_hook=self.object_hook, *args, **kwargs)
@staticmethod
def object_hook(d):
return DictTree(d)
def __contains__(self, k):
"""
Args:
k: Item key. May be list for deep access.
Returns:
bool: Key existence.
"""
if isinstance(k, list):
if len(k) == 0:
return True
elif len(k) == 1:
return k[0] in self
else:
if k[0] in self:
return k[1:] in self[k[0]]
else:
return False
else:
return k in vars(self)
def __iter__(self):
"""
Returns:
Iterator: Shallow iterator over keys.
"""
return iter(vars(self))
def keys(self):
"""
Returns:
Iterator: Shallow iterator over items.
"""
return vars(self).keys()
def items(self):
"""
Returns:
Iterator: Shallow iterator over items.
"""
return vars(self).items()
def values(self):
"""
Returns:
Iterator: Shallow iterator over values.
"""
return vars(self).values()
def allkeys(self):
"""
Returns:
Iterator: Deep iterator over keys.
"""
for k, v in self.items():
if isinstance(v, DictTree):
for k_ in v.allkeys():
yield [k] + k_
else:
yield [k]
def allitems(self):
"""
Returns:
Iterator: Deep iterator over items.
"""
for k, v in self.items():
if isinstance(v, DictTree):
for k_, v_ in v.allitems():
yield [k] + k_, v_
else:
yield [k], v
def allvalues(self):
"""
Returns:
Iterator: Deep iterator over values.
"""
for v in self.values():
if isinstance(v, DictTree):
for v_ in v.allvalues():
yield v_
else:
yield v
def copy(self):
"""
Returns:
DictTree: Deep copy of `self`.
"""
res = DictTree()
for k, v in self.items():
if isinstance(v, DictTree):
res[k] = v.copy()
else:
res[k] = v
return res
def update(self, d, op=None):
"""
Args:
d (Mapping): Mapping from which to update.
op ((Any, Any) -> Any): Update operator, such that `x = op(x, y)` updates value `x` with new value `y`.
Default: replace current value, `x = y`.
Returns:
DictTree: `self` after update.
"""
for k, v in d.items():
if k in self and isinstance(self[k], DictTree) and isinstance(v, collections.Mapping):
self[k].update(v, op)
elif op is None:
self[k] = v
else:
self[k] = op(self[k], v)
return self
def __ior__(self, d):
return self.update(d)
def __or__(self, d):
return self.copy().update(d)
def __iadd__(self, d):
return self.update(d, operator.iadd)
def __add__(self, d):
return self.copy().update(d, operator.iadd)
FORBIDDEN = set(dir(DictTree()))
def get_subclass_set(cls, base_cls):
subcls_set = {}
for subclass_name in dir(cls):
attr = getattr(cls, subclass_name)
if inspect.isclass(attr) and issubclass(attr, base_cls):
subcls_set[subclass_name] = attr
return subcls_set
def subclass_set(base_cls, property_name):
class MetaClass(type):
def __init__(cls, name, bases, dct):
super().__init__(name, bases, dct)
setattr(cls, property_name, get_subclass_set(cls, base_cls))
return MetaClass
def encode(x, l):
return [y == x for y in l]
def one_hot(i, l):
assert i < l, (i, l)
res = [0.] * l
res[i] = 1.
return res
def pad(x, l):
if x is None:
return [0.] * l
assert len(x) <= l, (len(x), l, x)
return x + ([0.] * (l - len(x)))