cf0954e1a30546d781bf25781ec716ef92a77e32
[SubU] /
1 import collections
2
3
4 # from jaraco.collections 3.3
5 class FreezableDefaultDict(collections.defaultdict):
6     """
7     Often it is desirable to prevent the mutation of
8     a default dict after its initial construction, such
9     as to prevent mutation during iteration.
10
11     >>> dd = FreezableDefaultDict(list)
12     >>> dd[0].append('1')
13     >>> dd.freeze()
14     >>> dd[1]
15     []
16     >>> len(dd)
17     1
18     """
19
20     def __missing__(self, key):
21         return getattr(self, '_frozen', super().__missing__)(key)
22
23     def freeze(self):
24         self._frozen = lambda key: self.default_factory()
25
26
27 class Pair(collections.namedtuple('Pair', 'name value')):
28     @classmethod
29     def parse(cls, text):
30         return cls(*map(str.strip, text.split("=", 1)))