Skip to content

Commit c8b6ad4

Browse files
committed
Merge branch 'master' of github.com:fluentpython/example-code-2e into master
2 parents cdbca80 + 4dcb6aa commit c8b6ad4

File tree

12 files changed

+123
-90
lines changed

12 files changed

+123
-90
lines changed

21-futures/getflags/flags.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@ def save_flag(img: bytes, filename: str) -> None: # <5>
3333
(DEST_DIR / filename).write_bytes(img)
3434

3535
def get_flag(cc: str) -> bytes: # <6>
36-
cc = cc.lower()
37-
url = f'{BASE_URL}/{cc}/{cc}.gif'
36+
url = f'{BASE_URL}/{cc}/{cc}.gif'.lower()
3837
resp = requests.get(url)
3938
return resp.content
4039

4140
def download_many(cc_list: list[str]) -> int: # <7>
4241
for cc in sorted(cc_list): # <8>
4342
image = get_flag(cc)
43+
save_flag(image, f'{cc}.gif')
4444
print(cc, end=' ', flush=True) # <9>
45-
save_flag(image, cc.lower() + '.gif')
4645
return len(cc_list)
4746

4847
def main(downloader: Callable[[list[str]], int]) -> None: # <10>

21-futures/getflags/flags2_asyncio.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
asyncio async/await version
66
77
"""
8-
# BEGIN FLAGS2_ASYNCIO_TOP
8+
# tag::FLAGS2_ASYNCIO_TOP[]
99
import asyncio
1010
from collections import Counter
1111

1212
import aiohttp
13-
from aiohttp import web
14-
from aiohttp.http_exceptions import HttpProcessingError
1513
import tqdm # type: ignore
1614

1715
from flags2_common import main, HTTPStatus, Result, save_flag
@@ -23,91 +21,83 @@
2321

2422

2523
class FetchError(Exception): # <1>
26-
def __init__(self, country_code):
24+
def __init__(self, country_code: str):
2725
self.country_code = country_code
2826

2927

30-
async def get_flag(session, base_url, cc): # <2>
31-
cc = cc.lower()
32-
url = f'{base_url}/{cc}/{cc}.gif'
28+
async def get_flag(session: aiohttp.ClientSession, # <2>
29+
base_url: str,
30+
cc: str) -> bytes:
31+
url = f'{base_url}/{cc}/{cc}.gif'.lower()
3332
async with session.get(url) as resp:
3433
if resp.status == 200:
3534
return await resp.read()
36-
elif resp.status == 404:
37-
raise web.HTTPNotFound()
3835
else:
39-
raise HttpProcessingError(
40-
code=resp.status, message=resp.reason,
41-
headers=resp.headers)
42-
43-
44-
async def download_one(session, cc, base_url, semaphore, verbose): # <3>
36+
resp.raise_for_status() # <3>
37+
return bytes()
38+
39+
async def download_one(session: aiohttp.ClientSession, # <4>
40+
cc: str,
41+
base_url: str,
42+
semaphore: asyncio.Semaphore,
43+
verbose: bool) -> Result:
4544
try:
46-
async with semaphore: # <4>
47-
image = await get_flag(session, base_url, cc) # <5>
48-
except web.HTTPNotFound: # <6>
49-
status = HTTPStatus.not_found
50-
msg = 'not found'
51-
except Exception as exc:
52-
raise FetchError(cc) from exc # <7>
45+
async with semaphore: # <5>
46+
image = await get_flag(session, base_url, cc)
47+
except aiohttp.ClientResponseError as exc:
48+
if exc.status == 404: # <6>
49+
status = HTTPStatus.not_found
50+
msg = 'not found'
51+
else:
52+
raise FetchError(cc) from exc # <7>
5353
else:
54-
save_flag(image, cc.lower() + '.gif') # <8>
54+
save_flag(image, f'{cc}.gif')
5555
status = HTTPStatus.ok
5656
msg = 'OK'
57-
5857
if verbose and msg:
5958
print(cc, msg)
60-
6159
return Result(status, cc)
62-
# END FLAGS2_ASYNCIO_TOP
60+
# end::FLAGS2_ASYNCIO_TOP[]
6361

64-
# BEGIN FLAGS2_ASYNCIO_DOWNLOAD_MANY
65-
async def downloader_coro(cc_list: list[str],
66-
base_url: str,
67-
verbose: bool,
68-
concur_req: int) -> Counter[HTTPStatus]: # <1>
62+
# tag::FLAGS2_ASYNCIO_START[]
63+
async def supervisor(cc_list: list[str],
64+
base_url: str,
65+
verbose: bool,
66+
concur_req: int) -> Counter[HTTPStatus]: # <1>
6967
counter: Counter[HTTPStatus] = Counter()
7068
semaphore = asyncio.Semaphore(concur_req) # <2>
71-
async with aiohttp.ClientSession() as session: # <8>
69+
async with aiohttp.ClientSession() as session:
7270
to_do = [download_one(session, cc, base_url, semaphore, verbose)
7371
for cc in sorted(cc_list)] # <3>
74-
7572
to_do_iter = asyncio.as_completed(to_do) # <4>
7673
if not verbose:
7774
to_do_iter = tqdm.tqdm(to_do_iter, total=len(cc_list)) # <5>
78-
for future in to_do_iter: # <6>
75+
for coro in to_do_iter: # <6>
7976
try:
80-
res = await future # <7>
77+
res = await coro # <7>
8178
except FetchError as exc: # <8>
8279
country_code = exc.country_code # <9>
8380
try:
84-
if exc.__cause__ is None:
85-
error_msg = 'Unknown cause'
86-
else:
87-
error_msg = exc.__cause__.args[0] # <10>
88-
except IndexError:
89-
error_msg = exc.__cause__.__class__.__name__ # <11>
81+
error_msg = exc.__cause__.message # type: ignore # <10>
82+
except AttributeError:
83+
error_msg = 'Unknown cause' # <11>
9084
if verbose and error_msg:
9185
print(f'*** Error for {country_code}: {error_msg}')
9286
status = HTTPStatus.error
9387
else:
9488
status = res.status
95-
9689
counter[status] += 1 # <12>
97-
9890
return counter # <13>
9991

100-
10192
def download_many(cc_list: list[str],
10293
base_url: str,
10394
verbose: bool,
10495
concur_req: int) -> Counter[HTTPStatus]:
105-
coro = downloader_coro(cc_list, base_url, verbose, concur_req)
96+
coro = supervisor(cc_list, base_url, verbose, concur_req)
10697
counts = asyncio.run(coro) # <14>
10798

10899
return counts
109100

110-
111101
if __name__ == '__main__':
112102
main(download_many, DEFAULT_CONCUR_REQ, MAX_CONCUR_REQ)
113-
# END FLAGS2_ASYNCIO_DOWNLOAD_MANY
103+
# end::FLAGS2_ASYNCIO_START[]

21-futures/getflags/flags2_common.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def initial_report(cc_list: list[str],
4545
print(f'{server_label} site: {SERVERS[server_label]}')
4646
plural = 's' if len(cc_list) != 1 else ''
4747
print(f'Searching for {len(cc_list)} flag{plural}: {cc_msg}')
48-
plural = 's' if actual_req != 1 else ''
49-
print(f'{actual_req} concurrent connection{plural} will be used.')
48+
if actual_req == 1:
49+
print('1 connection will be used.')
50+
else:
51+
print(f'{actual_req} concurrent connections will be used.')
5052

5153

5254
def final_report(cc_list: list[str],
@@ -60,7 +62,7 @@ def final_report(cc_list: list[str],
6062
print(f'{counter[HTTPStatus.not_found]} not found.')
6163
if counter[HTTPStatus.error]:
6264
plural = 's' if counter[HTTPStatus.error] != 1 else ''
63-
print(f'{counter[HTTPStatus.error]} error{plural}')
65+
print(f'{counter[HTTPStatus.error]} error{plural}.')
6466
print(f'Elapsed time: {elapsed:.2f}s')
6567

6668

21-futures/getflags/flags2_sequential.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929

3030
# tag::FLAGS2_BASIC_HTTP_FUNCTIONS[]
3131
def get_flag(base_url: str, cc: str) -> bytes:
32-
cc = cc.lower()
33-
url = f'{base_url}/{cc}/{cc}.gif'
32+
url = f'{base_url}/{cc}/{cc}.gif'.lower()
3433
resp = requests.get(url)
3534
if resp.status_code != 200: # <1>
3635
resp.raise_for_status()
@@ -47,7 +46,7 @@ def download_one(cc: str, base_url: str, verbose: bool = False):
4746
else: # <4>
4847
raise
4948
else:
50-
save_flag(image, cc.lower() + '.gif')
49+
save_flag(image, f'{cc}.gif')
5150
status = HTTPStatus.ok
5251
msg = 'OK'
5352

21-futures/getflags/flags2_threadpool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def download_many(cc_list: list[str],
4040
with futures.ThreadPoolExecutor(max_workers=concur_req) as executor: # <6>
4141
to_do_map = {} # <7>
4242
for cc in sorted(cc_list): # <8>
43-
future = executor.submit(download_one,
44-
cc, base_url, verbose) # <9>
43+
future = executor.submit(download_one, cc,
44+
base_url, verbose) # <9>
4545
to_do_map[future] = cc # <10>
4646
done_iter = futures.as_completed(to_do_map) # <11>
4747
if not verbose:

21-futures/getflags/flags_asyncio.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,27 @@
2020

2121
async def download_one(session: ClientSession, cc: str): # <3>
2222
image = await get_flag(session, cc)
23+
save_flag(image, f'{cc}.gif')
2324
print(cc, end=' ', flush=True)
24-
save_flag(image, cc.lower() + '.gif')
2525
return cc
2626

2727
async def get_flag(session: ClientSession, cc: str) -> bytes: # <4>
28-
cc = cc.lower()
29-
url = f'{BASE_URL}/{cc}/{cc}.gif'
28+
url = f'{BASE_URL}/{cc}/{cc}.gif'.lower()
3029
async with session.get(url) as resp: # <5>
31-
print(resp)
3230
return await resp.read() # <6>
33-
34-
3531
# end::FLAGS_ASYNCIO_TOP[]
3632

37-
# end::FLAGS_ASYNCIO_START[]
38-
def download_many(cc_list): # <1>
39-
return asyncio.run(supervisor(cc_list)) # <2>
33+
# tag::FLAGS_ASYNCIO_START[]
34+
def download_many(cc_list: list[str]) -> int: # <1>
35+
return asyncio.run(supervisor(cc_list)) # <2>
4036

41-
async def supervisor(cc_list):
42-
async with ClientSession() as session: # <3>
43-
to_do = [download_one(session, cc)
44-
for cc in sorted(cc_list)] # <4>
45-
res = await asyncio.gather(*to_do) # <5>
37+
async def supervisor(cc_list: list[str]) -> int:
38+
async with ClientSession() as session: # <3>
39+
to_do = [download_one(session, cc) # <4>
40+
for cc in sorted(cc_list)]
41+
res = await asyncio.gather(*to_do) # <5>
4642

47-
return len(res) # <6>
43+
return len(res) # <6>
4844

4945
if __name__ == '__main__':
5046
main(download_many)

21-futures/getflags/flags_threadpool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
def download_one(cc: str): # <2>
2121
image = get_flag(cc)
22+
save_flag(image, f'{cc}.gif')
2223
print(cc, end=' ', flush=True)
23-
save_flag(image, cc.lower() + '.gif')
2424
return cc
2525

2626
def download_many(cc_list: list[str]) -> int:

21-futures/getflags/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
aiohttp==3.7.3
1+
aiohttp==3.7.4
22
async-timeout==3.0.1
33
attrs==20.3.0
44
certifi==2020.12.5

21-futures/getflags/slow_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def server_bind(self):
8383
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
8484
return super().server_bind()
8585

86-
server.test(
86+
# test is a top-level function in http.server omitted from __all__
87+
server.test( # type: ignore
8788
HandlerClass=handler_class,
8889
ServerClass=DualStackServer,
8990
port=args.port,

22-asyncio/domains/netaddr.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import asyncio
2+
import socket
3+
from collections.abc import Iterable, AsyncIterator
4+
from typing import NamedTuple
5+
6+
7+
class Result(NamedTuple):
8+
domain: str
9+
found: bool
10+
11+
12+
async def probe(loop: asyncio.AbstractEventLoop, domain: str) -> Result:
13+
try:
14+
await loop.getaddrinfo(domain, None)
15+
except socket.gaierror:
16+
return Result(domain, False)
17+
return Result(domain, True)
18+
19+
20+
async def multi_probe(domains: Iterable[str]) -> AsyncIterator[Result]:
21+
loop = asyncio.get_running_loop()
22+
coros = [probe(loop, domain) for domain in domains]
23+
for coro in asyncio.as_completed(coros):
24+
result = await coro
25+
yield result

22-asyncio/domains/probe.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
import asyncio
4+
import sys
5+
from keyword import kwlist
6+
7+
from netaddr import multi_probe
8+
9+
10+
async def main(tld: str) -> None:
11+
tld = tld.strip('.')
12+
names = (kw for kw in kwlist if len(kw) <= 4)
13+
domains = (f'{name}.{tld}'.lower() for name in names)
14+
async for domain, found in multi_probe(domains):
15+
mark = '.' if found else '?\t\t'
16+
print(f'{mark} {domain}')
17+
18+
19+
if __name__ == '__main__':
20+
if len(sys.argv) == 2:
21+
asyncio.run(main(sys.argv[1]))
22+
else:
23+
print('Please provide a TLD.', f'Example: {sys.argv[0]} COM.BR')
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
uvicorn main:app --reload
3-
"""
4-
51
import pathlib
62
from unicodedata import name
73

