diff options
| author | Lex Neva <github.com@lexneva.name> | 2018-12-18 20:23:04 -0500 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2018-12-18 20:23:04 -0500 |
| commit | ef45f4b7406ec0d7457ea9a6e879007f37dc64f8 (patch) | |
| tree | 9145e4ef427ba89bc58eacd0e8709482efc5cb96 /lib/utils/dotdict.py | |
| parent | 8f3c922011a5c39a6154863160ea8354a502ed42 (diff) | |
| parent | 3cfda3c0b949bd4ba1f00a99f285b029dcc975ee (diff) | |
Merge branch 'master' into lexelby/bugs
Diffstat (limited to 'lib/utils/dotdict.py')
| -rw-r--r-- | lib/utils/dotdict.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/utils/dotdict.py b/lib/utils/dotdict.py new file mode 100644 index 00000000..1ab3a4fe --- /dev/null +++ b/lib/utils/dotdict.py @@ -0,0 +1,30 @@ +class DotDict(dict): + """A dict subclass that allows accessing methods using dot notation. + + adapted from: https://stackoverflow.com/questions/13520421/recursive-dotdict + """ + + def __init__(self, *args, **kwargs): + super(DotDict, self).__init__(*args, **kwargs) + + for k, v in self.iteritems(): + if isinstance(v, dict): + self[k] = DotDict(v) + + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ + + def __getattr__(self, name): + if name.startswith('_'): + raise AttributeError("'DotDict' object has no attribute '%s'" % name) + + if name in self: + return self.__getitem__(name) + else: + new_dict = DotDict() + self.__setitem__(name, new_dict) + return new_dict + + def __repr__(self): + super_repr = super(DotDict, self).__repr__() + return "DotDict(%s)" % super_repr |
