cython

Форк
0
/
carray_coercion.pyx 
438 строк · 10.6 Кб
1
# mode: run
2

3
from libc cimport stdint
4
from libc.stdint cimport int16_t as my_int16_t
5

6

7
def from_int_array():
8
    """
9
    >>> from_int_array()
10
    [1, 2, 3]
11
    """
12
    cdef int[3] v
13
    v[0] = 1
14
    v[1] = 2
15
    v[2] = 3
16
    return v
17

18

19
cpdef tuple tuple_from_int_array():
20
    """
21
    >>> tuple_from_int_array()
22
    (1, 2, 3)
23
    """
24
    cdef int[3] v
25
    v[0] = 1
26
    v[1] = 2
27
    v[2] = 3
28
    assert isinstance(<tuple>v, tuple)
29
    return v
30

31

32
cdef extern from "stdint.h":
33
    ctypedef unsigned long uint32_t
34

35

36
def from_typedef_int_array():
37
    """
38
    >>> from_typedef_int_array()
39
    [1, 2, 3]
40
    """
41
    cdef uint32_t[3] v
42
    v[0] = 1
43
    v[1] = 2
44
    v[2] = 3
45
    return v
46

47

48
cpdef tuple tuple_from_typedef_int_array():
49
    """
50
    >>> tuple_from_typedef_int_array()
51
    (1, 2, 3)
52
    """
53
    cdef uint32_t[3] v
54
    v[0] = 1
55
    v[1] = 2
56
    v[2] = 3
57
    return v
58

59

60
def from_cimported_int_array():
61
    """
62
    >>> from_cimported_int_array()
63
    [1, 2, 3]
64
    """
65
    cdef stdint.int32_t[3] v
66
    v[0] = 1
67
    v[1] = 2
68
    v[2] = 3
69
    return v
70

71

72
def from_cimported_as_int_array():
73
    """
74
    >>> from_cimported_as_int_array()
75
    [1, 2, 3]
76
    """
77
    cdef my_int16_t[3] v
78
    v[0] = 1
79
    v[1] = 2
80
    v[2] = 3
81
    return v
82

83

84
def from_int_array_array():
85
    """
86
    >>> from_int_array_array()
87
    [[11, 12, 13], [21, 22, 23]]
88
    """
89
    cdef int[2][3] v
90
    v[0][0] = 11
91
    v[0][1] = 12
92
    v[0][2] = 13
93
    v[1][0] = 21
94
    v[1][1] = 22
95
    v[1][2] = 23
96
    return v
97

98

99
def assign_int_array_array():
100
    """
101
    >>> assign_int_array_array()
102
    [[11, 12, 13], [21, 22, 23]]
103
    """
104
    cdef int[2][3] v = [[11, 12, 13], [21, 22, 23]]
105
    return v
106

107

108
def assign_int_array_array_from_tuples():
109
    """
110
    >>> assign_int_array_array_from_tuples()
111
    [[11, 12, 13], [21, 22, 23]]
112
    """
113
    cdef int[2][3] v = ([11, 12, 13], [21, 22, 23])
114
    return v
115

116

117
''' FIXME: this currently crashes:
118
def assign_int_array_array_from_tuples():
119
    """
120
    >>> assign_int_array_array_from_tuples()
121
    [[11, 12, 13], [21, 22, 23]]
122
    """
123
    cdef int[2][3] v = ((11, 12, 13), (21, 22, 23))
124
    return v
125
'''
126

127

128
def build_from_list_of_arrays():
129
    """
130
    >>> build_from_list_of_arrays()
131
    [[11, 12, 13], [21, 22, 23]]
132
    """
133
    cdef int[3] x = [11, 12, 13]
134
    cdef int[3] y = [21, 22, 23]
135
    cdef int[2][3] v = [x, y]
136
    return v
137

138

139
def build_from_tuple_of_arrays():
140
    """
141
    >>> build_from_tuple_of_arrays()
142
    [[11, 12, 13], [21, 22, 23]]
143
    """
144
    cdef int[3] x = [11, 12, 13]
145
    cdef int[3] y = [21, 22, 23]
146
    cdef int[2][3] v = (x, y)
147
    return v
148

149

150
ctypedef struct MyStructType:
151
    int x
152
    double y
153

154

155
cdef struct MyStruct:
156
    int x
157
    double y
158

159

160
def from_struct_array():
161
    """
162
    >>> a, b = from_struct_array()
163
    >>> a['x'], a['y']
164
    (1, 2.0)
165
    >>> b['x'], b['y']
166
    (3, 4.0)
167
    """
168
    cdef MyStructType[2] v
169
    cdef MyStruct[2] w
170
    v[0] = MyStructType(1, 2)
171
    v[1] = MyStructType(3, 4)
172
    assert isinstance(<tuple>v, tuple)
173
    assert isinstance(v, list)
174

175
    w[0] = MyStruct(1, 2)
176
    w[1] = MyStruct(3, 4)
177
    assert (<object>w) == v
178
    assert w == (<object>v)
179

180
    return v
181

182

