_itertools.py 675 B

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