summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/resources/_itertools.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2021-12-31 02:00:48 (GMT)
committerGitHub <noreply@github.com>2021-12-31 02:00:48 (GMT)
commite712a5b277866a71c195f38c1b5d87d9126dba3e (patch)
tree0adfeaa93118e057089101c9e07678ca6c6daafd /Lib/importlib/resources/_itertools.py
parent2cf7d02b99ce8cebd26d330aa8aac2ee369d4600 (diff)
downloadcpython-e712a5b277866a71c195f38c1b5d87d9126dba3e.zip
cpython-e712a5b277866a71c195f38c1b5d87d9126dba3e.tar.gz
cpython-e712a5b277866a71c195f38c1b5d87d9126dba3e.tar.bz2
bpo-46118: Move importlib.resources to its own package. (#30176)
* bpo-46118: Move importlib.resources to its own package. * Expand compatibility shims with documentation and explicit imports.
Diffstat (limited to 'Lib/importlib/resources/_itertools.py')
-rw-r--r--Lib/importlib/resources/_itertools.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/importlib/resources/_itertools.py b/Lib/importlib/resources/_itertools.py
new file mode 100644
index 0000000..cce0558
--- /dev/null
+++ b/Lib/importlib/resources/_itertools.py
@@ -0,0 +1,35 @@
+from itertools import filterfalse
+
+from typing import (
+ Callable,
+ Iterable,
+ Iterator,
+ Optional,
+ Set,
+ TypeVar,
+ Union,
+)
+
+# Type and type variable definitions
+_T = TypeVar('_T')
+_U = TypeVar('_U')
+
+
+def unique_everseen(
+ iterable: Iterable[_T], key: Optional[Callable[[_T], _U]] = None
+) -> Iterator[_T]:
+ "List unique elements, preserving order. Remember all elements ever seen."
+ # unique_everseen('AAAABBBCCDAABBB') --> A B C D
+ # unique_everseen('ABBCcAD', str.lower) --> A B C D
+ seen: Set[Union[_T, _U]] = set()
+ seen_add = seen.add
+ if key is None:
+ for element in filterfalse(seen.__contains__, iterable):
+ seen_add(element)
+ yield element
+ else:
+ for element in iterable:
+ k = key(element)
+ if k not in seen:
+ seen_add(k)
+ yield element