-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathspecial_dict.py
535 lines (439 loc) · 14.6 KB
/
special_dict.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Time: 2021-09-24 4:38 下午
Author: huayang
Subject: 自定义字典
"""
import os
import json
import doctest
from typing import *
from dataclasses import dataclass, fields
from collections import OrderedDict
__all__ = [
'ArrayDict',
'ValueArrayDict',
'BunchDict',
'ConfigDict',
'Fields',
'ArrayFields'
]
# class DefaultOrderedDict(defaultdict, OrderedDict):
#
# def __init__(self, default_factory=None, *a, **kw):
# for cls in DefaultOrderedDict.mro()[1:-2]:
# cls.__init__(self, *a, **kw)
#
# super(DefaultOrderedDict, self).__init__()
class ArrayDict(OrderedDict):
"""@Python 自定义数据结构
数组字典,支持 slice
Examples:
>>> d = ArrayDict(a=1, b=2)
>>> d
ArrayDict([('a', 1), ('b', 2)])
>>> d['a']
1
>>> d[1]
ArrayDict([('b', 2)])
>>> d['c'] = 3
>>> d[0] = 100
Traceback (most recent call last):
...
TypeError: ArrayDict cannot use `int` as key.
>>> d[1: 3]
ArrayDict([('b', 2), ('c', 3)])
>>> print(*d)
a b c
>>> d.setdefault('d', 4)
4
>>> print(d)
ArrayDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
>>> d.pop('a')
1
>>> d.update({'b': 20, 'c': 30})
>>> def f(**d): print(d)
>>> f(**d)
{'b': 20, 'c': 30, 'd': 4}
"""
@property
def tuple(self) -> Tuple[Any]:
"""
Convert self to a tuple containing all the attributes/keys that are not ``None``.
"""
return tuple(self.items())
def __getitem__(self, key):
""""""
if isinstance(key, (int,)):
return self.__class__.__call__([self.tuple[key]])
elif isinstance(key, (slice,)):
return self.__class__.__call__(list(self.tuple[key]))
else:
# return self[k] # err: RecursionError
# inner_dict = {k: v for (k, v) in self.items()}
# return inner_dict[k]
return super().__getitem__(key)
def __setitem__(self, key, value):
""""""
if isinstance(key, (int,)):
raise TypeError(f'{self.__class__.__name__} cannot use `{type(key).__name__}` as key.')
else:
super().__setitem__(key, value)
class ValueArrayDict(ArrayDict):
"""@Python 自定义数据结构
数组字典,支持 slice,且操作 values
Examples:
>>> d = ValueArrayDict(a=1, b=2)
>>> d
ValueArrayDict([('a', 1), ('b', 2)])
>>> assert d[1] == 2
>>> d['c'] = 3
>>> assert d[2] == 3
>>> d[1:]
(2, 3)
>>> print(*d) # 注意打印的是 values
1 2 3
>>> del d['a']
>>> d.update({'a':10, 'b': 20})
>>> d
ValueArrayDict([('b', 20), ('c', 3), ('a', 10)])
"""
@property
def tuple(self) -> Tuple[Any]:
"""
Convert self to a tuple containing all the attributes/keys that are not ``None``.
"""
return tuple(self.values())
def __getitem__(self, key):
""""""
if isinstance(key, (int, slice)):
return self.tuple[key]
else:
# return self[k] # err: RecursionError
# inner_dict = {k: v for (k, v) in self.items()}
# return inner_dict[k]
return super().__getitem__(key)
# def setdefault(self, *args, **kwargs):
# """ 不支持 setdefault 操作 """
# raise Exception(f"Cannot use ``setdefault`` on a {self.__class__.__name__} instance.")
# def pop(self, *args, **kwargs):
# """ 不支持 pop 操作 """
# raise Exception(f"Cannot use ``pop`` on a {self.__class__.__name__} instance.")
# def update(self, *args, **kwargs):
# """ 不支持 update 操作 """
# raise Exception(f"Cannot use ``update`` on a {self.__class__.__name__} instance.")
def __iter__(self):
""" dict 默认迭代的对象是 keys,重写使迭代 values
Examples:
>>> sd = ValueArrayDict(a=1, b=2)
>>> # 没有重写 __iter__ 时:
>>> # print(*sd) # a b
>>> # 重写 __iter__ 后:
>>> print(*sd)
1 2
"""
return iter(self.tuple)
class BunchDict(dict):
"""@Python 自定义数据结构
基于 dict 实现 Bunch 模式
行为上类似于 argparse.Namespace,但可以使用 dict 的方法,更通用
Examples:
>>> c = BunchDict(a=1, b=2)
>>> c
{'a': 1, 'b': 2}
>>> c.c = 3
>>> c
{'a': 1, 'b': 2, 'c': 3}
>>> dir(c)
['a', 'b', 'c']
>>> assert 'a' in c
>>> del c.a
>>> assert 'a' not in c
>>> x = BunchDict(d=4, e=c)
>>> x
{'d': 4, 'e': {'b': 2, 'c': 3}}
>>> z = {'d': 4, 'e': {'a': 1, 'b': 2, 'c': 3}}
>>> y = BunchDict.from_dict(z)
>>> y
{'d': 4, 'e': {'a': 1, 'b': 2, 'c': 3}}
References:
- bunch(pip install bunch)
"""
# 最简单实现 Bunch 模式的方法,可以不用重写 __setattr__ 等方法
# def __init__(self, *args, **kwargs):
# super(BunchDict, self).__init__(*args, **kwargs)
# self.__dict__ = self
def __dir__(self):
""" 屏蔽其他属性或方法 """
return self.keys()
def __getattr__(self, key):
""" 使 o.key 等价于 o[key] """
try:
return object.__getattribute__(self, key)
except AttributeError:
try:
return self[key]
except KeyError:
raise AttributeError(key)
def __setattr__(self, name, value):
""" 使 o.name = value 等价于 o[name] = value """
try:
# Throws exception if not in prototype chain
object.__getattribute__(self, name)
except AttributeError:
self[name] = value
else:
object.__setattr__(self, name, value)
def __delattr__(self, key):
""" 支持 del x.y """
try:
# Throws exception if not in prototype chain
object.__getattribute__(self, key)
except AttributeError:
try:
del self[key]
except KeyError:
raise AttributeError(key)
else:
object.__delattr__(self, key)
# 继承自 dict,所以不需要本方法
# def to_dict(self):
# return _unbunch(self)
@classmethod
def from_dict(cls, d: dict):
return _bunch(d, cls)
class ConfigDict(BunchDict):
"""@Python 自定义数据结构
配置字典(基于 BunchDict)
在 BunchDict 基础上添加了 save/load 等操作。
Examples:
# _TestConfig 继承自 BaseConfig,并对配置项设置默认值
>>> class _TestConfig(ConfigDict):
... def __init__(self, **config_items):
... from datetime import datetime
... self.a = 1
... self.b = datetime(2012, 1, 1) # 注意是一个特殊对象,默认 json 是不支持的
... super(_TestConfig, self).__init__(**config_items)
>>> args = _TestConfig()
>>> assert args.a == 1 # 默认值
>>> args.a = 10 # 修改值
>>> assert args.a == 10 # 自定义值
>>> args = _TestConfig(a=10) # 创建时修改
>>> assert args.a == 10
# 添加默认中不存的配置项
>>> args.c = 3 # 默认中没有的配置项(不推荐,建议都定义在继承类中,并设置默认值)
>>> assert args.c == 3
>>> print(args) # 注意 'b' 保存成了特殊形式
_TestConfig: {
"a": 10,
"b": "datetime.datetime(2012, 1, 1, 0, 0)__@AnyEncoder@__gASVKgAAAAAAAACMCGRhdGV0aW1llIwIZGF0ZXRpbWWUk5RDCgfcAQEAAAAAAACUhZRSlC4=",
"c": 3
}
# 保存配置到文件
>>> fp = r'./-test/test_save_config.json'
>>> os.makedirs(os.path.dirname(fp), exist_ok=True)
>>> args.save(fp) # 保存
>>> x = _TestConfig.load(fp) # 重新加载
>>> assert x.dict == args.dict
>>> _ = os.system('rm -rf ./-test')
"""
def __str__(self):
""""""
return f'{self.__class__.__name__}: {self.print_dict}'
@property
def dict(self):
""""""
return self
@property
def print_dict(self):
""""""
from my.python.custom import AnyEncoder
return json.dumps(self.dict, cls=AnyEncoder, indent=4, ensure_ascii=False, sort_keys=True)
def save(self, fp: str):
""" 保存配置到文件 """
with open(fp, 'w', encoding='utf8') as fw:
fw.write(self.print_dict)
@classmethod
def load(cls, fp: str):
""""""
from my.python.custom import AnyDecoder
config_items = json.load(open(fp, encoding='utf8'), cls=AnyDecoder)
return cls(**config_items)
class BunchArrayDict(ArrayDict, BunchDict):
"""
Examples:
>>> d = BunchArrayDict(a=1, b=2)
>>> isinstance(d, dict)
True
>>> print(d, d.a, d[1])
BunchArrayDict([('a', 1), ('b', 2)]) 1 BunchArrayDict([('b', 2)])
>>> d.a, d.b, d.c = 10, 20, 30
>>> print(d, d[1:])
BunchArrayDict([('a', 10), ('b', 20), ('c', 30)]) BunchArrayDict([('b', 20), ('c', 30)])
>>> print(*d)
a b c
>>> dir(d)
['a', 'b', 'c']
>>> assert 'a' in d
>>> del d.a
>>> assert 'a' not in d
>>> getattr(d, 'a', 100)
100
# 测试嵌套
>>> x = BunchArrayDict(d=40, e=d)
>>> x
BunchArrayDict([('d', 40), ('e', BunchArrayDict([('b', 20), ('c', 30)]))])
>>> print(x.d, x.e.b)
40 20
>>> z = {'d': 4, 'e': {'a': 1, 'b': 2, 'c': 3}}
>>> y = BunchArrayDict.from_dict(z)
>>> y
BunchArrayDict([('d', 4), ('e', BunchArrayDict([('a', 1), ('b', 2), ('c', 3)]))])
>>> y.e.c
3
"""
class BunchValueArrayDict(ValueArrayDict, BunchDict):
"""
Examples:
>>> d = BunchValueArrayDict(a=1, b=2)
>>> isinstance(d, dict)
True
>>> print(d, d.a, d[1])
BunchValueArrayDict([('a', 1), ('b', 2)]) 1 2
>>> d.a, d.b, d.c = 10, 20, 30
>>> print(d, d[2], d[1:])
BunchValueArrayDict([('a', 10), ('b', 20), ('c', 30)]) 30 (20, 30)
>>> print(*d)
10 20 30
>>> dir(d)
['a', 'b', 'c']
>>> assert 'a' in d
>>> del d.a
>>> assert 'a' not in d
>>> getattr(d, 'a', 100)
100
# 测试嵌套
>>> x = BunchValueArrayDict(d=40, e=d)
>>> x
BunchValueArrayDict([('d', 40), ('e', BunchValueArrayDict([('b', 20), ('c', 30)]))])
>>> print(x.d, x.e.b)
40 20
>>> z = {'d': 4, 'e': {'a': 1, 'b': 2, 'c': 3}}
>>> y = BunchValueArrayDict.from_dict(z)
>>> y
BunchValueArrayDict([('d', 4), ('e', BunchValueArrayDict([('a', 1), ('b', 2), ('c', 3)]))])
>>> y.e.c
3
"""
@dataclass()
class Fields(BunchDict):
"""
Examples:
>>> @dataclass()
... class Test(Fields):
... c1: str = 'c1'
... c2: int = 0
... c3: list = None
>>> r = Test()
>>> r
Test(c1='c1', c2=0, c3=None)
>>> r.c1
'c1'
>>> r = Test(c1='a', c3=[1,2,3])
>>> r.c1
'a'
>>> r.c3
[1, 2, 3]
>>> d = {'c1': 'C1', 'c2': 10, 'c3': [1, 2]}
>>> t = Test(**d)
>>> t.c4 = 1 # 不推荐新增 attr,因为不会显示在 __repr__ 中,容易混乱
>>> t # 没有 c4
Test(c1='C1', c2=10, c3=[1, 2])
>>> list(t.items()) # 这里有 c4
[('c1', 'C1'), ('c2', 10), ('c3', [1, 2]), ('c4', 1)]
"""
def __post_init__(self):
""""""
# 获取所有 field
class_fields = fields(self)
# 依次添加到 dict 中
for f in class_fields:
self[f.name] = getattr(self, f.name)
@dataclass()
class ArrayFields(Fields, BunchValueArrayDict):
"""
References:
transformers.file_utils.ModelOutput
Examples:
>>> @dataclass()
... class Test(ArrayFields):
... c1: str = 'c1'
... c2: int = 0
... c3: list = None
>>> r = Test()
>>> r
Test(c1='c1', c2=0, c3=None)
>>> r.tuple
('c1', 0, None)
>>> r.c1 # r[0]
'c1'
>>> r[1] # r.c2
0
>>> r[1:]
(0, None)
>>> r = Test(c1='a', c3=[1,2,3])
>>> r.c1
'a'
>>> r[-1]
[1, 2, 3]
>>> for it in r:
... print(it)
a
0
[1, 2, 3]
"""
def _bunch(x, cls):
""" Recursively transforms a dictionary into a Bunch via copy.
>>> b = _bunch({'urmom': {'sez': {'what': 'what'}}}, BunchDict)
>>> b.urmom.sez.what
'what'
bunchify can handle intermediary dicts, lists and tuples (as well as
their subclasses), but ymmv on custom datatypes.
>>> b = _bunch({ 'lol': ('cats', {'hah':'i win'}), 'hello': [{'french':'salut', 'german':'hallo'}]}, BunchDict)
>>> b.hello[0].french
'salut'
>>> b.lol[1].hah
'i win'
nb. As dicts are not hashable, they cannot be nested in sets/frozensets.
"""
if isinstance(x, dict):
return cls((k, _bunch(v, cls)) for k, v in x.items())
elif isinstance(x, (list, tuple)):
return type(x)(_bunch(v, cls) for v in x)
else:
return x
def _unbunch(x): # noqa
""" Recursively converts a Bunch into a dictionary.
>>> b = BunchDict(foo=BunchDict(lol=True), hello=42, ponies='are pretty!')
>>> _unbunch(b)
{'foo': {'lol': True}, 'hello': 42, 'ponies': 'are pretty!'}
unbunchify will handle intermediary dicts, lists and tuples (as well as
their subclasses), but ymmv on custom datatypes.
>>> b = BunchDict(foo=['bar', BunchDict(lol=True)], hello=42, ponies=('pretty!', BunchDict(lies='trouble!')))
>>> _unbunch(b)
{'foo': ['bar', {'lol': True}], 'hello': 42, 'ponies': ('pretty!', {'lies': 'trouble!'})}
nb. As dicts are not hashable, they cannot be nested in sets/frozensets.
"""
if isinstance(x, dict):
return dict((k, _unbunch(v)) for k, v in x.items())
elif isinstance(x, (list, tuple)):
return type(x)(_unbunch(v) for v in x)
else:
return x
def _test():
""""""
doctest.testmod()
if __name__ == '__main__':
""""""
_test()