Skip to content

Commit 03ace4f

Browse files
committed
ch01-12: clean up by @eumiro
1 parent 584a7f2 commit 03ace4f

33 files changed

+1383
-86
lines changed

01-data-model/vector2d.doctest

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
>>> from vector2d import Vector
2+
>>> v1 = Vector(2, 4)
3+
>>> v2 = Vector(2, 1)
4+
>>> v1 + v2
5+
Vector(4, 5)
6+
>>> v = Vector(3, 4)
7+
>>> abs(v)
8+
5.0
9+
>>> v * 3
10+
Vector(9, 12)
11+
>>> abs(v * 3)
12+
15.0

01-data-model/vector2d.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"""
3030

3131

32-
from math import hypot
32+
import math
3333

3434
class Vector:
3535

@@ -41,7 +41,7 @@ def __repr__(self):
4141
return f'Vector({self.x!r}, {self.y!r})'
4242

4343
def __abs__(self):
44-
return hypot(self.x, self.y)
44+
return math.hypot(self.x, self.y)
4545

4646
def __bool__(self):
4747
return bool(abs(self))

02-array-seq/.gitignore

-3
This file was deleted.

02-array-seq/README.md

-21
This file was deleted.

02-array-seq/README.rst

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

02-array-seq/bisect_demo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
3737
"""
3838

39-
# BEGIN BISECT_DEMO
39+
# tag::BISECT_DEMO[]
4040
import bisect
4141
import sys
4242

@@ -62,4 +62,4 @@ def demo(bisect_fn):
6262
print('haystack ->', ' '.join(f'{n:2}' for n in HAYSTACK))
6363
demo(bisect_fn)
6464

65-
# END BISECT_DEMO
65+
# end::BISECT_DEMO[]

02-array-seq/bisect_insort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
for i in range(SIZE):
1010
new_item = random.randrange(SIZE * 2)
1111
bisect.insort(my_list, new_item)
12-
print(f'{new_item:2} ->', my_list)
12+
print(f'{new_item:2d} -> {my_list}')

