Source code

Download source archive ↓

src/openpyxl/utils/bound_dictionary.py

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