bunch.py 652 B

12345678910111213141516171819202122232425
  1. """Yet another implementation of bunch
  2. attribute-access of items on a dict.
  3. """
  4. # Copyright (c) Jupyter Development Team.
  5. # Distributed under the terms of the Modified BSD License.
  6. class Bunch(dict):
  7. """A dict with attribute-access"""
  8. def __getattr__(self, key):
  9. try:
  10. return self.__getitem__(key)
  11. except KeyError:
  12. raise AttributeError(key)
  13. def __setattr__(self, key, value):
  14. self.__setitem__(key, value)
  15. def __dir__(self):
  16. # py2-compat: can't use super because dict doesn't have __dir__
  17. names = dir({})
  18. names.extend(self.keys())
  19. return names