Coverage for kwave/utils/dotdictionary.py: 45%

1 class dotdict(dict):

2 """

3 A dictionary supporting dot notation.

4 """

5 __getattr__ = dict.get

6 __setattr__ = dict.__setitem__

7 __delattr__ = dict.__delitem__

8

9 def __init__(self, *args, **kwargs):

10 super().__init__(*args, **kwargs)

11 for k, v in self.items():

12 if isinstance(v, dict):

13 self[k] = dotdict(v)

14

15 def lookup(self, dotkey):

16 """

17 Lookup value in a nested structure with a single key, e.g. "a.b.c"

18 """

19 path = list(reversed(dotkey.split(".")))

20 v = self

21 while path:

22 key = path.pop()

23 if isinstance(v, dict):

24 v = v[key]

25 elif isinstance(v, list):

26 v = v[int(key)]

27 else:

28 raise KeyError(key)

29 return v