unpack.pyx 9.12 KB
Newer Older
1 2 3 4 5 6 7 8 9
# mode: run
# tag: sequence_unpacking

_set = set

def _it(N):
    for i in range(N):
        yield i

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

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

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

Stefan Behnel's avatar
Stefan Behnel committed
39
    >>> a,b,c = None   # doctest: +ELLIPSIS
40
    Traceback (most recent call last):
Stefan Behnel's avatar
Stefan Behnel committed
41 42
    TypeError: ...
    >>> unpack_tuple(None)   # doctest: +ELLIPSIS
43
    Traceback (most recent call last):
Stefan Behnel's avatar
Stefan Behnel committed
44
    TypeError: ...
45 46 47 48 49 50 51 52 53
    """
    a,b,c = it
    return a,b,c

def unpack_list(list it):
    """
    >>> unpack_list([1,2,3])
    (1, 2, 3)

Stefan Behnel's avatar
Stefan Behnel committed
54
    >>> a,b,c = None   # doctest: +ELLIPSIS
55
    Traceback (most recent call last):
Stefan Behnel's avatar
Stefan Behnel committed
56 57
    TypeError: ...
    >>> unpack_list(None)   # doctest: +ELLIPSIS
58
    Traceback (most recent call last):
Stefan Behnel's avatar
Stefan Behnel committed
59
    TypeError: ...
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    """
    a,b,c = it
    return a,b,c

def unpack_to_itself(it):
    """
    >>> it = _it(2)
    >>> it, it = it
    >>> it
    1
    >>> unpack_to_itself([1,2])
    2
    >>> unpack_to_itself((1,2))
    2
    >>> unpack_to_itself(_it(2))
    1
    >>> unpack_to_itself((1,2,3))
    Traceback (most recent call last):
    ValueError: too many values to unpack (expected 2)
    >>> unpack_to_itself(_it(3))
    Traceback (most recent call last):
    ValueError: too many values to unpack (expected 2)
    """
    it, it = it
    return it

def unpack_partial(it):
    """
    >>> it = _it(2)
    >>> a = b = c = 0
    >>> a,b,c = it
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    >>> a, b, c
    (0, 0, 0)
    >>> unpack_partial([1,2])
    (0, 0, 0)
    >>> unpack_partial((1,2))
    (0, 0, 0)
    >>> unpack_partial(_it(2))
    (0, 0, 0)

    >>> it = ItCount([1,2])
    >>> a = b = c = 0
    >>> a,b,c = it
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    >>> a, b, c
    (0, 0, 0)
    >>> it.count
    3
    >>> it = ItCount([1,2])
    >>> unpack_partial(it)
    (0, 0, 0)
    >>> it.count
    3
    """
    a = b = c = 0
    try:
        a, b, c = it
    except ValueError:
        pass
    return a, b, c

def unpack_fail_assignment(it):
    """
    >>> it = ItCount([1, 2, 3])
    >>> a = b = c = 0
    >>> try: a, b[0], c = it
    ... except TypeError: pass
    >>> a,b,c
    (1, 0, 0)
    >>> it.count
    4
    >>> it = ItCount([1, 2, 3])
    >>> unpack_fail_assignment(it)
    (1, 0, 0)
    >>> it.count
    4
    """
    cdef object a,b,c
    a = b = c = 0
    try:
        a, b[0], c = it
    except TypeError:
        pass
    return a, b, c

def unpack_partial_typed(it):
    """
    >>> unpack_partial_typed([1, 2, 'abc'])
    (0, 0, 0)
    >>> unpack_partial_typed((1, 'abc', 3))
    (0, 0, 0)
    >>> unpack_partial_typed(_set([1, 'abc', 3]))
    (0, 0, 0)

    >>> it = ItCount([1, 'abc', 3])
    >>> unpack_partial_typed(it)
    (0, 0, 0)
    >>> it.count
    4
    """
    cdef int a,b,c
    a = b = c = 0
    try:
        a, b, c = it
    except TypeError:
        pass
    return a, b, c

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

183 184 185
    >>> unpack_typed((1, None, [1]))
    Traceback (most recent call last):
    TypeError: a float is required
186 187 188 189 190 191 192 193 194 195
    >>> unpack_typed([1, None, [1]])
    Traceback (most recent call last):
    TypeError: a float is required
    >>> it = ItCount([1, None, [1]])
    >>> unpack_typed(it)
    Traceback (most recent call last):
    TypeError: a float is required
    >>> it.count
    4

196 197 198
    >>> unpack_typed((1, 2.0, (1,)))
    Traceback (most recent call last):
    TypeError: Expected list, got tuple
199 200 201 202 203 204
    >>> it = ItCount([1, 2.0, (1,)])
    >>> unpack_typed(it)
    Traceback (most recent call last):
    TypeError: Expected list, got tuple
    >>> it.count
    4
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 242
    """
    cdef int a
    cdef float b
    cdef list c
    a,b,c = it
    return a,b,c

