ch02: automated tests

This commit is contained in:
Luciano Ramalho 2019-11-20 20:19:50 -03:00
parent 64e8bdcd79
commit 8a1b6fe145
6 changed files with 299 additions and 201 deletions

21
02-array-seq/README.md Normal file
View File

@ -0,0 +1,21 @@
# An Array of Sequences
Sample code for Chapter 2 of _Fluent Python 2e_ by Luciano Ramalho (O'Reilly, 2020)
## Running the tests
### Doctests
Use Python's standard ``doctest`` module, for example:
$ python3 -m doctest bisect_demo.py -v
### Jupyter Notebook
Install ``pytest`` and the ``nbval`` plugin:
$ pip install pytest nbval
Run:
$ pytest --nbval

View File

@ -1,4 +0,0 @@
Sample code for Chapter 2 - "An array of sequences"
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
http://shop.oreilly.com/product/0636920032519.do

View File

@ -923,21 +923,10 @@
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"139840389979848"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"l = [1, 2, 3]\n",
"id(l)"
"idl = id(l)"
]
},
{
@ -948,7 +937,7 @@
{
"data": {
"text/plain": [
"[1, 2, 3, 1, 2, 3]"
"4414271936"
]
},
"execution_count": 36,
@ -956,31 +945,32 @@
"output_type": "execute_result"
}
],
"source": [
"# NBVAL_IGNORE_OUTPUT\n",
"idl"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 1, 2, 3]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l *= 2\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"139840389979848"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(l) # same list"
]
},
{
"cell_type": "code",
"execution_count": 38,
@ -989,7 +979,7 @@
{
"data": {
"text/plain": [
"139840321336880"
"True"
]
},
"execution_count": 38,
@ -998,29 +988,59 @@
}
],
"source": [
"t = (1, 2, 3)\n",
"id(t)"
"id(l) == idl # same list"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"t = (1, 2, 3)\n",
"idt = id(t)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"139840440497640"
"4414275328"
]
},
"execution_count": 39,
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NBVAL_IGNORE_OUTPUT\n",
"idt"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t *= 2\n",
"id(t) # new tuple"
"id(t) == idt # new tuple"
]
},
{
@ -1032,7 +1052,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 42,
"metadata": {},
"outputs": [
{
@ -1053,7 +1073,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@ -1062,7 +1082,7 @@
"(1, 2, [30, 40, 50, 60])"
]
},
"execution_count": 41,
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
@ -1080,7 +1100,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@ -1115,7 +1135,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 45,
"metadata": {},
"outputs": [
{
@ -1124,7 +1144,7 @@
"['apple', 'banana', 'grape', 'raspberry']"
]
},
"execution_count": 43,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
@ -1136,7 +1156,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 46,
"metadata": {},
"outputs": [
{
@ -1145,53 +1165,13 @@
"['grape', 'raspberry', 'apple', 'banana']"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['raspberry', 'grape', 'banana', 'apple']"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(fruits, reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['grape', 'apple', 'banana', 'raspberry']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(fruits, key=len)"
"fruits"
]
},
{
@ -1202,7 +1182,7 @@
{
"data": {
"text/plain": [
"['raspberry', 'banana', 'grape', 'apple']"
"['raspberry', 'grape', 'banana', 'apple']"
]
},
"execution_count": 47,
@ -1211,7 +1191,7 @@
}
],
"source": [
"sorted(fruits, key=len, reverse=True)"
"sorted(fruits, reverse=True)"
]
},
{
@ -1222,7 +1202,7 @@
{
"data": {
"text/plain": [
"['grape', 'raspberry', 'apple', 'banana']"
"['grape', 'apple', 'banana', 'raspberry']"
]
},
"execution_count": 48,
@ -1231,7 +1211,7 @@
}
],
"source": [
"fruits"
"sorted(fruits, key=len)"
]
},
{
@ -1242,7 +1222,7 @@
{
"data": {
"text/plain": [
"['apple', 'banana', 'grape', 'raspberry']"
"['raspberry', 'banana', 'grape', 'apple']"
]
},
"execution_count": 49,
@ -1250,6 +1230,46 @@
"output_type": "execute_result"
}
],
"source": [
"sorted(fruits, key=len, reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['grape', 'raspberry', 'apple', 'banana']"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'banana', 'grape', 'raspberry']"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits.sort()\n",
"fruits"
@ -1271,7 +1291,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 52,
"metadata": {},
"outputs": [
{
@ -1318,7 +1338,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 53,
"metadata": {},
"outputs": [
{
@ -1354,7 +1374,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 54,
"metadata": {},
"outputs": [
{
@ -1363,7 +1383,7 @@
"['F', 'D', 'D', 'C', 'C', 'B', 'B', 'A', 'A']"
]
},
"execution_count": 52,
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
@ -1385,7 +1405,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 55,
"metadata": {},
"outputs": [
{
@ -1394,7 +1414,7 @@
"['F', 'F', 'D', 'D', 'C', 'C', 'B', 'B', 'A']"
]
},
"execution_count": 53,
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
@ -1416,7 +1436,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 56,
"metadata": {},
"outputs": [
{
@ -1470,40 +1490,6 @@
"#### Example 2-19. Creating, saving, and loading a large array of floats"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5963321947530882"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from array import array\n",
"from random import random\n",
"\n",
"floats = array('d', (random() for i in range(10**7)))\n",
"floats[-1]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"with open('floats.bin', 'wb') as fp:\n",
" floats.tofile(fp)"
]
},
{
"cell_type": "code",
"execution_count": 57,
@ -1520,6 +1506,40 @@
"output_type": "execute_result"
}
],
"source": [
"from array import array\n",
"from random import random\n",
"\n",
"floats = array('d', (random() for i in range(10**7)))\n",
"floats[-1]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"with open('floats.bin', 'wb') as fp:\n",
" floats.tofile(fp)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5963321947530882"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"floats2 = array('d')\n",
"\n",
@ -1531,7 +1551,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 60,
"metadata": {},
"outputs": [
{
@ -1540,7 +1560,7 @@
"True"
]
},
"execution_count": 58,
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
@ -1565,7 +1585,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 61,
"metadata": {},
"outputs": [
{
@ -1574,7 +1594,7 @@
"5"
]
},
"execution_count": 59,
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
@ -1587,7 +1607,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 62,
"metadata": {},
"outputs": [
{
@ -1596,7 +1616,7 @@
"-2"
]
},
"execution_count": 60,
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
@ -1607,7 +1627,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 63,
"metadata": {},
"outputs": [
{
@ -1616,7 +1636,7 @@
"[254, 255, 255, 255, 0, 0, 1, 0, 2, 0]"
]
},
"execution_count": 61,
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
@ -1628,7 +1648,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 64,
"metadata": {},
"outputs": [
{
@ -1637,7 +1657,7 @@
"array('h', [-2, -1, 1024, 1, 2])"
]
},
"execution_count": 62,
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
@ -1663,7 +1683,7 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 65,
"metadata": {},
"outputs": [
{
@ -1672,7 +1692,7 @@
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"execution_count": 63,
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
@ -1685,7 +1705,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 66,
"metadata": {},
"outputs": [
{
@ -1694,7 +1714,7 @@
"numpy.ndarray"
]
},
"execution_count": 64,
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
@ -1705,7 +1725,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 67,
"metadata": {},
"outputs": [
{
@ -1714,7 +1734,7 @@
"(12,)"
]
},
"execution_count": 65,
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
@ -1725,7 +1745,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 68,
"metadata": {},
"outputs": [
{
@ -1736,7 +1756,7 @@
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 66,
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
@ -1748,7 +1768,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 69,
"metadata": {},
"outputs": [
{
@ -1757,7 +1777,7 @@
"array([ 8, 9, 10, 11])"
]
},
"execution_count": 67,
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
@ -1768,7 +1788,7 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 70,
"metadata": {},
"outputs": [
{
@ -1777,7 +1797,7 @@
"9"
]
},
"execution_count": 68,
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
@ -1788,7 +1808,7 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 71,
"metadata": {},
"outputs": [
{
@ -1797,7 +1817,7 @@
"array([1, 5, 9])"
]
},
"execution_count": 69,
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
@ -1808,7 +1828,7 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 72,
"metadata": {},
"outputs": [
{
@ -1820,7 +1840,7 @@
" [ 3, 7, 11]])"
]
},
"execution_count": 70,
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
@ -1838,7 +1858,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
@ -1849,7 +1869,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
@ -1858,7 +1878,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 75,
"metadata": {},
"outputs": [
{
@ -1867,7 +1887,7 @@
"array([0.29150425, 0.33893554, 0.08112756])"
]
},
"execution_count": 73,
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
@ -1878,7 +1898,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 76,
"metadata": {},
"outputs": [
{
@ -1887,7 +1907,7 @@
"array([0.14575213, 0.16946777, 0.04056378])"
]
},
"execution_count": 74,
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
@ -1899,16 +1919,16 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.00606649300607387"
"True"
]
},
"execution_count": 75,
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
@ -1916,12 +1936,14 @@
"source": [
"from time import perf_counter as pc\n",
"\n",
"t0 = pc(); floats /= 3; pc() - t0"
"t0 = pc()\n",
"floats /= 3\n",
"(pc() - t0) < 0.01"
]
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
@ -1932,7 +1954,7 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 79,
"metadata": {},
"outputs": [
{
@ -1941,7 +1963,7 @@
"memmap([0.29150425, 0.33893554, 0.08112756])"
]
},
"execution_count": 77,
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
@ -1966,7 +1988,7 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 80,
"metadata": {},
"outputs": [
{
@ -1975,7 +1997,7 @@
"deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 78,
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
@ -1989,7 +2011,7 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 81,
"metadata": {},
"outputs": [
{
@ -1998,7 +2020,7 @@
"deque([7, 8, 9, 0, 1, 2, 3, 4, 5, 6])"
]
},
"execution_count": 79,
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
@ -2010,7 +2032,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 82,
"metadata": {},
"outputs": [
{
@ -2019,7 +2041,7 @@
"deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])"
]
},
"execution_count": 80,
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
@ -2031,7 +2053,7 @@
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": 83,
"metadata": {},
"outputs": [
{
@ -2040,7 +2062,7 @@
"deque([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 81,
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
@ -2052,7 +2074,7 @@
},
{
"cell_type": "code",
"execution_count": 82,
"execution_count": 84,
"metadata": {},
"outputs": [
{
@ -2061,7 +2083,7 @@
"deque([3, 4, 5, 6, 7, 8, 9, 11, 22, 33])"
]
},
"execution_count": 82,
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
@ -2073,7 +2095,7 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": 85,
"metadata": {},
"outputs": [
{
@ -2082,7 +2104,7 @@
"deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])"
]
},
"execution_count": 83,
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
@ -2108,7 +2130,7 @@
},
{
"cell_type": "code",
"execution_count": 84,
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
@ -2117,7 +2139,7 @@
},
{
"cell_type": "code",
"execution_count": 85,
"execution_count": 87,
"metadata": {},
"outputs": [
{
@ -2144,7 +2166,7 @@
},
{
"cell_type": "code",
"execution_count": 86,
"execution_count": 88,
"metadata": {},
"outputs": [
{
@ -2153,7 +2175,7 @@
"[0, '1', 5, 6, '9', 14, 19, '23', 28, '28']"
]
},
"execution_count": 86,
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
@ -2166,7 +2188,7 @@
},
{
"cell_type": "code",
"execution_count": 87,
"execution_count": 89,
"metadata": {},
"outputs": [
{
@ -2175,7 +2197,7 @@
"[0, '1', 14, 19, '23', 28, '28', 5, 6, '9']"
]
},
"execution_count": 87,
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
@ -2208,7 +2230,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.0"
}
},
"nbformat": 4,

View File

@ -1,3 +1,41 @@
"""
bisect_demo.py
Demonstration of ``bisect.bisect``::
>>> import bisect
>>> demo(bisect.bisect)
31 @ 14 | | | | | | | | | | | | | |31
30 @ 14 | | | | | | | | | | | | | |30
29 @ 13 | | | | | | | | | | | | |29
23 @ 11 | | | | | | | | | | |23
22 @ 9 | | | | | | | | |22
10 @ 5 | | | | |10
8 @ 5 | | | | |8
5 @ 3 | | |5
2 @ 1 |2
1 @ 1 |1
0 @ 0 0
Demonstration of ``bisect.bisect_left``::
>>> demo(bisect.bisect_left)
31 @ 14 | | | | | | | | | | | | | |31
30 @ 13 | | | | | | | | | | | | |30
29 @ 12 | | | | | | | | | | | |29
23 @ 9 | | | | | | | | |23
22 @ 9 | | | | | | | | |22
10 @ 5 | | | | |10
8 @ 4 | | | |8
5 @ 2 | |5
2 @ 1 |2
1 @ 0 1
0 @ 0 0
"""
# BEGIN BISECT_DEMO
import bisect
import sys

View File

@ -1,3 +1,16 @@
"""
metro_lat_long.py
Demonstration of nested tuple unpacking::
>>> main()
| lat. | long.
Mexico City | 19.4333 | -99.1333
New York-Newark | 40.8086 | -74.0204
Sao Paulo | -23.5478 | -46.6358
"""
metro_areas = [
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), # <1>
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
@ -6,8 +19,12 @@ metro_areas = [
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
]
print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
fmt = '{:15} | {:9.4f} | {:9.4f}'
for name, cc, pop, (latitude, longitude) in metro_areas: # <2>
if longitude <= 0: # <3>
print(fmt.format(name, latitude, longitude))
def main():
print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
fmt = '{:15} | {:9.4f} | {:9.4f}'
for name, cc, pop, (latitude, longitude) in metro_areas: # <2>
if longitude <= 0: # <3>
print(fmt.format(name, latitude, longitude))
if __name__ == '__main__':
main()

4
02-array-seq/test.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
python3 -m doctest bisect_demo.py
python3 -m doctest metro_lat_long.py
pytest -q --nbval