cython

Форк
0
/
inlined_generator_expressions.pyx 
220 строк · 6.4 Кб
1

2
cimport cython
3

4
## def range_tuple_genexp(int N):
5
##     """
6
##     >>> range_tuple_genexp(5)
7
##     (0, 1, 2, 3, 4)
8
##     """
9
##     return tuple(i for i in range(N))
10

11

12
@cython.test_assert_path_exists('//ForFromStatNode',
13
                                "//InlinedGeneratorExpressionNode")
14
@cython.test_fail_if_path_exists('//SimpleCallNode',
15
                                 '//ForInStatNode')
16
def range_sum(int N):
17
    """
18
    >>> sum(range(10))
19
    45
20
    >>> range_sum(10)
21
    45
22
    """
23
    result = sum(i for i in range(N))
24
    return result
25

26

27
@cython.test_assert_path_exists('//ForFromStatNode',
28
                                "//InlinedGeneratorExpressionNode")
29
@cython.test_fail_if_path_exists('//SimpleCallNode',
30
                                 '//CoerceFromPyTypeNode//InlinedGeneratorExpressionNode',
31
                                 '//ForInStatNode')
32
def range_sum_typed(int N):
33
    """
34
    >>> sum(range(10))
35
    45
36
    >>> range_sum_typed(10)
37
    45
38
    """
39
    cdef int result = sum(i for i in range(N))
40
    return result
41

42

43
@cython.test_assert_path_exists('//ForFromStatNode',
44
                                "//InlinedGeneratorExpressionNode",
45
                                "//ReturnStatNode//InlinedGeneratorExpressionNode",
46
                                "//ReturnStatNode//CoerceToPyTypeNode//InlinedGeneratorExpressionNode")
47
@cython.test_fail_if_path_exists('//SimpleCallNode',
48
                                 '//CoerceFromPyTypeNode//InlinedGeneratorExpressionNode',
49
                                 '//TypecastNode//InlinedGeneratorExpressionNode',
50
                                 '//ForInStatNode')
51
def return_range_sum_cast(int N):
52
    """
53
    >>> sum(range(10))
54
    45
55
    >>> return_range_sum_cast(10)
56
    45
57
    """
58
    return <int>sum(i for i in range(N))
59

60

61
@cython.test_assert_path_exists('//ForFromStatNode',
62
                                "//InlinedGeneratorExpressionNode")
63
@cython.test_fail_if_path_exists('//SimpleCallNode',
64
                                 '//ForInStatNode')
65
def return_range_sum(int N):
66
    """
67
    >>> sum(range(10))
68
    45
69
    >>> return_range_sum(10)
70
    45
71
    """
72
    return sum(i for i in range(N))
73

74

75
@cython.test_assert_path_exists('//ForFromStatNode',
76
                                "//InlinedGeneratorExpressionNode")
77
@cython.test_fail_if_path_exists('//SimpleCallNode',
78
                                 '//ForInStatNode')
79
def return_range_sum_squares(int N):
80
    """
81
    >>> sum([i*i for i in range(10)])
82
    285
83
    >>> return_range_sum_squares(10)
84
    285
85

86
    >>> print(sum([i*i for i in range(10000)]))
87
    333283335000
88
    >>> print(return_range_sum_squares(10000))
89
    333283335000
90
    """
91
    return sum(i*i for i in range(N))
92

93

94
@cython.test_assert_path_exists('//ForInStatNode',
95
                                "//InlinedGeneratorExpressionNode")
96
@cython.test_fail_if_path_exists('//SimpleCallNode')
97
def return_sum_squares(seq):
98
    """
99
    >>> sum([i*i for i in range(10)])
100
    285
101
    >>> return_sum_squares(range(10))
102
    285
103

104
    >>> print(sum([i*i for i in range(10000)]))
105
    333283335000
106
    >>> print(return_sum_squares(range(10000)))
107
    333283335000
108
    """
109
    return sum(i*i for i in seq)
110

111

112
@cython.test_assert_path_exists('//ForInStatNode',
113
                                "//InlinedGeneratorExpressionNode")
