Amo Chen

Python string.Formatter - 取得 format string 中的 placeholders

有時候我們需要取得 format string(例如: This is a format string: Hello, {name} )中的 placeholders(例如: {name} )進行比較,例如檢查翻譯人員是否不小心把翻譯字串中的 placeholder 給遺漏了,因為這可能會造成翻譯後的字串漏字。

所幸這種需求可以透過 string.Formatter 幫忙取得字串中的 placeholders 。

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

Python threading event objects 溝通範例教學

The simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.

Threads(執行緒)之間溝通最簡單的方式,即是透過 Event Objects ,這種方式通常應用在 1 個 thread 發起 1 個 event ,然後其他 threads 會等待發出 event 的 thread ,譬如 1 個發號施令的 thread ,其他 threads 會等待該 thread 發號施令後才開始工作。

Last updated on  Jul 24, 2023  in  Python 程式設計 - 中階  by  Amo Chen  ‐ 2 min read

利用 Linux 指令 envsubst 產生設定檔

最近在 k Docker 相關文件時,發現一個好用的指令 envsubst ,因此特別紀錄一下。

The envsubst program substitutes the values of environment variables.

因為撰寫 Dockerfile 時會有將設定檔放進 Docker 內的需求,而設定檔中又有些必須設定為變數,視不同情況寫不一樣的值進去,例如針對開發環境跟生產環境的區別有不一樣的設定。

這時最簡單的方法是利用環境變數(environment variables)搭配 envsubst 來產生設定檔。

Posted on  Feb 23, 2018  in  Unix-like 命令列教學  by  Amo Chen  ‐ 1 min read

魔鬼細節 - Javascript Comma Operator

最近工作的過程偶然發現一個神奇的錯誤,覺得蠻值得紀錄成文章與大家分享的。

原因是有一段 Javascript 大致上長得像以下程式碼片段,但卻不管怎樣都會進 if 區塊:

if(response.statusCode == 200 && _.contains(['a', 'b']), response.status) {
    console.log(true);
}

Posted on  Feb 5, 2018  in  Javascript  by  Amo Chen  ‐ 1 min read

用 Google Sheets 製作簡易爬蟲

Google Sheets 有內建幾個能夠用來擷取網頁資料的函式(function) ,例如 IMPORTXML , IMPORTHTML , IMPORTDATA 等等。

如果只是簡單的需求,不妨可以考慮用這些函數來幫忙擷取資料。

Posted on  Dec 17, 2017  in  Google 試算表  by  Amo Chen  ‐ 2 min read

Google Sheets VLOOKUP HLOOKUP MATCH 範例解說

Google 試算表(Spreadsheets)是個好工具,內建不少跟 Excel 相似的函數能夠使用,所以會一點 EXCEL 的人都能夠輕鬆上手。如果用來取代 Excel 應該是問題不大,因此我也常常會推薦使用 Google Spreadsheets 統計數據、製作表單等等。

本篇介紹 3 個實用的 Google Spreadsheets 函數:

  1. VLOOKUP
  2. HLOOKUP
  3. MATCH

Posted on  Dec 5, 2017  in  Google 試算表  by  Amo Chen  ‐ 3 min read