cython

Форк
0
/
any.pyx 
295 строк · 5.3 Кб
1

2
cdef class VerboseGetItem(object):
3
    cdef object sequence
4
    def __init__(self, seq):
5
        self.sequence = seq
6
    def __getitem__(self, i):
7
        print i
8
        return self.sequence[i] # may raise IndexError
9

10

11
cimport cython
12

13
@cython.test_assert_path_exists("//SimpleCallNode")
14
@cython.test_fail_if_path_exists("//ForInStatNode")
15
def any_item(x):
16
    """
17
    >>> any_item([0,0,1,0,0])
18
    True
19
    >>> any_item([0,0,0,0,1])
20
    True
21
    >>> any_item([0,0,0,0,0])
22
    False
23

24
    >>> any(VerboseGetItem([0,0,1,0,0]))
25
    0
26
    1
27
    2
28
    True
29
    >>> any_item(VerboseGetItem([0,0,1,0,0]))
30
    0
31
    1
32
    2
33
    True
34

35
    >>> any(VerboseGetItem([0,0,0,0,0]))
36
    0
37
    1
38
    2
39
    3
40
    4
41
    5
42
    False
43
    >>> any_item(VerboseGetItem([0,0,0,0,0]))
44
    0
45
    1
46
    2
47
    3
48
    4
49
    5
50
    False
51
    """
52
    return any(x)
53

54

55
@cython.test_assert_path_exists(
56
    "//ForInStatNode",
57
    "//InlinedGeneratorExpressionNode"
58
)
59
@cython.test_fail_if_path_exists(
60
    "//SimpleCallNode",
61
    "//YieldExprNode"
62
)
63
def any_in_simple_gen(seq):
64
    """
65
    >>> any_in_simple_gen([0,1,0])
66
    True
67
    >>> any_in_simple_gen([0,0,0])
68
    False
69

70
    >>> any_in_simple_gen(VerboseGetItem([0,0,1,0,0]))
71
    0
72
    1
73
    2
74
    True
75
    >>> any_in_simple_gen(VerboseGetItem([0,0,0,0,0]))
76
    0
77
    1
78
    2
79
    3
80
    4
81
    5
82
    False
83
    """
84
    return any(x for x in seq)
85

86

87
@cython.test_assert_path_exists(
88
    "//ForInStatNode",
89
    "//InlinedGeneratorExpressionNode"
90
)
91
@cython.test_fail_if_path_exists(
92
    "//SimpleCallNode",
93
    "//YieldExprNode"
94
)
95
def any_in_simple_gen_scope(seq):
96
    """
97
    >>> any_in_simple_gen_scope([0,1,0])
98
    True
99
    >>> any_in_simple_gen_scope([0,0,0])
100
    False
101

102
    >>> any_in_simple_gen_scope(VerboseGetItem([0,0,1,0,0]))
103
    0
104
    1
105
    2
106
    True
107
    >>> any_in_simple_gen_scope(VerboseGetItem([0,0,0,0,0]))
108
    0
109
    1
110
    2
111
    3
112
    4
113
    5
114
    False
115
    """
116
    x = 'abc'
117
    result = any(x for x in seq)
118
    assert x == 'abc'
119
    return result
120

121

122
@cython.test_assert_path_exists(
123
    "//ForInStatNode",
124
    "//InlinedGeneratorExpressionNode"
125
)
126
@cython.test_fail_if_path_exists(
127
    "//SimpleCallNode",
128
    "//YieldExprNode"
129
)
130
def any_in_conditional_gen(seq):
131
    """
132
    >>> any_in_conditional_gen([3,6,9])
133
    False
134
    >>> any_in_conditional_gen([0,3,7])
135
    True
136
    >>> any_in_conditional_gen([1,0,1])
137
    True
138

139
    >>> any_in_conditional_gen(VerboseGetItem([0,0,3,0,0]))
140
    0
141
    1
142
    2
143
    3
144
    4
145
    5
146
    False
147
    >>> any_in_conditional_gen(VerboseGetItem([0,3,0,1,1]))
148
    0
149
    1
150
    2
151
    3
152
    True
153
    """
154
    return any(x%3 for x in seq if x%2 == 1)
155

156
mixed_ustring = u'AbcDefGhIjKlmnoP'
157
lower_ustring = mixed_ustring.lower()
158
upper_ustring = mixed_ustring.upper()
159