@@ -11,28 +7,30 @@
117

128
from charindex import InvertedIndex
139

14-
app = FastAPI(
10+
app = FastAPI( # <1>
1511
title='Mojifinder Web',
1612
description='Search for Unicode characters by name.',
1713
)
1814

19-
class CharName(BaseModel):
15+
class CharName(BaseModel): # <2>
2016
char: str
2117
name: str
2218

23-
def init(app):
19+
def init(app): # <3>
2420
app.state.index = InvertedIndex()
2521
static = pathlib.Path(__file__).parent.absolute() / 'static'
2622
with open(static / 'form.html') as fp:
2723
app.state.form = fp.read()
2824

29-
init(app)
25+
init(app) # <4>
26+
27+
@app.get('/search', response_model=list[CharName]) # <5>
28+
async def search(q: str): # <6>
29+
chars = app.state.index.search(q)
30+
return ({'char': c, 'name': name(c)} for c in chars) # <7>
3031

31-
@app.get('/', response_class=HTMLResponse, include_in_schema=False)
32+
@app.get('/', # <8>
33+
response_class=HTMLResponse,
34+
include_in_schema=False)
3235
def form():
3336
return app.state.form
34-
35-
@app.get('/search', response_model=list[CharName])
36-
async def search(q: str):
37-
chars = app.state.index.search(q)
38-
return [{'char': c, 'name': name(c)} for c in chars]

0 commit comments

Comments
 (0)