bound_dictionary.py 798 B

123456789101112131415161718192021222324252627
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from collections import defaultdict
  4. class BoundDictionary(defaultdict):
  5. """
  6. A default dictionary where elements are tightly coupled.
  7. The factory method is responsible for binding the parent object to the child.
  8. If a reference attribute is assigned then child objects will have the key assigned to this.
  9. Otherwise it's just a defaultdict.
  10. """
  11. def __init__(self, reference=None, *args, **kw):
  12. self.reference = reference
  13. super(BoundDictionary, self).__init__(*args, **kw)
  14. def __getitem__(self, key):
  15. value = super(BoundDictionary, self).__getitem__(key)
  16. if self.reference is not None:
  17. setattr(value, self.reference, key)
  18. return value