def failure_too_many(it):
    """
    >>> a,b,c = [1,2,3,4]    # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ValueError: too many values to unpack...
    >>> failure_too_many([1,2,3,4])
    Traceback (most recent call last):
    ValueError: too many values to unpack (expected 3)

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

    >>> a,b,c = _set([1,2,3,4])    # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ValueError: too many values to unpack...
    >>> failure_too_many(_set([1,2,3,4]))
    Traceback (most recent call last):
    ValueError: too many values to unpack (expected 3)

    >>> a,b,c = _it(4)    # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ValueError: too many values to unpack...
    >>> failure_too_many(_it(4))
    Traceback (most recent call last):
    ValueError: too many values to unpack (expected 3)
    """
    a,b,c = it
243
    return a,b,c
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

def failure_too_few(it):
    """
    >>> a,b,c = [1,2]
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    >>> failure_too_few([1,2])
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack

    >>> a,b,c = (1,2)
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    >>> failure_too_few((1,2))
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack

    >>> a,b,c = _set([1,2])
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    >>> failure_too_few(_set([1,2]))
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack

    >>> a,b,c = _it(2)
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    >>> failure_too_few(_it(2))
    Traceback (most recent call last):
    ValueError: need more than 2 values to unpack
    """
    a,b,c = it
276
    return a,b,c
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

def _it_failure(N):
    for i in range(N):
        yield i
    raise ValueError("huhu")

def failure_while_unpacking(it):
    """
    >>> a,b,c = _it_failure(0)
    Traceback (most recent call last):
    ValueError: huhu
    >>> failure_while_unpacking(_it_failure(0))
    Traceback (most recent call last):
    ValueError: huhu

    >>> a,b,c = _it_failure(1)
    Traceback (most recent call last):
    ValueError: huhu
    >>> failure_while_unpacking(_it_failure(1))
    Traceback (most recent call last):
    ValueError: huhu

    >>> a,b,c = _it_failure(2)
    Traceback (most recent call last):
    ValueError: huhu
    >>> failure_while_unpacking(_it_failure(2))
    Traceback (most recent call last):
    ValueError: huhu

    >>> a,b,c = _it_failure(3)
    Traceback (most recent call last):
    ValueError: huhu
    >>> failure_while_unpacking(_it_failure(3))
    Traceback (most recent call last):
    ValueError: huhu

    >>> a,b,c = _it_failure(4)    # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ValueError: too many values to unpack...
    >>> failure_while_unpacking(_it_failure(4))
    Traceback (most recent call last):
    ValueError: too many values to unpack (expected 3)
    """
    a,b,c = it
321
    return a,b,c
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

def unpack_many(it):
    """
    >>> items = range(1,13)
    >>> unpack_many(items)
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    >>> unpack_many(iter(items))
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    >>> unpack_many(list(items))
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    >>> unpack_many(tuple(items))
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    """
    a,b,c,d,e,f,g,h,i,j,k,l = it
    return a,b,c,d,e,f,g,h,i,j,k,l

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

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

def unpack_many_int(it):
    """
    >>> items = range(1,13)
    >>> unpack_many_int(items)
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    >>> unpack_many_int(iter(items))
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    >>> unpack_many_int(list(items))
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    >>> unpack_many_int(tuple(items))
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    """
    cdef int b
    cdef long f
    cdef Py_ssize_t h
    a,b,c,d,e,f,g,h,i,j,k,l = it
    return a,b,c,d,e,f,g,h,i,j,k,l