MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / download_many

Function download_many

20-executors/getflags/flags2_sequential.py:60–87  ·  view source on GitHub ↗
(cc_list: list[str],
                  base_url: str,
                  verbose: bool,
                  _unused_concur_req: int)

Source from the content-addressed store, hash-verified

58
59# tag::FLAGS2_DOWNLOAD_MANY_SEQUENTIAL[]
60def download_many(cc_list: list[str],
61 base_url: str,
62 verbose: bool,
63 _unused_concur_req: int) -> Counter[DownloadStatus]:
64 counter: Counter[DownloadStatus] = Counter() # <1>
65 cc_iter = sorted(cc_list) # <2>
66 if not verbose:
67 cc_iter = tqdm.tqdm(cc_iter) # <3>
68 for cc in cc_iter:
69 try:
70 status = download_one(cc, base_url, verbose) # <4>
71 except httpx.HTTPStatusError as exc: # <5>
72 error_msg = 'HTTP error {resp.status_code} - {resp.reason_phrase}'
73 error_msg = error_msg.format(resp=exc.response)
74 except httpx.RequestError as exc: # <6>
75 error_msg = f'{exc} {type(exc)}'.strip()
76 except KeyboardInterrupt: # <7>
77 break
78 else: # <8>
79 error_msg = ''
80
81 if error_msg:
82 status = DownloadStatus.ERROR # <9>
83 counter[status] += 1 # <10>
84 if verbose and error_msg: # <11>
85 print(f'{cc} error: {error_msg}')
86
87 return counter # <12>
88# end::FLAGS2_DOWNLOAD_MANY_SEQUENTIAL[]
89
90if __name__ == '__main__':

Callers

nothing calls this directly

Calls 1

download_oneFunction · 0.70

Tested by

no test coverage detected