QuantEcon / QuantEcon/lecture-python-programming

wb_download.py: use HTTPS, add a timeout and raise_for_status()

未關閉 適合新手
#608 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
JavaScript
星號
72
分支
31
平均合併
2 天 20 小時
30 天內合併 PR
8

描述

lectures/_static/lecture_specific/pandas/wb_download.py fetches the World Bank indicator over plain HTTP and writes the response body to disk without checking whether the request succeeded. Raised by Copilot while reviewing the downstream port at QuantEcon/lecture-python-programming.ml#6; it is upstream code, so it belongs here rather than in a translation edition.

Current lines 6-9:

wb_data_query = "http://api.worldbank.org/v2/en/indicator/gc.dod.totl.gd.zs?downloadformat=excel"
r = requests.get(wb_data_query)
with open('gd.xls', 'wb') as output:
    output.write(r.content)

Three points, in rough order of how much they matter:

No error check. On any non-200 response the error page body is written to gd.xls, and the failure only surfaces further down as an opaque pd.read_excel parse error rather than as the network problem it actually is. r.raise_for_status() turns that into a clear failure at the point of the fault.

Plain HTTP. api.worldbank.org serves HTTPS, so this is a free upgrade — and it is a file the lectures hold up as example code, which is the argument for getting it right beyond the MITM exposure itself.

No timeout. requests.get without timeout blocks indefinitely if the endpoint stops responding, which is an unpleasant failure mode inside a notebook build.

Suggested:

wb_data_query = "https://api.worldbank.org/v2/en/indicator/gc.dod.totl.gd.zs?downloadformat=excel"
r = requests.get(wb_data_query, timeout=30)
r.raise_for_status()
with open('gd.xls', 'wb') as output:
    output.write(r.content)

Note this script is a static asset rather than an executed cell, so nothing in CI exercises it — worth a manual check that the HTTPS endpoint returns the same workbook before merging.

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

開啟 lectures/_static/lecture_specific/pandas/wb_download.py,檢查第 6-9 行的 fetch。按照描述更新請求,然後手動檢查 HTTPS World Bank 端點是否回傳預期的活頁簿,因為 CI 不會執行這個靜態資產。完成的標準是失敗能夠清楚地顯現,且活頁簿仍支援下游讀取。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
缺陷
難度
1/5
預估耗時
1-3 小時
活躍度
冷清
描述清晰度
描述清楚
新手友好度
88/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。