diff options
| author | Lex Neva <lexelby@users.noreply.github.com> | 2018-12-15 20:21:41 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-12-15 20:21:41 -0500 |
| commit | 1e0280db10cbb987842648f78f37bb9acc827305 (patch) | |
| tree | b3ddf36770535402ca9a2ae7af2d1e3d8bffae33 /lib/utils/dotdict.py | |
| parent | 8389d792adfd4d43785d3850e0558f2b386c7266 (diff) | |
basic lettering GUI (#351)
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 |
