1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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
|
# mode: run
# tag: list, slice, slicing
def test_full(seq):
"""
>>> l = [1,2,3,4]
>>> test_full(l)
[1, 2, 3, 4]
>>> l == test_full(l)
True
>>> l is test_full(l)
False
>>> try: test_full(42)
... except TypeError: pass
"""
obj = seq[:]
return obj
def test_start(seq, start):
"""
>>> l = [1,2,3,4]
>>> test_start(l, 2)
[3, 4]
>>> test_start(l, 3)
[4]
>>> test_start(l, 4)
[]
>>> test_start(l, 8)
[]
>>> test_start(l, -3)
[2, 3, 4]
>>> test_start(l, -4)
[1, 2, 3, 4]
>>> test_start(l, -8)
[1, 2, 3, 4]
>>> test_start(l, 0)
[1, 2, 3, 4]
>>> test_start(l, None)
[1, 2, 3, 4]
>>> try: test_start(42, 2, 3)
... except TypeError: pass
"""
obj = seq[start:]
return obj
def test_stop(seq, stop):
"""
>>> l = [1,2,3,4]
>>> test_stop(l, 3)
[1, 2, 3]
>>> test_stop(l, -1)
[1, 2, 3]
>>> test_stop(l, -3)
[1]
>>> test_stop(l, -4)
[]
>>> test_stop(l, -8)
[]
>>> test_stop(l, 0)
[]
>>> test_stop(l, None)
[1, 2, 3, 4]
>>> try: test_stop(42, 3)
... except TypeError: pass
"""
obj = seq[:stop]
return obj
def test_step(seq, step):
"""
>>> l = [1,2,3,4]
>>> test_step(l, -1)
[4, 3, 2, 1]
>>> test_step(l, 1)
[1, 2, 3, 4]
>>> test_step(l, 2)
[1, 3]
>>> test_step(l, 3)
[1, 4]
>>> test_step(l, -3)
[4, 1]
>>> test_step(l, None)
[1, 2, 3, 4]
>>> try: test_step(l, 0)
... except ValueError: pass
...
>>> try: test_step(42, 0)
... except TypeError: pass
...
"""
obj = seq[::step]
return obj
def test_start_and_stop(seq, start, stop):
"""
>>> l = [1,2,3,4]
>>> test_start_and_stop(l, 2, 3)
[3]
>>> test_start_and_stop(l, -3, -1)
[2, 3]
>>> test_start_and_stop(l, None, None)
[1, 2, 3, 4]
>>> try: test_start_and_stop(42, 2, 3)
... except TypeError: pass
"""
obj = seq[start:stop]
return obj
def test_start_stop_and_step(seq, start, stop, step):
"""
>>> l = [1,2,3,4,5]
>>> test_start_stop_and_step(l, 0, 5, 1)
[1, 2, 3, 4, 5]
>>> test_start_stop_and_step(l, 5, -1, -1)
[]
>>> test_start_stop_and_step(l, 5, None, -1)
[5, 4, 3, 2, 1]
>>> test_start_stop_and_step(l, 2, 5, 2)
[3, 5]
>>> test_start_stop_and_step(l, -100, 100, 1)
[1, 2, 3, 4, 5]
>>> test_start_stop_and_step(l, None, None, None)
[1, 2, 3, 4, 5]
>>> try: test_start_stop_and_step(l, None, None, 0)
... except ValueError: pass
...
>>> try: test_start_stop_and_step(42, 1, 2, 3)
... except TypeError: pass
"""
obj = seq[start:stop:step]
return obj
class A(object):
pass
def slice_of_temporary_smoketest():
"""
>>> slice_of_temporary_smoketest()
[3, 2]
"""
x = A()
x.a = [1, 2]
x.a[:] = [3,2]
return x.a
|