]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | from setuptools.extern.more_itertools import consume # noqa: F401 |
2 | ||
3 | ||
4 | # copied from jaraco.itertools 6.1 | |
5 | def ensure_unique(iterable, key=lambda x: x): | |
6 | """ | |
7 | Wrap an iterable to raise a ValueError if non-unique values are encountered. | |
8 | ||
9 | >>> list(ensure_unique('abc')) | |
10 | ['a', 'b', 'c'] | |
11 | >>> consume(ensure_unique('abca')) | |
12 | Traceback (most recent call last): | |
13 | ... | |
14 | ValueError: Duplicate element 'a' encountered. | |
15 | """ | |
16 | seen = set() | |
17 | seen_add = seen.add | |
18 | for element in iterable: | |
19 | k = key(element) | |
20 | if k in seen: | |
21 | raise ValueError(f"Duplicate element {element!r} encountered.") | |
22 | seen_add(k) | |
23 | yield element |