-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDict.py
More file actions
51 lines (40 loc) · 1.39 KB
/
Dict.py
File metadata and controls
51 lines (40 loc) · 1.39 KB
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
def ExtractNestedKeys(D):
ks = D.keys()
for k in ks:
yield k
if isinstance(D[k],dict):
# ExtractNestedKeys(D[k]) won't work because its a generator
for j in ExtractNestedKeys(D[k]):
yield j
class Dict(dict):
"""
A dict with index path resolving.
d['a']['b']['c'] could be simplified to d['a.b.c']
Written by Logan
"""
def __init__(self, *args, **kwargs):
super(self.__class__,self).__init__(*args,**kwargs)
def __getitem__(self,key):
indices = key.split('.')
# value = self[indices[0]]
# If so, the program will get stuck in the infinite recursion
value = super(self.__class__,self).__getitem__(indices[0])
#print(value.__class__)
# You will see that now value is a dict object, not Dict, and thus we can call the [] operator.
for k in indices[1:]:
value = value[k]
return value
def __contains__(self,key):
"""
magic method for 'in' operator to check whether some key exists in a nested dict.
"""
for i in ExtractNestedKeys(self):
if i ==key:
return True
return False
if __name__ == '__main__':
#d = {'l01':{'l11':{'l21':'a','l22':'b'},'l12':'c'},'l02':{'l12':'e'}}
d = {'l01':{'l11':'c'},'l02':'e'}
D = Dict(d)
result = 'l11' in D
print(result)