data.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # encoding: utf-8
  2. """Utilities for working with data structures like lists, dicts and tuples.
  3. """
  4. #-----------------------------------------------------------------------------
  5. # Copyright (C) 2008-2011 The IPython Development Team
  6. #
  7. # Distributed under the terms of the BSD License. The full license is in
  8. # the file COPYING, distributed as part of this software.
  9. #-----------------------------------------------------------------------------
  10. import warnings
  11. from collections.abc import Iterable, Sequence
  12. from typing import TypeVar
  13. T = TypeVar("T")
  14. def uniq_stable(elems: Iterable[T]) -> list[T]:
  15. """uniq_stable(elems) -> list
  16. .. deprecated:: 9.8
  17. This function is deprecated and will be removed in a future version.
  18. It is not used within IPython and was never part of the public API.
  19. Return from an iterable, a list of all the unique elements in the input,
  20. but maintaining the order in which they first appear.
  21. Note: All elements in the input must be hashable for this routine
  22. to work, as it internally uses a set for efficiency reasons.
  23. """
  24. warnings.warn(
  25. "uniq_stable is deprecated since IPython 9.8 and will be removed in a future version. "
  26. "It was never part of the public API.",
  27. DeprecationWarning,
  28. stacklevel=2,
  29. )
  30. seen: set[T] = set()
  31. result: list[T] = []
  32. for x in elems:
  33. if x not in seen:
  34. seen.add(x)
  35. result.append(x)
  36. return result
  37. def chop(seq: Sequence[T], size: int) -> list[Sequence[T]]:
  38. """Chop a sequence into chunks of the given size."""
  39. return [seq[i : i + size] for i in range(0, len(seq), size)]