ch02: exception handling in example

This commit is contained in:
Luciano Ramalho 2019-04-06 18:06:32 -03:00
parent 57966cfd53
commit e2122fbcdc

View File

@ -663,20 +663,33 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"By design, this example raises an exception::\n",
"\n",
"```python\n",
">>> l[2:5] = 100\n",
"Traceback (most recent call last):\n",
" ...\n",
"TypeError: can only assign an iterable\n",
"```"
"By design, this example raises an exception::"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TypeError('can only assign an iterable')\n"
]
}
],
"source": [
"try:\n",
" l[2:5] = 100\n",
"except TypeError as e:\n",
" print(repr(e))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
@ -684,7 +697,7 @@
"[0, 1, 100, 22, 9]"
]
},
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@ -703,7 +716,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@ -712,7 +725,7 @@
"[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]"
]
},
"execution_count": 26,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@ -724,7 +737,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@ -733,7 +746,7 @@
"'abcdabcdabcdabcdabcd'"
]
},
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@ -758,7 +771,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@ -767,7 +780,7 @@
"[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]"
]
},
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
@ -779,7 +792,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@ -788,7 +801,7 @@
"[['_', '_', '_'], ['_', '_', 'X'], ['_', '_', '_']]"
]
},
"execution_count": 29,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
@ -807,7 +820,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"outputs": [
{
@ -816,7 +829,7 @@
"[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]"
]
},
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
@ -828,7 +841,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@ -837,7 +850,7 @@
"[['_', '_', 'O'], ['_', '_', 'O'], ['_', '_', 'O']]"
]
},
"execution_count": 31,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@ -856,7 +869,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 33,
"metadata": {},
"outputs": [
{
@ -865,7 +878,7 @@
"[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]"
]
},
"execution_count": 32,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
@ -880,7 +893,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"outputs": [
{
@ -889,7 +902,7 @@
"[['_', '_', '_'], ['_', '_', '_'], ['X', '_', '_']]"
]
},
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
@ -908,16 +921,16 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140413155609544"
"139840389979848"
]
},
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
@ -929,7 +942,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 36,
"metadata": {},
"outputs": [
{
@ -938,7 +951,7 @@
"[1, 2, 3, 1, 2, 3]"
]
},
"execution_count": 35,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
@ -950,16 +963,16 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140413155609544"
"139840389979848"
]
},
"execution_count": 36,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
@ -970,16 +983,16 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140413154489904"
"139840321336880"
]
},
"execution_count": 37,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
@ -991,16 +1004,16 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140413265327592"
"139840440497640"
]
},
"execution_count": 38,
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
@ -1019,7 +1032,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@ -1040,7 +1053,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 41,
"metadata": {},
"outputs": [
{
@ -1049,7 +1062,7 @@
"(1, 2, [30, 40, 50, 60])"
]
},
"execution_count": 40,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@ -1067,7 +1080,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 42,
"metadata": {},
"outputs": [
{
@ -1102,7 +1115,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@ -1111,7 +1124,7 @@
"['apple', 'banana', 'grape', 'raspberry']"
]
},
"execution_count": 42,
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
@ -1123,7 +1136,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@ -1132,7 +1145,7 @@
"['grape', 'raspberry', 'apple', 'banana']"
]
},
"execution_count": 43,
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
@ -1143,7 +1156,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 45,
"metadata": {},
"outputs": [
{
@ -1152,7 +1165,7 @@
"['raspberry', 'grape', 'banana', 'apple']"
]
},
"execution_count": 44,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
@ -1163,7 +1176,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 46,
"metadata": {},
"outputs": [
{
@ -1172,7 +1185,7 @@
"['grape', 'apple', 'banana', 'raspberry']"
]
},
"execution_count": 45,
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
@ -1183,7 +1196,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 47,
"metadata": {},
"outputs": [
{
@ -1192,7 +1205,7 @@
"['raspberry', 'banana', 'grape', 'apple']"
]
},
"execution_count": 46,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
@ -1203,7 +1216,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 48,
"metadata": {},
"outputs": [
{
@ -1212,7 +1225,7 @@
"['grape', 'raspberry', 'apple', 'banana']"
]
},
"execution_count": 47,
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
@ -1223,7 +1236,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 49,
"metadata": {},
"outputs": [
{
@ -1232,7 +1245,7 @@
"['apple', 'banana', 'grape', 'raspberry']"
]
},
"execution_count": 48,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
@ -1258,7 +1271,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 50,
"metadata": {},
"outputs": [
{
@ -1292,20 +1305,20 @@
"ROW_FMT = '{0:2d} @ {1:2d} {2}{0:<2d}'\n",
"\n",
"def demo(haystack, needles, bisect_fn):\n",
" print('DEMO:', bisect_fn.__name__) # <5>\n",
" print('DEMO:', bisect_fn.__name__) # <1>\n",
" print('haystack ->', ' '.join('%2d' % n for n in haystack))\n",
" for needle in reversed(needles):\n",
" position = bisect_fn(haystack, needle) # <1>\n",
" offset = position * ' |' # <2>\n",
" print(ROW_FMT.format(needle, position, offset)) # <3>\n",
" position = bisect_fn(haystack, needle) # <2>\n",
" offset = position * ' |' # <3>\n",
" print(ROW_FMT.format(needle, position, offset)) # <4>\n",
"\n",
"demo(HAYSTACK, NEEDLES, bisect.bisect)\n",
"demo(HAYSTACK, NEEDLES, bisect.bisect) # <5>\n",
"# END BISECT_DEMO"
]
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 51,
"metadata": {},
"outputs": [
{
@ -1341,7 +1354,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 52,
"metadata": {},
"outputs": [
{
@ -1350,7 +1363,7 @@
"['F', 'D', 'D', 'C', 'C', 'B', 'B', 'A', 'A']"
]
},
"execution_count": 51,
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
@ -1372,7 +1385,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 53,
"metadata": {},
"outputs": [
{
@ -1381,7 +1394,7 @@
"['F', 'F', 'D', 'D', 'C', 'C', 'B', 'B', 'A']"
]
},
"execution_count": 52,
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
@ -1403,7 +1416,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 54,
"metadata": {},
"outputs": [
{
@ -1459,7 +1472,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 55,
"metadata": {},
"outputs": [
{
@ -1468,7 +1481,7 @@
"0.5963321947530882"
]
},
"execution_count": 54,
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
@ -1483,7 +1496,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
@ -1493,7 +1506,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 57,
"metadata": {},
"outputs": [
{
@ -1502,7 +1515,7 @@
"0.5963321947530882"
]
},
"execution_count": 56,
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
@ -1518,7 +1531,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 58,
"metadata": {},
"outputs": [
{
@ -1527,7 +1540,7 @@
"True"
]
},
"execution_count": 57,
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
@ -1552,7 +1565,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 59,
"metadata": {},
"outputs": [
{
@ -1561,7 +1574,7 @@
"5"
]
},
"execution_count": 58,
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
@ -1574,7 +1587,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 60,
"metadata": {},
"outputs": [
{
@ -1583,7 +1596,7 @@
"-2"
]
},
"execution_count": 59,
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
@ -1594,7 +1607,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 61,
"metadata": {},
"outputs": [
{
@ -1603,7 +1616,7 @@
"[254, 255, 255, 255, 0, 0, 1, 0, 2, 0]"
]
},
"execution_count": 60,
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
@ -1615,7 +1628,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 62,
"metadata": {},
"outputs": [
{
@ -1624,7 +1637,7 @@
"array('h', [-2, -1, 1024, 1, 2])"
]
},
"execution_count": 61,
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
@ -1650,7 +1663,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 63,
"metadata": {},
"outputs": [
{
@ -1659,7 +1672,7 @@
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"execution_count": 62,
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
@ -1672,7 +1685,7 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 64,
"metadata": {},
"outputs": [
{
@ -1681,7 +1694,7 @@
"numpy.ndarray"
]
},
"execution_count": 63,
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
@ -1692,7 +1705,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 65,
"metadata": {},
"outputs": [
{
@ -1701,7 +1714,7 @@
"(12,)"
]
},
"execution_count": 64,
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
@ -1712,7 +1725,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 66,
"metadata": {},
"outputs": [
{
@ -1723,7 +1736,7 @@
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 65,
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
@ -1735,7 +1748,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 67,
"metadata": {},
"outputs": [
{
@ -1744,7 +1757,7 @@
"array([ 8, 9, 10, 11])"
]
},
"execution_count": 66,
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
@ -1755,7 +1768,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 68,
"metadata": {},
"outputs": [
{
@ -1764,7 +1777,7 @@
"9"
]
},
"execution_count": 67,
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
@ -1775,7 +1788,7 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 69,
"metadata": {},
"outputs": [
{
@ -1784,7 +1797,7 @@
"array([1, 5, 9])"
]
},
"execution_count": 68,
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
@ -1795,7 +1808,7 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 70,
"metadata": {},
"outputs": [
{
@ -1807,7 +1820,7 @@
" [ 3, 7, 11]])"
]
},
"execution_count": 69,
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
@ -1825,7 +1838,7 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
@ -1836,7 +1849,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
@ -1845,7 +1858,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 73,
"metadata": {},
"outputs": [
{
@ -1854,7 +1867,7 @@
"array([0.29150425, 0.33893554, 0.08112756])"
]
},
"execution_count": 72,
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
@ -1865,7 +1878,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 74,
"metadata": {},
"outputs": [
{
@ -1874,7 +1887,7 @@
"array([0.14575213, 0.16946777, 0.04056378])"
]
},
"execution_count": 73,
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
@ -1886,16 +1899,16 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0065746790060075"
"0.00606649300607387"
]
},
"execution_count": 74,
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
@ -1908,7 +1921,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
@ -1919,7 +1932,7 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 77,
"metadata": {},
"outputs": [
{
@ -1928,7 +1941,7 @@
"memmap([0.29150425, 0.33893554, 0.08112756])"
]
},
"execution_count": 76,
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
@ -1953,7 +1966,7 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 78,
"metadata": {},
"outputs": [
{
@ -1962,7 +1975,7 @@
"deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 77,
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
@ -1976,7 +1989,7 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 79,
"metadata": {},
"outputs": [
{
@ -1985,7 +1998,7 @@
"deque([7, 8, 9, 0, 1, 2, 3, 4, 5, 6])"
]
},
"execution_count": 78,
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
@ -1997,7 +2010,7 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 80,
"metadata": {},
"outputs": [
{
@ -2006,7 +2019,7 @@
"deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])"
]
},
"execution_count": 79,
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
@ -2018,7 +2031,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 81,
"metadata": {},
"outputs": [
{
@ -2027,7 +2040,7 @@
"deque([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 80,
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
@ -2039,7 +2052,7 @@
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": 82,
"metadata": {},
"outputs": [
{
@ -2048,7 +2061,7 @@
"deque([3, 4, 5, 6, 7, 8, 9, 11, 22, 33])"
]
},
"execution_count": 81,
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
@ -2060,7 +2073,7 @@
},
{
"cell_type": "code",
"execution_count": 82,
"execution_count": 83,
"metadata": {},
"outputs": [
{
@ -2069,7 +2082,7 @@
"deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])"
]
},
"execution_count": 82,
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
@ -2095,7 +2108,7 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
@ -2104,7 +2117,7 @@
},
{
"cell_type": "code",
"execution_count": 84,
"execution_count": 85,
"metadata": {},
"outputs": [
{
@ -2131,7 +2144,7 @@
},
{
"cell_type": "code",
"execution_count": 85,
"execution_count": 86,
"metadata": {},
"outputs": [
{
@ -2140,7 +2153,7 @@
"[0, '1', 5, 6, '9', 14, 19, '23', 28, '28']"
]
},
"execution_count": 85,
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
@ -2153,7 +2166,7 @@
},
{
"cell_type": "code",
"execution_count": 86,
"execution_count": 87,
"metadata": {},
"outputs": [
{
@ -2162,7 +2175,7 @@
"[0, '1', 14, 19, '23', 28, '28', 5, 6, '9']"
]
},
"execution_count": 86,
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}