1
0
mirror of https://github.com/balkian/gists.git synced 2024-11-21 17:22:29 +00:00
This commit is contained in:
J. Fernando Sánchez 2017-11-07 14:37:08 +01:00 committed by GitHub
commit f9d451457d

20
recursivedict.py Normal file
View File

@ -0,0 +1,20 @@
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