Posts

Showing posts with the label python

PyCon Taiwan 2021 與會隨筆

PyCon Taiwan 2021:大會之後會將影片上傳至 Youtube Day 1 成功地測試失敗 需求是要修理某個折扣的邏輯,但講者發現 api 傳來的資料是 list,猜測可能會傳過來不只一組折扣,所以用「迭代 list 中所有項目」的方式處理。上線後發現,果然沒猜錯,會傳過來不只一組折扣。如果當時沒有迭代整個 list,而只處理 list[0] 這組折扣,那就又要修 bug 了。 使用 Pytest 進行單元測試 python 內建 unitest 模組。pytest 套件支援 unitest 的 test case,在使用上 pytest 的語法比較方便,功能也比較強大。 參考書:The Art of Unit Testing 出口驗證有三種: 檢查函式的 output 檢查函式的 private property。若沒有 getter 可存取該 property,會比較麻煩一點。不過 python 沒有真正的 private property,處理起來比較容易。 函式的 return 就是呼叫外部 api。此時可以模擬外部 api,並記錄函式如何呼叫 api,這樣就能檢查呼叫的方式是否正確。 以前沒有想過「exception也要測試」這件事 原來測試碼覆蓋率,是有套件可以幫我們計算的。 Lightning Talks Patrick:用 Python 解決 iOS 14 之亂所帶來的數位廣告問題 塔圖科技針對「第三方 cookie 停用,網站需要在後端把資料送去給 FB」而推出了一個解決方案,讓網站不需要更動後端的程式碼,就可以送資料給 FB。我目前主要是學習網站後端的技能,這個消息透露了,第三方 cookie 停用,替代方案可能會需要由網站的後端工程師完成。 DennySORA:挖掘 asyncio 時的 try, except, else 與 finally 的小發現 原來 try-except-finally ,在不同的地方 return,會出現各種意料之外的效果。嚇得我立刻去搜尋到這篇文章 Python: 浅析 return 和 fin...

flask 小抄(Pipenv 版)(Flask 2.0.1)

初始化專案 建立並移動到專案資料夾 用 pipenv 將專案初始化: pipenv --python 3.9 安裝 Flask 套件: pipenv install flask 在根目錄建立 app.py 設定環境變數: export FLASK_APP=app.py export FLASK_ENV=development (存擋後會自動重啟 app) .gitignore: .gitignore 產生器 Application Factories  起手式: # app.py from flask import Flask, render_template, request def create_app():     app = Flask(__name__)     # do something     return app 參考資料 为Git仓库里的.idea文件夹正名 執行專案 flask run pipenv or pipenv run python app.py 在瀏覽器網址列輸入 http://127.0.0.1:5000/ Jinja2 Builtin Filter 連接資料庫 mongodb 安裝套件(先確定有啟動 venv): pipenv install pymongo or support for mongodb+srv:// URIs requires dnspython: pipenv install pymongo[srv] PyMongo 3.12.0 Documentation 連線設定: from pymongo import MongoClient client = MongoClient('url') app.db = client['databas...

在 Mac 上安裝 pipenv(套件管理工具)

安裝教學 Configure a Pipenv environment Pipenv & Virtual Environments 確認 pip 已安裝: pip --version 結果:pip 21.1.3 from /.../.pyenv/versions/3.9.6/... pip3 --version 結果:pip 21.1.3 from /.../.pyenv/versions/3.9.6/... 安裝 pipenv: pip install --user pipenv 安裝後的訊息: WARNING: The script virtualenv-clone is installed in '/Users/flora/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The script virtualenv is installed in '/Users/flora/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts pipenv and pipenv-resolver are installed in '/Users/flora/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully instal...

在 Mac 上安裝 pyenv(python 版本管理工具)

安裝教學: Managing Multiple Python Versions With pyenv 執行完 curl https://pyenv.run | bash 後得到的指示: WARNING: seems you still have not added 'pyenv' to the load path. # (The below instructions are intended for common # shell setups. See the README for more guidance # if they don't apply and/or don't work for you.) # Add pyenv executable to PATH and # enable shims by adding the following # to ~/.profile: export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" # If your ~/.profile sources ~/.bashrc, # the lines need to be inserted before the part # that does that. See the README for another option. # If you have ~/.bash_profile, make sure that it # also executes the above lines -- e.g. by # copying them there or by sourcing ~/.profile # Load pyenv into the shell by adding # the following to ~/.bashrc: eval "$(pyenv init -)" # Make sure to restart your entir...