notinop.pyx 5.75 KB
Newer Older
1 2 3

cimport cython

4 5
def f(a,b):
    """
Stefan Behnel's avatar
Stefan Behnel committed
6 7 8 9 10 11
    >>> f(1,[1,2,3])
    False
    >>> f(5,[1,2,3])
    True
    >>> f(2,(1,2,3))
    False
12 13 14
    """
    result = a not in b
    return result
Stefan Behnel's avatar
Stefan Behnel committed
15

16 17
def g(a,b):
    """
Stefan Behnel's avatar
Stefan Behnel committed
18 19 20 21 22 23
    >>> g(1,[1,2,3])
    0
    >>> g(5,[1,2,3])
    1
    >>> g(2,(1,2,3))
    0
24 25 26 27
    """
    cdef int result
    result = a not in b
    return result
Stefan Behnel's avatar
Stefan Behnel committed
28

29 30
def h(b):
    """
Stefan Behnel's avatar
Stefan Behnel committed
31 32 33 34
    >>> h([1,2,3,4])
    False
    >>> h([1,3,4])
    True
35 36 37
    """
    result = 2 not in b
    return result
Stefan Behnel's avatar
Stefan Behnel committed
38

39 40
def j(b):
    """
Stefan Behnel's avatar
Stefan Behnel committed
41 42 43 44
    >>> j([1,2,3,4])
    0
    >>> j([1,3,4])
    1
45 46 47 48
    """
    cdef int result
    result = 2 not in b
    return result
49

50
@cython.test_fail_if_path_exists("//SwitchStatNode")
51 52
def k(a):
    """
53 54 55 56
    >>> k(1)
    0
    >>> k(5)
    1
57 58 59
    """
    cdef int result = a not in [1,2,3,4]
    return result
60

61 62 63
@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def m_list(int a):
64
    """
65
    >>> m_list(2)
66
    0
67
    >>> m_list(5)
68
    1
69 70 71
    """
    cdef int result = a not in [1,2,3,4]
    return result
72

73 74 75 76 77 78 79 80 81 82 83 84
@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def m_tuple(int a):
    """
    >>> m_tuple(2)
    0
    >>> m_tuple(5)
    1
    """
    cdef int result = a not in (1,2,3,4)
    return result

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def m_set(int a):
    """
    >>> m_set(2)
    0
    >>> m_set(5)
    1
    """
    cdef int result = a not in {1,2,3,4}
    return result

cdef bytes bytes_string = b'abcdefg'

@cython.test_assert_path_exists("//PrimaryCmpNode")
@cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode")
def m_bytes(char a):
    """
    >>> m_bytes(ord('f'))
    0
    >>> m_bytes(ord('X'))
    1
    """
    cdef int result = a not in bytes_string
    return result

@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def m_bytes_literal(char a):
    """
    >>> m_bytes_literal(ord('f'))
    0
    >>> m_bytes_literal(ord('X'))
    1
    """
    cdef int result = a not in b'abcdefg'
    return result

cdef unicode unicode_string = u'abcdefg\u1234\uF8D2'

@cython.test_assert_path_exists("//PrimaryCmpNode")
@cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode")
def m_unicode(Py_UNICODE a):
    """
    >>> m_unicode(ord('f'))
    0
    >>> m_unicode(ord('X'))
    1
    """
    cdef int result = a not in unicode_string
    return result

@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def m_unicode_literal(Py_UNICODE a):
    """
    >>> m_unicode_literal(ord('f'))
    0
    >>> m_unicode_literal(ord('X'))
    1
    """
    cdef int result = a not in u'abcdefg\u1234\uF8D2'
    return result

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
@cython.test_assert_path_exists("//SwitchStatNode", "//BoolBinopNode")
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def m_tuple_in_or_notin(int a):
    """
    >>> m_tuple_in_or_notin(2)
    0
    >>> m_tuple_in_or_notin(3)
    1
    >>> m_tuple_in_or_notin(5)
    1
    """
    cdef int result = a not in (1,2,3,4) or a in (3,4)
    return result

@cython.test_assert_path_exists("//SwitchStatNode", "//BoolBinopNode")
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def m_tuple_notin_or_notin(int a):
    """
    >>> m_tuple_notin_or_notin(2)
    1
    >>> m_tuple_notin_or_notin(6)
    1
    >>> m_tuple_notin_or_notin(4)
    0
    """
    cdef int result = a not in (1,2,3,4) or a not in (4,5)
    return result

@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def m_tuple_notin_and_notin(int a):
    """
    >>> m_tuple_notin_and_notin(2)
    0
    >>> m_tuple_notin_and_notin(6)
    0
    >>> m_tuple_notin_and_notin(5)
    1
    """
    cdef int result = a not in (1,2,3,4) and a not in (6,7)
    return result

@cython.test_assert_path_exists("//SwitchStatNode", "//BoolBinopNode")
@cython.test_fail_if_path_exists("//PrimaryCmpNode")
def m_tuple_notin_and_notin_overlap(int a):
    """
    >>> m_tuple_notin_and_notin_overlap(2)
    0
    >>> m_tuple_notin_and_notin_overlap(4)
    0
    >>> m_tuple_notin_and_notin_overlap(5)
    1
    """
    cdef int result = a not in (1,2,3,4) and a not in (3,4)
    return result

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def conditional_int(int a):
    """
    >>> conditional_int(1)
    2
    >>> conditional_int(0)
    1
    >>> conditional_int(5)
    1
    """
    return 1 if a not in (1,2,3,4) else 2

@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def conditional_object(int a):
    """
    >>> conditional_object(1)
    '2'
    >>> conditional_object(0)
    1
    >>> conditional_object(5)
    1
    """
    return 1 if a not in (1,2,3,4) else '2'

@cython.test_assert_path_exists("//SwitchStatNode")
@cython.test_fail_if_path_exists("//BoolBinopNode", "//PrimaryCmpNode")
def conditional_none(int a):
    """
    >>> conditional_none(1)
    1
    >>> conditional_none(0)
    >>> conditional_none(5)
    """
    return None if a not in {1,2,3,4} else 1

242 243
def n(a):
    """
244 245 246 247
    >>> n('d *')
    0
    >>> n('xxx')
    1
248 249 250
    """
    cdef int result = a.lower() not in [u'a *',u'b *',u'c *',u'd *']
    return result
Stefan Behnel's avatar
Stefan Behnel committed
251

252 253
def p(a):
    """
Stefan Behnel's avatar
Stefan Behnel committed
254 255 256 257
    >>> p('a')
    0
    >>> p(1)
    1
258
    """
Stefan Behnel's avatar
Stefan Behnel committed
259 260 261 262 263
    cdef dict d = {u'a': 1, u'b': 2}
    cdef int result = a not in d
    return result

def q(a):
264 265 266 267 268
    """
    >>> q(1)
    Traceback (most recent call last):
    TypeError: 'NoneType' object is not iterable
    """
Stefan Behnel's avatar
Stefan Behnel committed
269 270 271
    cdef dict d = None
    cdef int result = a not in d # should fail with a TypeError
    return result