cython

Форк
0
/
builtin_sorted.pyx 
94 строки · 2.0 Кб
1
cimport cython
2

3

4
def generator():
5
    yield 2
6
    yield 1
7
    yield 3
8

9

10
def returns_set():
11
    return {"foo", "bar", "baz"}
12

13

14
def returns_tuple():
15
    return (1, 2, 3, 0)
16

17

18
@cython.test_fail_if_path_exists("//SimpleCallNode")
19
def sorted_arg(x):
20
    """
21
    >>> a = [3, 2, 1]
22
    >>> sorted_arg(a)
23
    [1, 2, 3]
24
    >>> a
25
    [3, 2, 1]
26
    >>> sorted(generator())
27
    [1, 2, 3]
28
    >>> sorted(returns_set())
29
    ['bar', 'baz', 'foo']
30
    >>> sorted(returns_tuple())
31
    [0, 1, 2, 3]
32
    >>> sorted(object())
33
    Traceback (most recent call last):
34
    TypeError: 'object' object is not iterable
35
    """
36
    return sorted(x)
37

38

39
@cython.test_assert_path_exists("//GeneralCallNode")
40
def sorted_arg_with_key(x):
41
    """
42
    >>> a = [3, 2, 1]
43
    >>> sorted_arg_with_key(a)
44
    [3, 2, 1]
45
    >>> a
46
    [3, 2, 1]
47
    >>> sorted_arg_with_key(generator())
48
    [3, 2, 1]
49
    >>> sorted_arg_with_key(returns_tuple())
50
    [3, 2, 1, 0]
51
    >>> sorted_arg_with_key(object())
52
    Traceback (most recent call last):
53
    TypeError: 'object' object is not iterable
54
    """
55
    return sorted(x, key=lambda x: -x)
56

57

58
@cython.test_fail_if_path_exists("//YieldExprNode",
59
                                 "//NoneCheckNode")
60
@cython.test_assert_path_exists("//InlinedGeneratorExpressionNode")
61
def sorted_genexp():
62
    """
63
    >>> sorted_genexp()
64
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
65
    """
66
    return sorted(i*i for i in range(10,0,-1))
67

68

69
@cython.test_fail_if_path_exists("//SimpleCallNode//SimpleCallNode")
70
@cython.test_assert_path_exists("//SimpleCallNode/NameNode[@name = 'range']")
71
def sorted_list_of_range():
72
    """
73
    >>> sorted_list_of_range()
74
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
75
    """
76
    return sorted(list(range(10,0,-1)))
77

78

79
@cython.test_fail_if_path_exists("//SimpleCallNode")
80
def sorted_list_literal():
81
    """
82
    >>> sorted_list_literal()
83
    [1, 1, 2, 2, 3, 3]
84
    """
85
    return sorted([3, 1, 2] * 2)
86

87

88
@cython.test_fail_if_path_exists("//SimpleCallNode")
89
def sorted_tuple_literal():
90
    """
91
    >>> sorted_tuple_literal()
92
    [1, 1, 2, 2, 3, 3]
93
    """
94
    return sorted((1, 3, 2) * 2)
95

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

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

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

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