cython

Форк
0
/
slice2.pyx 
144 строки · 2.9 Кб
1
# mode: run
2
# tag: list, slice, slicing
3

4
def test_full(seq):
5
    """
6
    >>> l = [1,2,3,4]
7
    >>> test_full(l)
8
    [1, 2, 3, 4]
9
    >>> l == test_full(l)
10
    True
11
    >>> l is test_full(l)
12
    False
13
    >>> try: test_full(42)
14
    ... except TypeError: pass
15
    """
16
    obj = seq[:]
17
    return obj
18

19
def test_start(seq, start):
20
    """
21
    >>> l = [1,2,3,4]
22
    >>> test_start(l, 2)
23
    [3, 4]
24
    >>> test_start(l, 3)
25
    [4]
26
    >>> test_start(l, 4)
27
    []
28
    >>> test_start(l, 8)
29
    []
30
    >>> test_start(l, -3)
31
    [2, 3, 4]
32
    >>> test_start(l, -4)
33
    [1, 2, 3, 4]
34
    >>> test_start(l, -8)
35
    [1, 2, 3, 4]
36
    >>> test_start(l, 0)
37
    [1, 2, 3, 4]
38
    >>> test_start(l, None)
39
    [1, 2, 3, 4]
40
    >>> try: test_start(42, 2, 3)
41
    ... except TypeError: pass
42
    """
43
    obj = seq[start:]
44
    return obj
45

46
def test_stop(seq, stop):
47
    """
48
    >>> l = [1,2,3,4]
49
    >>> test_stop(l, 3)
50
    [1, 2, 3]
51
    >>> test_stop(l, -1)
52
    [1, 2, 3]
53
    >>> test_stop(l, -3)
54
    [1]
55
    >>> test_stop(l, -4)
56
    []
57
    >>> test_stop(l, -8)
58
    []
59
    >>> test_stop(l, 0)
60
    []
61
    >>> test_stop(l, None)
62
    [1, 2, 3, 4]
63
    >>> try: test_stop(42, 3)
64
    ... except TypeError: pass
65
    """
66
    obj = seq[:stop]
67
    return obj
68

69
def test_step(seq, step):
70
    """
71
    >>> l = [1,2,3,4]
72
    >>> test_step(l, -1)
73
    [4, 3, 2, 1]
74
    >>> test_step(l, 1)
75
    [1, 2, 3, 4]
76
    >>> test_step(l, 2)
77
    [1, 3]
78
    >>> test_step(l, 3)
79
    [1, 4]
80
    >>> test_step(l, -3)
81
    [4, 1]
82
    >>> test_step(l, None)
83
    [1, 2, 3, 4]
84
    >>> try: test_step(l, 0)
85
    ... except ValueError: pass
86
    ...
87
    >>> try: test_step(42, 0)
88
    ... except TypeError: pass
89
    ...
90
    """
91
    obj = seq[::step]
92
    return obj
93

94
def test_start_and_stop(seq, start, stop):
95
    """
96
    >>> l = [1,2,3,4]
97
    >>> test_start_and_stop(l, 2, 3)
98
    [3]
99
    >>> test_start_and_stop(l, -3, -1)
100
    [2, 3]
101
    >>> test_start_and_stop(l, None, None)
102
    [1, 2, 3, 4]
103
    >>> try: test_start_and_stop(42, 2, 3)
104
    ... except TypeError: pass
105
    """
106
    obj = seq[start:stop]
107
    return obj
108

109
def test_start_stop_and_step(seq, start, stop, step):
110
    """
111
    >>> l = [1,2,3,4,5]
112
    >>> test_start_stop_and_step(l, 0, 5, 1)
113
    [1, 2, 3, 4, 5]
114
    >>> test_start_stop_and_step(l, 5, -1, -1)
115
    []
116
    >>> test_start_stop_and_step(l, 5, None, -1)
117
    [5, 4, 3, 2, 1]
118
    >>> test_start_stop_and_step(l, 2, 5, 2)
119
    [3, 5]
120
    >>> test_start_stop_and_step(l, -100, 100, 1)
121
    [1, 2, 3, 4, 5]
122
    >>> test_start_stop_and_step(l, None, None, None)
123
    [1, 2, 3, 4, 5]
124
    >>> try: test_start_stop_and_step(l, None, None, 0)
125
    ... except ValueError: pass
126
    ... 
127
    >>> try: test_start_stop_and_step(42, 1, 2, 3)
128
    ... except TypeError: pass
129
    """
130
    obj = seq[start:stop:step]
131
    return obj
132

133
class A(object):
134
    pass
135

136
def slice_of_temporary_smoketest():
137
    """
138
    >>> slice_of_temporary_smoketest()
139
    [3, 2]
140
    """
141
    x = A()
142
    x.a = [1, 2]
143
    x.a[:] = [3,2]
144
    return x.a
145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.