ch11-24: clean up by @eumiro & sync with Atlas

This commit is contained in:
Luciano Ramalho
2021-02-14 20:58:46 -03:00
parent 03ace4f4ae
commit 47cafc801a
143 changed files with 21692 additions and 63 deletions

1
21-futures/getflags/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
flags/

View File

@@ -69,9 +69,9 @@ def expand_cc_args(every_cc: bool,
cc_args: list[str],
limit: int) -> list[str]:
codes: set[str] = set()
A_Z = set(string.ascii_uppercase)
A_Z = string.ascii_uppercase
if every_cc:
codes.update(f'{a}{b}' for a in A_Z for b in A_Z)
codes.update(a+b for a in A_Z for b in A_Z)
elif all_cc:
text = COUNTRY_CODES_FILE.read_text()
codes.update(text.split())

View File

@@ -11,36 +11,41 @@ Sample run::
20 flags downloaded in 1.07s
"""
# tag::FLAGS_ASYNCIO[]
# tag::FLAGS_ASYNCIO_TOP[]
import asyncio
from aiohttp import ClientSession # <1>
from flags import BASE_URL, save_flag, main # <2>
async def get_flag(session: ClientSession, cc: str) -> bytes: # <3>
cc = cc.lower()
url = f'{BASE_URL}/{cc}/{cc}.gif'
async with session.get(url) as resp: # <4>
print(resp)
return await resp.read() # <5>
async def download_one(session: ClientSession, cc: str): # <6>
async def download_one(session: ClientSession, cc: str): # <3>
image = await get_flag(session, cc)
print(cc, end=' ', flush=True)
save_flag(image, cc.lower() + '.gif')
return cc
async def supervisor(cc_list):
async with ClientSession() as session: # <7>
to_do = [download_one(session, cc)
for cc in sorted(cc_list)] # <8>
res = await asyncio.gather(*to_do) # <9>
return len(res)
async def get_flag(session: ClientSession, cc: str) -> bytes: # <4>
cc = cc.lower()
url = f'{BASE_URL}/{cc}/{cc}.gif'
async with session.get(url) as resp: # <5>
print(resp)
return await resp.read() # <6>
def download_many(cc_list):
return asyncio.run(supervisor(cc_list)) # <10>
# end::FLAGS_ASYNCIO_TOP[]
# end::FLAGS_ASYNCIO_START[]
def download_many(cc_list): # <1>
return asyncio.run(supervisor(cc_list)) # <2>
async def supervisor(cc_list):
async with ClientSession() as session: # <3>
to_do = [download_one(session, cc)
for cc in sorted(cc_list)] # <4>
res = await asyncio.gather(*to_do) # <5>
return len(res) # <6>
if __name__ == '__main__':
main(download_many)
# end::FLAGS_ASYNCIO[]
# end::FLAGS_ASYNCIO_START[]

View File

@@ -20,7 +20,7 @@ def download_many(cc_list: list[str]) -> int:
to_do.append(future) # <5>
print(f'Scheduled for {cc}: {future}') # <6>
for count, future in enumerate(futures.as_completed(to_do)): # <7>
for count, future in enumerate(futures.as_completed(to_do), 1): # <7>
res: str = future.result() # <8>
print(f'{future} result: {res!r}') # <9>