160

161
@cython.test_assert_path_exists(
162
    '//PythonCapiCallNode',
163
    '//ForFromStatNode',
164
    "//InlinedGeneratorExpressionNode"
165
)
166
@cython.test_fail_if_path_exists(
167
    '//SimpleCallNode',
168
    '//ForInStatNode'
169
)
170
def any_lower_case_characters(unicode ustring):
171
    """
172
    >>> any_lower_case_characters(upper_ustring)
173
    False
174
    >>> any_lower_case_characters(mixed_ustring)
175
    True
176
    >>> any_lower_case_characters(lower_ustring)
177
    True
178
    """
179
    return any(uchar.islower() for uchar in ustring)
180

181

182
@cython.test_assert_path_exists(
183
    "//ForInStatNode",
184
    "//InlinedGeneratorExpressionNode",
185
    "//InlinedGeneratorExpressionNode//IfStatNode"
186
)
187
@cython.test_fail_if_path_exists(
188
    "//SimpleCallNode",
189
    "//YieldExprNode",
190
#    "//IfStatNode//CoerceToBooleanNode"
191
)
192
def any_in_typed_gen(seq):
193
    """
194
    >>> any_in_typed_gen([0,1,0])
195
    True
196
    >>> any_in_typed_gen([0,0,0])
197
    False
198

199
    >>> any_in_typed_gen(VerboseGetItem([0,0,1,0,0]))
200
    0
201
    1
202
    2
203
    True
204
    >>> any_in_typed_gen(VerboseGetItem([0,0,0,0,0]))
205
    0
206
    1
207
    2
208
    3
209
    4
210
    5
211
    False
212
    """
213
    cdef int x
214
    return any(x for x in seq)
215

216

217
@cython.test_assert_path_exists(
218
    "//ForInStatNode",
219
    "//InlinedGeneratorExpressionNode",
220
    "//InlinedGeneratorExpressionNode//IfStatNode"
221
)
222
@cython.test_fail_if_path_exists(
223
    "//SimpleCallNode",
224
    "//YieldExprNode"
225
)
226
def any_in_gen_builtin_name(seq):
227
    """
228
    >>> any_in_gen_builtin_name([0,1,0])
229
    True
230
    >>> any_in_gen_builtin_name([0,0,0])
231
    False
232

233
    >>> any_in_gen_builtin_name(VerboseGetItem([0,0,1,0,0]))
234
    0
235
    1
236
    2
237
    True
238
    >>> any_in_gen_builtin_name(VerboseGetItem([0,0,0,0,0]))
239
    0
240
    1
241
    2
242
    3
243
    4
244
    5
245
    False
246
    """
247
    return any(type for type in seq)
248

249

250
@cython.test_assert_path_exists(
251
    "//ForInStatNode",
252
    "//InlinedGeneratorExpressionNode",
253
    "//InlinedGeneratorExpressionNode//IfStatNode"
254
)
255
@cython.test_fail_if_path_exists(
256
    "//SimpleCallNode",
257
    "//YieldExprNode",
258
#    "//IfStatNode//CoerceToBooleanNode"
259
)
260
def any_in_double_gen(seq):
261
    """
262
    >>> any(x for L in [[0,0,0],[0,0,1],[0,0,0]] for x in L)
263
    True
264
    >>> any_in_double_gen([[0,0,0],[0,0,1],[0,0,0]])
265
    True
266

267
    >>> any(x for L in [[0,0,0],[0,0,0],[0,0,0]] for x in L)
268
    False
269
    >>> any_in_double_gen([[0,0,0],[0,0,0],[0,0,0]])
270
    False
271

272
    >>> any_in_double_gen([VerboseGetItem([0,0,0]), VerboseGetItem([0,0,1,0,0])])
273
    0
274
    1
275
    2
276
    3
277
    0
278
    1
279
    2
280
    True
281
    >>> any_in_double_gen([VerboseGetItem([0,0,0]),VerboseGetItem([0,0]),VerboseGetItem([0,0,0])])
282
    0
283
    1
284
    2
285
    3
286
    0
287
    1
288
    2
289
    0
290
    1
291
    2
292
    3
293
    False
294
    """
295
    cdef int x
296
    return any(x for L in seq for x in L)
297

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

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

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

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