bound_dictionary.py 717 B

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