02-array-seq/memoryviews.ipynb

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# memoryviews\n",
8+
"\n",
9+
"## References\n",
10+
"\n",
11+
"* [PEP-3118](https://www.python.org/dev/peps/pep-3118/) Revising the buffer protocol (2006)\n",
12+
"* [issue14130](https://bugs.python.org/issue14130) memoryview: add multi-dimensional indexing and slicing (2012)"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"metadata": {},
19+
"outputs": [
20+
{
21+
"name": "stdout",
22+
"output_type": "stream",
23+
"text": [
24+
"3.7.1 (default, Dec 14 2018, 13:28:58) \n",
25+
"[Clang 4.0.1 (tags/RELEASE_401/final)]\n"
26+
]
27+
}
28+
],
29+
"source": [
30+
"import sys\n",
31+
"print(sys.version)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 2,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"data": {
41+
"text/plain": [
42+
"<memory at 0x1045a31c8>"
43+
]
44+
},
45+
"execution_count": 2,
46+
"metadata": {},
47+
"output_type": "execute_result"
48+
}
49+
],
50+
"source": [
51+
"mv1d = memoryview(bytes(range(35, 50)))\n",
52+
"mv1d"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 3,
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"data": {
62+
"text/plain": [
63+
"[35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]"
64+
]
65+
},
66+
"execution_count": 3,
67+
"metadata": {},
68+
"output_type": "execute_result"
69+
}
70+
],
71+
"source": [
72+
"list(mv1d)"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 4,
78+
"metadata": {},
79+
"outputs": [
80+
{
81+
"data": {
82+
"text/plain": [
83+
"<memory at 0x10464a120>"
84+
]
85+
},
86+
"execution_count": 4,
87+
"metadata": {},
88+
"output_type": "execute_result"
89+
}
90+
],
91+
"source": [
92+
"mv2d = mv1d.cast('B', [3, 5])\n",
93+
"mv2d"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 5,
99+
"metadata": {},
100+
"outputs": [
101+
{
102+
"data": {
103+
"text/plain": [
104+
"(3, 5)"
105+
]
106+
},
107+
"execution_count": 5,
108+
"metadata": {},
109+
"output_type": "execute_result"
110+
}
111+
],
112+
"source": [
113+
"mv2d.shape"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 6,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"data": {
123+
"text/plain": [
124+
"3"
125+
]
126+
},
127+
"execution_count": 6,
128+
"metadata": {},
129+
"output_type": "execute_result"
130+
}
131+
],
132+
"source": [
133+
"len(mv2d)"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 7,
139+
"metadata": {},
140+
"outputs": [
141+
{
142+
"data": {
143+
"text/plain": [
144+
"[[35, 36, 37, 38, 39], [40, 41, 42, 43, 44], [45, 46, 47, 48, 49]]"
145+
]
146+
},
147+
"execution_count": 7,
148+
"metadata": {},
149+
"output_type": "execute_result"
150+
}
151+
],
152+
"source": [
153+
"mv2d.tolist()"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 8,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"name": "stdout",
163+
"output_type": "stream",
164+
"text": [
165+
"[35, 36, 37, 38, 39]\n",
166+
"[40, 41, 42, 43, 44]\n",
167+
"[45, 46, 47, 48, 49]\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"for row in mv2d.tolist():\n",
173+
" print(row)"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 9,
179+
"metadata": {},
180+
"outputs": [
181+
{
182+
"data": {
183+
"text/plain": [
184+
"42"
185+
]
186+
},
187+
"execution_count": 9,
188+
"metadata": {},
189+
"output_type": "execute_result"
190+
}
191+
],
192+
"source": [
193+
"mv2d[1, 2]"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 10,
199+
"metadata": {},
200+
"outputs": [
201+
{
202+
"data": {
203+
"text/plain": [
204+
"42"
205+
]
206+
},
207+
"execution_count": 10,
208+
"metadata": {},
209+
"output_type": "execute_result"
210+
}
211+
],
212+
"source": [
213+
"mv2d.tolist()[1][2]"
214+
]
215+
}
216+
],
217+
"metadata": {
218+
"kernelspec": {
219+
"display_name": "Python 3",
220+
"language": "python",
221+
"name": "python3"
222+
},
223+
"language_info": {
224+
"codemirror_mode": {
225+
"name": "ipython",
226+
"version": 3
227+
},
228+
"file_extension": ".py",
229+
"mimetype": "text/x-python",
230+
"name": "python",
231+
"nbconvert_exporter": "python",
232+
"pygments_lexer": "ipython3",
233+
"version": "3.7.1"
234+
}
235+
},
236+
"nbformat": 4,
237+
"nbformat_minor": 2
238+
}

02-array-seq/slice-assign.rst

-11
This file was deleted.

03-dict-set/index0.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
for line_no, line in enumerate(fp, 1):
1616
for match in WORD_RE.finditer(line):
1717
word = match.group()
18-
column_no = match.start()+1
18+
column_no = match.start() + 1
1919
location = (line_no, column_no)
2020
# this is ugly; coded like this to make a point
2121
occurrences = index.get(word, []) # <1>

03-dict-set/index_default.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
for line_no, line in enumerate(fp, 1):
1717
for match in WORD_RE.finditer(line):
1818
word = match.group()
19-
column_no = match.start() + 1
19+
column_no = match.start()+1
2020
location = (line_no, column_no)
2121
index[word].append(location) # <2>
2222

03-dict-set/transformdict.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def getitem(self, key):
5656

5757
@property
5858
def transform_func(self):
59-
"""This TransformDict's transformation function"""
59+
"""This is TransformDict's transformation function"""
6060
return self._transform
6161

6262
# Minimum set of methods required for MutableMapping

0 commit comments

Comments
 (0)