183
def to_int_array(x):
184
    """
185
    >>> to_int_array([1, 2, 3])
186
    (1, 2, 3)
187
    >>> to_int_array([1, 2])
188
    Traceback (most recent call last):
189
    IndexError: not enough values found during array assignment, expected 3, got 2
190
    >>> to_int_array([1, 2, 3, 4])
191
    Traceback (most recent call last):
192
    IndexError: too many values found during array assignment, expected 3
193
    """
194
    cdef int[3] v = x
195
    return v[0], v[1], v[2]
196

197

198
def to_int_array_array(x):
199
    """
200
    >>> to_int_array_array([[1, 2, 3], [4, 5, 6]])
201
    (1, 2, 3, 4, 5, 6)
202
    >>> to_int_array_array(iter([[1, 2, 3], [4, 5, 6]]))
203
    (1, 2, 3, 4, 5, 6)
204

205
    >>> to_int_array_array([[1, 2, 3]])
206
    Traceback (most recent call last):
207
    IndexError: not enough values found during array assignment, expected 2, got 1
208
    >>> to_int_array_array(iter([[1, 2, 3]]))
209
    Traceback (most recent call last):
210
    IndexError: not enough values found during array assignment, expected 2, got 1
211

212
    >>> to_int_array_array([[1, 2, 3], [4, 5]])
213
    Traceback (most recent call last):
214
    IndexError: not enough values found during array assignment, expected 3, got 2
215
    >>> to_int_array_array(iter([[1, 2, 3], [4, 5]]))
216
    Traceback (most recent call last):
217
    IndexError: not enough values found during array assignment, expected 3, got 2
218

219
    >>> to_int_array_array([[1, 2, 3, 4], [5, 6, 7]])
220
    Traceback (most recent call last):
221
    IndexError: too many values found during array assignment, expected 3
222
    >>> to_int_array_array(iter([[1, 2, 3, 4], [5, 6, 7]]))
223
    Traceback (most recent call last):
224
    IndexError: too many values found during array assignment, expected 3
225
    """
226
    cdef int[2][3] v = x
227
    return v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2]
228

229

230
'''
231
# FIXME: this isn't currently allowed
232
cdef enum:
233
    SIZE_A = 2
234
    SIZE_B = 3
235

236
def to_int_array_array_enumsize(x):
237
    """
238
    >>> to_int_array_array([[1, 2, 3], [4, 5, 6]])
239
    (1, 2, 3, 4, 5, 6)
240
    >>> to_int_array_array(iter([[1, 2, 3], [4, 5, 6]]))
241
    (1, 2, 3, 4, 5, 6)
242
    >>> to_int_array([1, 2])
243
    Traceback (most recent call last):
244
    IndexError: not enough values found during array assignment, expected 3, got 2
245
    >>> to_int_array([1, 2, 3, 4])
246
    Traceback (most recent call last):
247
    IndexError: too many values found during array assignment, expected 3
248
    """
249
    cdef int[SIZE_A][SIZE_B] v = x
250
    return v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2]
251
'''
252

253

254
'''
255
# FIXME: this isn't currently supported
256
def array_as_argument(int[2] x):
257
    """
258
    >>> array_as_argument([1, 2])
259
    (1, 2)
260
    """
261
    return x[0], x[1]
262
'''
263

264

265
def to_int_array_slice(x):
266
    """
267
    >>> to_int_array_slice([1, 2, 3])
268
    (1, 2, 3)
269
    >>> to_int_array_slice([1, 2])
270
    Traceback (most recent call last):
271
    IndexError: not enough values found during array assignment, expected 3, got 2
272
    >>> to_int_array_slice([1, 2, 3, 4])
273
    Traceback (most recent call last):
274
    IndexError: too many values found during array assignment, expected 3
275
    """
276
    cdef int[3] v
277
    v[:] = x[:3]
278
    assert v[0] == x[0]
279
    assert v[1] == x[1]
280
    assert v[2] == x[2]
281
    v[:3] = [0, 0, 0]
282
    assert v[0] == 0
283
    assert v[1] == 0
284
    assert v[2] == 0
285
    v[:] = x
286
    return v[0], v[1], v[2]
287

288

289
def iterable_to_int_array(x):
290
    """
291
    >>> iterable_to_int_array(iter([1, 2, 3]))
292
    (1, 2, 3)
293
    >>> iterable_to_int_array(iter([1, 2]))
294
    Traceback (most recent call last):
295
    IndexError: not enough values found during array assignment, expected 3, got 2
296
    >>> iterable_to_int_array(iter([1, 2, 3, 4]))
297
    Traceback (most recent call last):
298
    IndexError: too many values found during array assignment, expected 3
299
    """
300
    cdef int[3] v
301
    v[:] = x
302
    return v[0], v[1], v[2]
303

304

305
def to_struct_array(x):
306
    """
307
    >>> a, b = to_struct_array(({'x': 1, 'y': 2}, {'x': 3, 'y': 4}))
308
    >>> a['x'], a['y']
309
    (1, 2.0)
310
    >>> b['x'], b['y']
311
    (3, 4.0)
312
    """
