1
0
mirror of https://github.com/balkian/gists.git synced 2024-11-01 08:01:44 +00:00
gists/repos/2de5983237b66098945374eb79878a1d/recursivedict.py

21 lines
541 B
Python
Raw Normal View History

2017-11-07 13:37:08 +00:00
class RecursiveDict(UserDict):
def __getitem__(self, k):
if not isinstance(k, Iterable):
return self.data[k]
dest = self.data
for j in k:
dest = dest[j]
return dest
def __setitem__(self, k, v):
if not isinstance(k, Iterable):
return self.data[k] = v
levels = len(k)
dest = self.data
for j in range(levels-1):
if k[j] not in dest:
dest[k[j]] = {}
dest = dest[k[j]]
dest[levels-1] = v