cython

Форк
0
/
unpack.pyx 
405 строк · 9.4 Кб
1
# mode: run
2
# tag: sequence_unpacking
3

4
import cython
5

6

7
def _it(N):
8
    for i in range(N):
9
        yield i
10

11

12
cdef class ItCount(object):
13
    cdef object values
14
    cdef readonly count
15
    def __init__(self, values):
16
        self.values = iter(values)
17
        self.count = 0
18
    def __iter__(self):
19
        return self
20
    def __next__(self):
21
        self.count += 1
22
        return next(self.values)
23

24
def kunterbunt(obj1, obj2, obj3, obj4, obj5):
25
    """
26
    >>> kunterbunt(1, (2,), (3,4,5), (6,(7,(8,9))), 0)
27
    (8, 9, (8, 9), (6, (7, (8, 9))), 0)
28
    """
29
    obj1, = obj2
30
    obj1, obj2 = obj2 + obj2
31
    obj1, obj2, obj3 = obj3
32
    obj1, (obj2, obj3) = obj4
33
    [obj1, obj2] = obj3
34
    return obj1, obj2, obj3, obj4, obj5
35

36
def unpack_tuple(tuple it):
37
    """
38
    >>> unpack_tuple((1,2,3))
39
    (1, 2, 3)
40

41
    >>> a,b,c = None   # doctest: +ELLIPSIS
42
    Traceback (most recent call last):
43
    TypeError: ...
44
    >>> unpack_tuple(None)   # doctest: +ELLIPSIS
45
    Traceback (most recent call last):
46
    TypeError: ...
47
    """
48
    a,b,c = it
49
    return a,b,c
50

51
def unpack_list(list it):
52
    """
53
    >>> unpack_list([1,2,3])
54
    (1, 2, 3)
55

56
    >>> a,b,c = None   # doctest: +ELLIPSIS
57
    Traceback (most recent call last):
58
    TypeError: ...
59
    >>> unpack_list(None)   # doctest: +ELLIPSIS
60
    Traceback (most recent call last):
61
    TypeError: ...
62
    """
63
    a,b,c = it
64
    return a,b,c
65

66
def unpack_to_itself(it):
67
    """
68
    >>> it = _it(2)
69
    >>> it, it = it
70
    >>> it
71
    1
72
    >>> unpack_to_itself([1,2])
73
    2
74
    >>> unpack_to_itself((1,2))
75
    2
76
    >>> unpack_to_itself(_it(2))
77
    1
78
    >>> unpack_to_itself((1,2,3))
79
    Traceback (most recent call last):
80
    ValueError: too many values to unpack (expected 2)
81
    >>> unpack_to_itself(_it(3))
82
    Traceback (most recent call last):
83
    ValueError: too many values to unpack (expected 2)
84
    """
85
    it, it = it
86
    return it
87

88
def unpack_partial(it):
89
    """
90
    >>> it = _it(2)
91
    >>> a = b = c = 0
92
    >>> try: a,b,c = it
93
    ... except ValueError: pass
94
    ... else: print("DID NOT FAIL!")
95
    >>> a, b, c
96
    (0, 0, 0)
97
    >>> unpack_partial([1,2])
98
    (0, 0, 0)
99
    >>> unpack_partial((1,2))
100
    (0, 0, 0)
101
    >>> unpack_partial(_it(2))
102
    (0, 0, 0)
103

104
    >>> it = ItCount([1,2])
105
    >>> a = b = c = 0
106
    >>> try: a,b,c = it
107
    ... except ValueError: pass
108
    ... else: print("DID NOT FAIL!")
109
    >>> a, b, c
110
    (0, 0, 0)
111
    >>> it.count
112
    3
113
    >>> it = ItCount([1,2])
114
    >>> unpack_partial(it)
115
    (0, 0, 0)
116
    >>> it.count
117
    3
118
    """
119
    a = b = c = 0
120
    try:
121
        a, b, c = it
122
    except ValueError:
123
        pass
124
    return a, b, c
125

126
def unpack_fail_assignment(it):
127
    """
128
    >>> it = ItCount([1, 2, 3])
129
    >>> a = b = c = 0
130
    >>> try: a, b[0], c = it
131
    ... except TypeError: pass
132
    >>> a,b,c
133
    (1, 0, 0)
134
    >>> it.count
135
    4
136
    >>> it = ItCount([1, 2, 3])
137
    >>> unpack_fail_assignment(it)
138
    (1, 0, 0)
139
    >>> it.count
140
    4
141
    """
