Skip to content

Commit

Permalink
Add freezable dict
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Mar 27, 2021
1 parent 99f83cf commit 8e3e4af
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions importlib_metadata/_collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import collections


class freezable_defaultdict(collections.defaultdict):
"""
Mix-in to freeze a defaultdict.
>>> dd = freezable_defaultdict(list)
>>> dd[0].append('1')
>>> dd.freeze()
>>> dd[1]
[]
>>> len(dd)
1
"""

def __missing__(self, key):
return getattr(self, '_frozen', super().__missing__)(key)

def freeze(self):
self._frozen = lambda key: self.default_factory()

0 comments on commit 8e3e4af

Please sign in to comment.