Debian Codegraph @

IPython/utils/data.py

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