142
    cdef object a,b,c
143
    a = b = c = 0
144
    try:
145
        a, b[0], c = it
146
    except TypeError:
147
        pass
148
    return a, b, c
149

150
def unpack_partial_typed(it):
151
    """
152
    >>> unpack_partial_typed([1, 2, 'abc'])
153
    (0, 0, 0)
154
    >>> unpack_partial_typed((1, 'abc', 3))
155
    (0, 0, 0)
156
    >>> unpack_partial_typed(set([1, 'abc', 3]))
157
    (0, 0, 0)
158

159
    >>> it = ItCount([1, 'abc', 3])
160
    >>> unpack_partial_typed(it)
161
    (0, 0, 0)
162
    >>> it.count
163
    4
164
    """
165
    cdef int a,b,c
166
    a = b = c = 0
167
    try:
168
        a, b, c = it
169
    except TypeError:
170
        pass
171
    return a, b, c
172

173
def unpack_typed(it):
174
    """
175
    >>> unpack_typed((1, 2.0, [1]))
176
    (1, 2.0, [1])
177
    >>> unpack_typed([1, 2.0, [1]])
178
    (1, 2.0, [1])
179
    >>> it = ItCount([1, 2.0, [1]])
180
    >>> unpack_typed(it)
181
    (1, 2.0, [1])
182
    >>> it.count
183
    4
184

185
    >>> try: unpack_typed((1, None, [1]))
186
    ... except TypeError: pass
187
    >>> try: unpack_typed([1, None, [1]])
188
    ... except TypeError: pass
189
    >>> it = ItCount([1, None, [1]])
190
    >>> try: unpack_typed(it)
191
    ... except TypeError: pass
192
    >>> it.count
193
    4
194

195
    >>> unpack_typed((1, 2.0, (1,)))
196
    Traceback (most recent call last):
197
    TypeError: Expected list, got tuple
198
    >>> it = ItCount([1, 2.0, (1,)])
199
    >>> unpack_typed(it)
200
    Traceback (most recent call last):
201
    TypeError: Expected list, got tuple
202
    >>> it.count
203
    4
204
    """
205
    cdef int a
206
    cdef float b
207
    cdef list c
208
    a,b,c = it
209
    return a,b,c
210

211
def failure_too_many(it):
212
    """
213
    >>> try: a,b,c = [1,2,3,4]
214
    ... except ValueError: pass
215
    >>> failure_too_many([1,2,3,4])
216
    Traceback (most recent call last):
217
    ValueError: too many values to unpack (expected 3)
218

219
    >>> try: a,b,c = [1,2,3,4]
220
    ... except ValueError: pass
221
    >>> failure_too_many((1,2,3,4))
222
    Traceback (most recent call last):
223
    ValueError: too many values to unpack (expected 3)
224

225
    >>> a,b,c = set([1,2,3,4])    # doctest: +ELLIPSIS
226
    Traceback (most recent call last):
227
    ValueError: too many values to unpack...
228
    >>> failure_too_many(set([1,2,3,4]))
229
    Traceback (most recent call last):
230
    ValueError: too many values to unpack (expected 3)
231

232
    >>> a,b,c = _it(4)    # doctest: +ELLIPSIS
233
    Traceback (most recent call last):
234
    ValueError: too many values to unpack...
235
    >>> failure_too_many(_it(4))
236
    Traceback (most recent call last):
237
    ValueError: too many values to unpack (expected 3)
238
    """
239
    a,b,c = it
240
    return a,b,c
241

242

243
def failure_too_few(it):
244
    """
245
    >>> try: a,b,c = [1,2]
246
    ... except ValueError: pass
247
    >>> failure_too_few([1,2])
248
    Traceback (most recent call last):
249
    ValueError: need more than 2 values to unpack
250

251
    >>> try: a,b,c = (1,2)
252
    ... except ValueError: pass
253
    >>> failure_too_few((1,2))
254
    Traceback (most recent call last):
255
    ValueError: need more than 2 values to unpack
256

257
    >>> try: a,b,c = set([1,2])
258
    ... except ValueError: pass
259
    ... else: print("DID NOT FAIL!")
260
    >>> failure_too_few(set([1,2]))
261
    Traceback (most recent call last):
262
    ValueError: need more than 2 values to unpack
263

264
    >>> try: a,b,c = _it(2)
265
    ... except ValueError: pass
266
    ... else: print("DID NOT FAIL!")
267
    >>> failure_too_few(_it(2))
268
    Traceback (most recent call last):
269
    ValueError: need more than 2 values to unpack
270
    """
