Posts

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...

shop_platform - 對電商業務流程不熟,而不確定該怎麼處理的地方

串接藍新金流 使用者用藍新金流刷信用卡付款。伺服器收到藍新金流回傳的 TradeInfo 與 TradeSha,TradeInfo 雜湊後的值與 TradeSha 不符。Status 是 Success 所以使用者應該已經付錢了,這個時候要怎麼處理?先退錢給使用者嗎? TradeInfo 雜湊後的值與 TradeSha 相符,但付款金額和訂單上的金額不一樣時,要怎麼辦?(若排除商業邏輯寫錯的可能,這種情況,可能是 HashKey 和 HashIV 外洩,導致使用者可以自己修改 post 給藍新金流的 TradeInfo 與 TradeSha)。需要考慮這種可能性嗎? 藍新金流回傳的 TradeInfo 裡包含許多付款資訊。我應該把哪些付款資訊存到資料庫裡?記錄這些付款資訊是為了處理什麼問題?

shop_platform - 串接藍新金流:TradeSha

Image
(本文使用的語言為 python 3) 文件:藍新金流 > API 文件下載 > 多功能收款 MPG。我下載時,版本是 1.6 TradeSha 是什麼 根據文件第 33 頁的第一個表格,需要傳送給藍新金流的參數共有四個,其中一個就是 TradeSha: 根據文件第 70 頁,得到 TradeSha 的方法,是把 TradeInfo 前面加上 HashKey,後面加上 HashIV,然後用 SHA256 做雜湊,接著把所有英文字母轉成大寫。這樣就是合格的 TradeSha。 test case 為: trade_info = 'ff91c8aa01379e4de621a44e5f11f72e4d25bdb1a18242db6cef9ef07d80b0165e476fd1d9acaa53170272c82d122961e1a0700a7427cfa1cf90db7f6d6593bbc93102a4d4b9b66d9974c13c31a7ab4bba1d4e0790f0cbbbd7ad64c6d3c8012a601ceaa808bff70f94a8efa5a4f984b9d41304ffd879612177c622f75f4214fa' key = '12345678901234567890123456789012' iv = '1234567890123456' expect_output = 'EA0A6CC37F40C1EA5692E7CBB8AE097653DF3E91365E6A9CD7E91312413C7BB8' TradeSha 的作用,我猜是藍新金流用來檢查他們收到的 TradeInfo 是否有被篡改。如果有被篡改,他們自己對 TradeInfo 雜湊後的結果,會跟我們傳過去的 TradeSha 值不同。 實作 from Crypto.Hash import SHA256 def get_trade_sha(trade_info: str, key: str = HashKey, iv: str = HashIV) -> str: # 串接 HashKey、TradeSha、HashKey msg = f'H...

shop_platform - 串接藍新金流:TradeInfo

Image
(本文使用的語言為 python 3) 文件:藍新金流 > API 文件下載 > 多功能收款 MPG。我下載時,版本是 1.6 TradeInfo 是什麼 根據文件第 33 頁的第一個表格,需要傳送給藍新金流的參數共有四個,其中一個就是 TradeInfo: 需要放進 TradeInfo 裡的參數欄位,如文件第 33~37 頁所列。準備好這些參數欄位後,要幫這些參數欄位加密,加密後的結果,才是要傳送給藍新金流的 TradeInfo。 加密 根據文件第 65~66 頁,可以看到,要先把參數欄位整理成 query string 的格式,才進行 AES 加密。AES 加密採用 CBC 模式,padding 的 blocksize 是 32。然後把加密後的結果轉成 16 進位。test case 為: data = { 'MerchantID': 3430112, 'RespondType': 'JSON', 'TimeStamp': 1485232229, 'Version': 1.4, 'MerchantOrderNo': 'S_1485232229', 'Amt': 40, 'ItemDesc': 'UnitTest' } Key = '12345678901234567890123456789012' IV = '1234567890123456' expect_output = 'ff91c8aa01379e4de621a44e5f11f72e4d25bdb1a18242db6cef9ef07d80b0165e476fd1d9acaa53170272c82d122961e1a0700a7427cfa1cf90db7f6d6593bbc93102a4d4b9b66d9974c13c31a7ab4bba1d4e0790f0cbbbd7ad64c6d3c8012a601ceaa808bff70f94a8efa5a4f984b9d41304ffd879612177c622f75f4214...

用 nvm alias default version 更改預設的 node 版本,但 shell 重開後依然是原來的 node 版本

結論 根據官網 Automatically call nvm use ,在 ~/.bashrc 檔案最後面加上 cdnvm() { cd "$@"; nvm_path=$(nvm_find_up .nvmrc | tr -d '\n') # If there are no .nvmrc file, use the default nvm version if [[ ! $nvm_path = *[^[:space:]]* ]]; then declare default_version; default_version=$(nvm version default); # If there is no default version, set it to `node` # This will use the latest version on your machine if [[ $default_version == "N/A" ]]; then nvm alias default node; default_version=$(nvm version default); fi # If the current version is not the default version, set it to use the default version if [[ $(nvm current) != "$default_version" ]]; then nvm use default; fi elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then declare nvm_version nvm_version=$(<"$nvm_path"/.nvmrc) declar...

shop_platform - 路由規劃

商品相關路由 ✓ 瀏覽所有商品 GET /products ✓ 瀏覽單一商品 GET /products/<string:product_id> ✓ 瀏覽某位賣家所有商品 GET /products/seller/<string:seller_id> ✓ 新增一筆商品頁面 GET /products/new ✓ 新增一筆商品 POST /products/new ✓ 修改一筆商品頁面 GET /products/edit/<string:product_id> ✓ 修改一筆商品 POST /products/edit/<string:product_id> ✓ 刪除一筆商品 POST /products/delete/<string:product_id> 購物車相關路由 ✓ 瀏覽購物車中所有商品 GET /carts ✓ 在首頁,或單一商品頁新增一筆商品 POST /carts/<string:product_id> ✓ 在購物車頁面修改一筆商品 POST /cars/edit/<string:product_id> ✓ 在購物車頁面刪除一筆商品 POST /carts/delete/<string:product_id> 訂單相關路由 ✓ 瀏覽所有訂單 GET /orders ✓ 瀏覽一筆訂單 GET /orders/<string:order_id> ✓ 結帳購物車中某位賣家的商品 POST /orders/order_item/<string:seller_id> ✓ 瀏覽結帳明細 GET /orders/checkout/<string:order_id> ✓ 完成訂單結帳 POST /orders/checkout/<string:order_id> ✓ 確認付款明細 GET /orders/payment/<string:order_id> ✓ 藍新金流 return url POST /orders/newebpay/return ✓ 藍新金流 not...

[LeetCode] #442. Find All Duplicates in an Array

題目 Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice , return an array of all the integers that appears twice . You must write an algorithm that runs in  O(n)  time and uses only constant extra space. 直覺解 構想: 用 seen 把出現過的數字存起來。如果遇到已經在 seen 裡面的數字,那就是重複的(不符合 uses only constant extra space)。 python 3 實作: class Solution: def findDuplicates(self, nums: List[int]) -> List[int]: ans = [] seen = {} for num in nums: if num not in seen: seen[num] = True else: ans.append(num) return ans 效能: Runtime: 351 ms, faster than 76.63% of Python3 online submissions for Find All Duplicates in an Array. Memory Usage: 22.8 MB, less than 19.60% of Python3 online submissions for Find All Duplicates in an Array. 研究別人的解 來源:sample 308 ms submission 構想:List comprehension 和 set(不符合 uses only const...