-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoodict.py
85 lines (69 loc) · 1.98 KB
/
oodict.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
"""OODict: object view of dict
Copyright (C) 2008-2009 Chen Zheng <[email protected]>
Distributed under terms of GPL v2
"""
class OODict(dict):
"""
OODict
OO style dict
Examples:
>>> a = OODict({'a': 1, 'c': {'d': 2}, 'b': 2})
>>> a
{'a': 1, 'c': {'d': 2}, 'b': 2}
>>> a.a=0
>>> a
{'a': 0, 'c': {'d': 2}, 'b': 2}
>>> a.e=0
>>> a
{'a': 0, 'c': {'d': 2}, 'b': 2, 'e': 0}
>>> a.c = 5
>>> a
{'a': 0, 'c': 5, 'b': 2, 'e': 0}
>>> a.f = OODict({'f':'f'})
>>> a
{'a': 0, 'c': 5, 'b': 2, 'e': 0, 'f': {'f': 'f'}}
>>> a.f.f
'f'
>>> a.c = {'d': 2}
>>> a
{'a': 0, 'c': {'d': 2}, 'b': 2, 'e': 0, 'f': {'f': 'f'}}
>>> a.c
{'d': 2}
>>> a.c.d
2
>>> a.c.e = {'e': 'e'}
>>> a
{'a': 0, 'c': {'e': {'e': 'e'}, 'd': 2}, 'b': 2, 'e': 0, 'f': {'f': 'f'}}
>>> a.c.e.e
'e'
>>> a['c']['e'].e
'e'
Problems:
* can't use del a.c, must use a['c']
*If a.k is a dict, v is returned still as a dict in the following code:
for k, v in a.items():
pass # v is still a dict
You can use like this instead:
for k in a.keys():
v = a[k] # v is a OODict now
Perhaps we should define our own 'items'.
"""
def __init__(self, data = {}):
dict.__init__(self, data)
def __getitem__(self, key):
value = dict.__getitem__(self, key)
if isinstance(value, dict) and not isinstance(value, OODict):
# Fixme! There maybe a problem here when value is a subclass of dict
value = OODict(value)
self[key] = value # Auto covert children dict to OODict
return value
def __getattr__(self, key):
try:
return self[key]
except KeyError, err:
raise AttributeError(err)
def __setattr__(self, key, value):
self[key] = value
if __name__ == "__main__":
import doctest
doctest.testmod()