diff --git a/repos/2de5983237b66098945374eb79878a1d/recursivedict.py b/repos/2de5983237b66098945374eb79878a1d/recursivedict.py new file mode 100644 index 0000000..cf5fb49 --- /dev/null +++ b/repos/2de5983237b66098945374eb79878a1d/recursivedict.py @@ -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