114
@cython.test_fail_if_path_exists('//SimpleCallNode')
115
def return_sum_squares_start(seq, int start):
116
    """
117
    >>> sum([i*i for i in range(10)], -1)
118
    284
119
    >>> return_sum_squares_start(range(10), -1)
120
    284
121

122
    >>> print(sum([i*i for i in range(10000)], 9))
123
    333283335009
124
    >>> print(return_sum_squares_start(range(10000), 9))
125
    333283335009
126
    """
127
    return sum((i*i for i in seq), start)
128

129

130
@cython.test_assert_path_exists(
131
    '//ForInStatNode',
132
    "//InlinedGeneratorExpressionNode")
133
@cython.test_fail_if_path_exists(
134
    '//SimpleCallNode',
135
    "//InlinedGeneratorExpressionNode//CoerceToPyTypeNode")
136
def return_typed_sum_squares_start(seq, int start):
137
    """
138
    >>> sum([i*i for i in range(10)], -1)
139
    284
140
    >>> return_typed_sum_squares_start(range(10), -1)
141
    284
142

143
    >>> print(sum([i*i for i in range(1000)], 9))
144
    332833509
145
    >>> print(return_typed_sum_squares_start(range(1000), 9))
146
    332833509
147
    """
148
    cdef int i
149
    return <int>sum((i*i for i in seq), start)
150

151

152
@cython.test_assert_path_exists('//ForInStatNode',
153
                                "//InlinedGeneratorExpressionNode")
154
@cython.test_fail_if_path_exists('//SimpleCallNode')
155
def return_sum_of_listcomp_consts_start(seq, int start):
156
    """
157
    >>> sum([1 for i in range(10) if i > 3], -1)
158
    5
159
    >>> return_sum_of_listcomp_consts_start(range(10), -1)
160
    5
161

162
    >>> print(sum([1 for i in range(10000) if i > 3], 9))
163
    10005
164
    >>> print(return_sum_of_listcomp_consts_start(range(10000), 9))
165
    10005
166
    """
167
    return sum([1 for i in seq if i > 3], start)
168

169

170
@cython.test_assert_path_exists('//ForInStatNode',
171
                                "//InlinedGeneratorExpressionNode",
172
                                # the next test is for a deficiency
173
                                # (see InlinedGeneratorExpressionNode.coerce_to()),
174
                                # hope this breaks one day
175
                                "//CoerceFromPyTypeNode//InlinedGeneratorExpressionNode")
176
@cython.test_fail_if_path_exists('//SimpleCallNode')
177
def return_typed_sum_of_listcomp_consts_start(seq, int start):
178
    """
179
    >>> sum([1 for i in range(10) if i > 3], -1)
180
    5
181
    >>> return_typed_sum_of_listcomp_consts_start(range(10), -1)
182
    5
183

184
    >>> print(sum([1 for i in range(10000) if i > 3], 9))
185
    10005
186
    >>> print(return_typed_sum_of_listcomp_consts_start(range(10000), 9))
187
    10005
188
    """
189
    return <int>sum([1 for i in seq if i > 3], start)
190

191

192
@cython.test_assert_path_exists(
193
    '//ForInStatNode',
194
    "//InlinedGeneratorExpressionNode")
195
@cython.test_fail_if_path_exists(
196
    '//SimpleCallNode',
197
    "//InlinedGeneratorExpressionNode//CoerceToPyTypeNode")
198
def return_typed_sum_cond_exp(seq):
199
    """
200
    >>> return_typed_sum_cond_exp([1,2,3,4])
201
    2
202
    """
203
    cdef int i
204
    return <int>sum( 0 if i%2 else 1
205
                     for i in seq )
206

207

208
@cython.test_assert_path_exists(
209
    '//ForInStatNode',
210
    "//InlinedGeneratorExpressionNode")
211
@cython.test_fail_if_path_exists(
212
    '//SimpleCallNode',
213
    "//InlinedGeneratorExpressionNode//CoerceToPyTypeNode")
214
def return_typed_sum_cond_exp_in(seq):
215
    """
216
    >>> return_typed_sum_cond_exp_in([1,2,3,4,5,6,7,8,9])
217
    3
218
    """
219
    cdef int i
220
    return <int>sum( 0 if i%3 in (0,1) else 1
221
                     for i in seq )
222

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

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

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

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