From f9d451457d7b731dfae3a2840dac5fc790acf237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 7 Nov 2017 14:37:08 +0100 Subject: [PATCH] --- recursivedict.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 recursivedict.py diff --git a/recursivedict.py b/recursivedict.py new file mode 100644 index 0000000..cf5fb49 --- /dev/null +++ b/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