271
    a,b,c = it
272
    return a,b,c
273

274

275
def _it_failure(N):
276
    for i in range(N):
277
        yield i
278
    raise ValueError("huhu")
279

280
def failure_while_unpacking(it):
281
    """
282
    >>> a,b,c = _it_failure(0)
283
    Traceback (most recent call last):
284
    ValueError: huhu
285
    >>> failure_while_unpacking(_it_failure(0))
286
    Traceback (most recent call last):
287
    ValueError: huhu
288

289
    >>> a,b,c = _it_failure(1)
290
    Traceback (most recent call last):
291
    ValueError: huhu
292
    >>> failure_while_unpacking(_it_failure(1))
293
    Traceback (most recent call last):
294
    ValueError: huhu
295

296
    >>> a,b,c = _it_failure(2)
297
    Traceback (most recent call last):
298
    ValueError: huhu
299
    >>> failure_while_unpacking(_it_failure(2))
300
    Traceback (most recent call last):
301
    ValueError: huhu
302

303
    >>> a,b,c = _it_failure(3)
304
    Traceback (most recent call last):
305
    ValueError: huhu
306
    >>> failure_while_unpacking(_it_failure(3))
307
    Traceback (most recent call last):
308
    ValueError: huhu
309

310
    >>> a,b,c = _it_failure(4)    # doctest: +ELLIPSIS
311
    Traceback (most recent call last):
312
    ValueError: too many values to unpack...
313
    >>> failure_while_unpacking(_it_failure(4))
314
    Traceback (most recent call last):
315
    ValueError: too many values to unpack (expected 3)
316
    """
317
    a,b,c = it
318
    return a,b,c
319

320
def unpack_many(it):
321
    """
322
    >>> items = range(1,13)
323
    >>> unpack_many(items)
324
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
325
    >>> unpack_many(iter(items))
326
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
327
    >>> unpack_many(list(items))
328
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
329
    >>> unpack_many(tuple(items))
330
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
331
    """
332
    a,b,c,d,e,f,g,h,i,j,k,l = it
333
    return a,b,c,d,e,f,g,h,i,j,k,l
334

335
def unpack_many_tuple(tuple it):
336
    """
337
    >>> items = range(1,13)
338
    >>> unpack_many_tuple(tuple(items))
339
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
340
    """
341
    a,b,c,d,e,f,g,h,i,j,k,l = it
342
    return a,b,c,d,e,f,g,h,i,j,k,l
343

344
def unpack_many_list(list it):
345
    """
346
    >>> items = range(1,13)
347
    >>> unpack_many_list(list(items))
348
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
349
    """
350
    a,b,c,d,e,f,g,h,i,j,k,l = it
351
    return a,b,c,d,e,f,g,h,i,j,k,l
352

353
def unpack_many_int(it):
354
    """
355
    >>> items = range(1,13)
356
    >>> unpack_many_int(items)
357
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
358
    >>> unpack_many_int(iter(items))
359
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
360
    >>> unpack_many_int(list(items))
361
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
362
    >>> unpack_many_int(tuple(items))
363
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
364
    """
365
    cdef int b
366
    cdef long f
367
    cdef Py_ssize_t h
368
    a,b,c,d,e,f,g,h,i,j,k,l = it
369
    return a,b,c,d,e,f,g,h,i,j,k,l
370

371

372
@cython.test_fail_if_path_exists('//PyTypeTestNode')
373
def unpack_literal_none_to_builtin_type():
374
    """
375
    >>> unpack_literal_none_to_builtin_type()
376
    (None, None, None, None)
377
    """
378
    cdef list a,b,c,d
379
    a, b = c, d = None, None
380
    return a,b,c,d
381

382

383
cdef class ExtType:
384
    pass
385

386

387
@cython.test_fail_if_path_exists('//PyTypeTestNode')
388
def unpack_literal_none_to_exttype():
389
    """
390
    >>> unpack_literal_none_to_exttype()
391
    (None, None, None, None)
392
    """
393
    cdef ExtType a,b,c,d
394
    a, b = c, d = None, None
395
    return a,b,c,d
396

397

398
# Github issue #1523
399
def test_unpack_resultref():
400
    """
401
    >>> test_unpack_resultref() == ((1, set()), 1, set())
402
    True
403
    """
404
    a = b, c = 1, set()
405
    return a, b, c
406

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

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

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

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