File: data.py

package info (click to toggle)
ipython 9.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,624 kB
  • sloc: python: 45,268; sh: 317; makefile: 168
file content (50 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# encoding: utf-8
"""Utilities for working with data structures like lists, dicts and tuples.
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2008-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

import warnings
from collections.abc import Iterable, Sequence
from typing import TypeVar


T = TypeVar("T")


def uniq_stable(elems: Iterable[T]) -> list[T]:
    """uniq_stable(elems) -> list

    .. deprecated:: 9.8
        This function is deprecated and will be removed in a future version.
        It is not used within IPython and was never part of the public API.

    Return from an iterable, a list of all the unique elements in the input,
    but maintaining the order in which they first appear.

    Note: All elements in the input must be hashable for this routine
    to work, as it internally uses a set for efficiency reasons.
    """
    warnings.warn(
        "uniq_stable is deprecated since IPython 9.8 and will be removed in a future version. "
        "It was never part of the public API.",
        DeprecationWarning,
        stacklevel=2,
    )
    seen: set[T] = set()
    result: list[T] = []
    for x in elems:
        if x not in seen:
            seen.add(x)
            result.append(x)
    return result


def chop(seq: Sequence[T], size: int) -> list[Sequence[T]]:
    """Chop a sequence into chunks of the given size."""
    return [seq[i : i + size] for i in range(0, len(seq), size)]