313
    cdef MyStructType[2] v
314
    v[:] = x
315

316
    cdef MyStruct[2] w
317
    w[:] = x
318

319
    assert w[0].x == v[0].x
320
    assert w[0].y == v[0].y
321
    assert w[1].x == v[1].x
322
    assert w[1].y == v[1].y
323

324
    return v[0], w[1]
325

326

327
def to_struct_array_array(x):
328
    """
329
    >>> (a1, a2, a3), (b1, b2, b3) = to_struct_array_array([
330
    ...     ({'x': 11, 'y': 12}, {'x': 13, 'y': 14}, {'x': 15, 'y': 16}),
331
    ...     ({'x': 21, 'y': 22}, {'x': 23, 'y': 24}, {'x': 25, 'y': 26}),
332
    ... ])
333
    >>> a1['x'], a1['y']
334
    (11, 12.0)
335
    >>> b3['x'], b3['y']
336
    (25, 26.0)
337
    """
338
    cdef MyStructType[2][3] v = x
339
    return v[0], v[1]
340

341

342
cdef struct StructWithArray:
343
    int a
344
    MyStruct[2] b
345

346

347
def to_struct_with_array(x):
348
    """
349
    >>> x, y = to_struct_with_array([
350
    ...     {'a': 11, 'b': [{'x': 12, 'y': 13}, {'x': 14, 'y': 15}]},
351
    ...     {'a': 21, 'b': [{'x': 22, 'y': 23}, {'x': 24, 'y': 25}]},
352
    ... ])
353
    >>> x['a'], y['a']
354
    (11, 21)
355
    >>> sorted(sorted(v.items()) for v in x['b'])
356
    [[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
357
    >>> sorted(sorted(v.items()) for v in y['b'])
358
    [[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
359

360
    >>> x, y = to_struct_with_array(iter([
361
    ...     {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
362
    ...     {'a': 21, 'b': iter([{'x': 22, 'y': 23}, {'x': 24, 'y': 25}])},
363
    ... ]))
364
    >>> x['a'], y['a']
365
    (11, 21)
366
    >>> sorted(sorted(v.items()) for v in x['b'])
367
    [[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
368
    >>> sorted(sorted(v.items()) for v in y['b'])
369
    [[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
370
    """
371
    cdef StructWithArray[2] v
372
    v = x
373
    return v
374

375

376
def to_struct_with_array_slice(x):
377
    """
378
    >>> x, y = to_struct_with_array_slice([
379
    ...     {'a': 11, 'b': [{'x': 12, 'y': 13}, {'x': 14, 'y': 15}]},
380
    ...     {'a': 21, 'b': [{'x': 22, 'y': 23}, {'x': 24, 'y': 25}]},
381
    ... ])
382
    >>> x['a'], y['a']
383
    (11, 21)
384
    >>> sorted(sorted(v.items()) for v in x['b'])
385
    [[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
386
    >>> sorted(sorted(v.items()) for v in y['b'])
387
    [[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
388

389
    >>> x, y = to_struct_with_array_slice(iter([
390
    ...     {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
391
    ...     {'a': 21, 'b': iter([{'x': 22, 'y': 23}, {'x': 24, 'y': 25}])},
392
    ... ]))
393
    >>> x['a'], y['a']
394
    (11, 21)
395
    >>> sorted(sorted(v.items()) for v in x['b'])
396
    [[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
397
    >>> sorted(sorted(v.items()) for v in y['b'])
398
    [[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
399
    """
400
    cdef StructWithArray[2] v
401
    v[:] = x
402
    return v
403

404

405
'''
406
# FIXME: this isn't currently allowed
407
def to_struct_with_array_slice_end(x):
408
    """
409
    >>> to_struct_with_array_slice_end([
410
    ...     {'a': 11, 'b': [{'x': 12, 'y': 13}, {'x': 14, 'y': 15}]},
411
    ... ])
412
    [{'a': 11, 'b': [{'y': 13.0, 'x': 12}, {'y': 15.0, 'x': 14}]}]
413
    >>> to_struct_with_array_slice_end(iter([
414
    ...     {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
415
    ... ]))
416
    [{'a': 11, 'b': [{'y': 13.0, 'x': 12}, {'y': 15.0, 'x': 14}]}]
417
    >>> to_struct_with_array_slice_end(iter([
418
    ...     {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
419
    ...     {'a': 21, 'b': iter([{'x': 22, 'y': 23}, {'x': 24, 'y': 25}])},
420
    ... ]))
421
    Traceback (most recent call last):
422
    IndexError: too many values found during array assignment, expected 1
423
    """
424
    cdef StructWithArray[2] v
425
    v[:1] = x
426
    return v
427

428

429
def to_int_array_slice_start_end(x):
430
    """
431
    >>> to_int_array_slice_start_end([1, 2, 3])
432
    (1, 2, 3, 2, 3)
433
    """
434
    cdef int[5] v
435
    v[2:] = x
436
    v[:3] = x
437
    return v[0], v[1], v[2], v[3], v[4]
438
'''
439

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

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

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

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