Add dotdict.py.

This commit is contained in:
Ethan Dalool 2020-02-19 13:11:26 -08:00
parent b66a89268b
commit e334396d56

22
voussoirkit/dotdict.py Normal file
View file

@ -0,0 +1,22 @@
from voussoirkit import sentinel
NO_DEFAULT = sentinel.Sentinel('NO_DEFAULT')
class DotDict:
def __init__(self, default=NO_DEFAULT, **kwargs):
self.__default = default
self.__dict__.update(**kwargs)
def __getattr__(self, key):
try:
return self.__dict__[key]
except KeyError:
if self.__default is not NO_DEFAULT:
return self.__default
raise
def __setattr__(self, key, value):
self.__dict__[key] = value
def __repr__(self):
return f'DotDict {self.__dict__}'