summaryrefslogtreecommitdiff
path: root/Lib/importlib/metadata/_collections.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2021-05-02 17:03:40 -0400
committerGitHub <noreply@github.com>2021-05-02 17:03:40 -0400
commit37e0c7850de902179b28f1378fbbc38a5ed3628c (patch)
treeecc352d5d7eaf99485bc4c2735d2a5f14f532084 /Lib/importlib/metadata/_collections.py
parent0ad1e0384c8afc5259a6d03363491d89500a5d03 (diff)
downloadcpython-git-37e0c7850de902179b28f1378fbbc38a5ed3628c.tar.gz
bpo-43926: Cleaner metadata with PEP 566 JSON support. (GH-25565)
* bpo-43926: Cleaner metadata with PEP 566 JSON support. * Add blurb * Add versionchanged and versionadded declarations for changes to metadata. * Use descriptor for PEP 566
Diffstat (limited to 'Lib/importlib/metadata/_collections.py')
-rw-r--r--Lib/importlib/metadata/_collections.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/importlib/metadata/_collections.py b/Lib/importlib/metadata/_collections.py
new file mode 100644
index 0000000000..cf0954e1a3
--- /dev/null
+++ b/Lib/importlib/metadata/_collections.py
@@ -0,0 +1,30 @@
+import collections
+
+
+# from jaraco.collections 3.3
+class FreezableDefaultDict(collections.defaultdict):
+ """
+ Often it is desirable to prevent the mutation of
+ a default dict after its initial construction, such
+ as to prevent mutation during iteration.
+
+ >>> dd = FreezableDefaultDict(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()
+
+
+class Pair(collections.namedtuple('Pair', 'name value')):
+ @classmethod
+ def parse(cls, text):
+ return cls(*map(str.strip, text.split("=", 1)))