File: set_iter.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (99 lines) | stat: -rw-r--r-- 1,989 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# mode: run
# tag: set

cimport cython


@cython.test_assert_path_exists(
    "//SetIterationNextNode",
)
def set_iter_comp(set s):
    """
    >>> s = set([1, 2, 3])
    >>> sorted(set_iter_comp(s))
    [1, 2, 3]
    """
    return [x for x in s]


@cython.test_assert_path_exists(
    "//SetIterationNextNode",
)
def set_iter_comp_typed(set s):
    """
    >>> s = set([1, 2, 3])
    >>> sorted(set_iter_comp(s))
    [1, 2, 3]
    """
    cdef int x
    return [x for x in s]


@cython.test_assert_path_exists(
    "//SetIterationNextNode",
)
def frozenset_iter_comp(frozenset s):
    """
    >>> s = frozenset([1, 2, 3])
    >>> sorted(frozenset_iter_comp(s))
    [1, 2, 3]
    """
    return [x for x in s]


@cython.test_assert_path_exists(
    "//SetIterationNextNode",
)
def set_iter_comp_frozenset(set s):
    """
    >>> s = set([1, 2, 3])
    >>> sorted(set_iter_comp(s))
    [1, 2, 3]
    """
    return [x for x in frozenset(s)]


@cython.test_assert_path_exists(
    "//SetIterationNextNode",
)
def set_iter_modify(set s, int value):
    """
    >>> s = set([1, 2, 3])
    >>> sorted(set_iter_modify(s, 1))
    [1, 2, 3]
    >>> sorted(set_iter_modify(s, 2))
    [1, 2, 3]
    >>> sorted(set_iter_modify(s, 3))
    [1, 2, 3]
    >>> sorted(set_iter_modify(s, 4))  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    RuntimeError: ...et changed size during iteration
    """
    for x in s:
        s.add(value)
    return s


@cython.test_fail_if_path_exists(
    "//SimpleCallNode//NameNode[@name = 'enumerate']",
)
@cython.test_assert_path_exists(
    "//AddNode",
    "//SetIterationNextNode",
)
def set_iter_enumerate(set s):
    """
    >>> s = set(['a', 'b', 'c'])
    >>> numbers, values = set_iter_enumerate(s)
    >>> sorted(numbers)
    [0, 1, 2]
    >>> sorted(values)
    ['a', 'b', 'c']
    """
    cdef int i
    numbers = []
    values = []
    for i, x in enumerate(s):
        numbers.append(i)
        values.append(x)
    return numbers, values