ch11-24: clean up by @eumiro & sync with Atlas
This commit is contained in:
1
21-futures/getflags/.gitignore
vendored
Normal file
1
21-futures/getflags/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
flags/
|
||||
@@ -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())
|
||||
|
||||
@@ -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[]
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user