Python 程式設計 - 高階

Python mock 模組 - 淺談 spec, return_value, side_effect, wraps - Part 1

本文為 unittest.mock 系列文章:

Python 的測試案例(test case)寫到最後,免不了都會用上 unittest.mock 模組,原因在於有些類別、資料用 mock 的方式製造,遠遠方便於實際執行過一遍,例如測試使用者利用臉書登入,利用 mock 模擬實際臉書伺服器的回應遠比實際執行來得方便之外,也更加確保測試案例的可控制性,不會因為臉書伺服器故障或者網路問題而導致測試案例不穩定的情況發生。

然而 mock 對於初學者而言並不是十分容易理解,本文就談談 mock 中幾個重要的功能,了解這些功能之後,肯定能夠打通 mock 模組的任督二脈!

Posted on  Dec 16, 2018  in  Python 程式設計 - 高階  by  Amo Chen  ‐ 3 min read

Python - QueueHandler & QueueListener 範例

Python logging 模組提供很多內建的 handlers ,可以依照各種不一樣的使用情況選擇不同的 handler 處理日誌(log)。

然而,關於 QueueHandlers 的說明最為吸引我的目光:

Along with the QueueListener class, QueueHandler can be used to let handlers do their work on a separate thread from the one which does the logging.

This is important in Web applications and also other service applications where threads servicing clients need to respond as quickly as possible, while any potentially slow operations (such as sending an email via SMTPHandler) are done on a separate thread.

QueueHandler 與 QueueListener 讓我們能夠把 logging 的功能用另外的執行緒(thread)執行,如此一來像是 SMTPHandler 這種耗時間會拖慢 Web 應用或是其他服務的 handler 就可以在另外的執行緒執行,讓重要的服務能夠盡快地回應用戶,避免被 logging 拖慢回應速度。

Posted on  Nov 30, 2018  in  Python 程式設計 - 高階  by  Amo Chen  ‐ 2 min read

用 Python 學 Google Protocol Buffers - Part 1

本文為系列教學:

What I Learned from Quip on How to Build a Product on 8 Different Platforms with Only 13 Engineers 一文說明 Quip 如何用僅 13 人的人力同時建置 8 種不同平台的產品,十分值得借鏡。

該文有個很重要的概念 - Build once, use multiple times ,就是提倡減少重複打造相同元件的過程,提高元件的再利用率。而該文也揭露 Quip 大量使用 Google Protocol Buffers ,透過 Google Protocol Buffers 定義資料結構之後,就能夠在各個語言或平台上自動化產生能夠讀寫相同資料結構的程式碼,甚至能夠作為資料交換格式在各種不同平台間傳遞,降低重複開發的成本進而增加開發效率。

如此方便的工具怎能夠放過,本篇就用 Python 學習 Google Protocol Buffers 吧!

Posted on  Oct 27, 2018  in  Python 程式設計 - 高階  by  Amo Chen  ‐ 4 min read