cython

Форк
0
/
genexpr_iterable_lookup_T600.pyx 
100 строк · 2.3 Кб
1
# mode: run
2
# ticket: t600
3
# tag: genexpr
4
# cython: language_level=3
5

6
cimport cython
7

8
#@cython.test_assert_path_exists('//ComprehensionNode')
9
#@cython.test_fail_if_path_exists('//SimpleCallNode')
10
def list_genexpr_iterable_lookup():
11
    """
12
    >>> x = (0,1,2,3,4,5)
13
    >>> [ x*2 for x in x if x % 2 == 0 ]  # leaks in Py2 but finds the right 'x'
14
    [0, 4, 8]
15

16
    >>> list_genexpr_iterable_lookup()
17
    [0, 4, 8]
18
    """
19
    x = (0,1,2,3,4,5)
20
    result = list( x*2 for x in x if x % 2 == 0 )
21
    assert x == (0,1,2,3,4,5)
22
    return result
23

24

25
#@cython.test_assert_path_exists('//ComprehensionNode')
26
#@cython.test_fail_if_path_exists('//SingleAssignmentNode//SimpleCallNode')
27
def genexpr_iterable_in_closure():
28
    """
29
    >>> genexpr_iterable_in_closure()
30
    ['aa', 'cc']
31
    """
32
    x = 'abc'
33
    def f():
34
        return x
35
    result = list( x*2 for x in x if x != 'b' )
36
    assert x == 'abc' # don't leak in Py3 code
37
    assert f() == 'abc' # don't leak in Py3 code
38

39
    return result
40

41

42
def genexpr_over_complex_arg(func, L):
43
    """
44
    >>> class wrapper(object):
45
    ...     value = 5
46
    >>> genexpr_over_complex_arg(list, wrapper())
47
    [5]
48
    """
49
    return func(d for d in set([type(L).value, L.__class__.value, L.value]))
50

51

52
def listcomp():
53
    """
54
    >>> listcomp()
55
    [0, 1, 5, 8]
56
    """
57
    data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
58
    data.sort(key=lambda r: r[1])
59
    keys = [r[1] for r in data]
60
    return keys
61

62

63
def genexpr_in_listcomp(L):
64
    """
65
    >>> genexpr_in_listcomp( [[1,2,3]]*2 )
66
    [[1, 2, 3], [1, 2, 3]]
67
    """
68
    return list(d for d in [list(d for d in d) for d in L])
69

70

71
@cython.test_assert_path_exists('//ForFromStatNode')
72
def genexpr_range_in_listcomp(L):
73
    """
74
    >>> genexpr_range_in_listcomp( [1,2,3] )
75
    [[0], [0, 1], [0, 1, 2]]
76
    """
77
    cdef int z,d
78
    return [list(d for d in range(z)) for z in L]
79

80

81
@cython.test_fail_if_path_exists('//ForInStatNode')
82
def genexpr_in_dictcomp_dictiter():
83
    """
84
    >>> sorted(genexpr_in_dictcomp_dictiter())
85
    [1, 5]
86
    """
87
    d = {1:2, 3:4, 5:6}
88
    return {k:d for k,d in d.iteritems() if d != 4}
89

90

91
def genexpr_over_array_slice():
92
    """
93
    >>> list(genexpr_over_array_slice())
94
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
95
    """
96
    cdef double x[10]
97
    for i in range(10):
98
        x[i] = i
99
    cdef int n = 5
100
    return (n for n in x[:n+1])
101

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

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

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

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