Category: Python

  • concurrent.futures —  創立非同步任務 — 你所不知道的 Python 標準函式庫用法 06

    concurrent.futures — 創立非同步任務 — 你所不知道的 Python 標準函式庫用法 06

    concurrent.futures – Launching parallel tasks The concurrent.futures module provides a high-level interface for asynchronously executing callables. 官方介紹文件:17.4. concurrent.futures — Launching parallel tasks concurrent.futures 提供了一組高階 API 給使用者操作非同步執行的任務。透過 ThreadPoolExectuor 執行 thread 層級的非同步任務,或是使用 ProcessPoolExecutor 執行 process 層級的非同步任務。兩者的 API 介面都相同,同樣繼承於 Executor。 01. Quickstart Tutorial 第一個範例的 target function 使用大家最喜歡的遞迴費氏數列: def fib(n): if n < 2: return 1 return fib(n – 1) + fib(n – 2)…

  • linecache — 你所不知道的 Python 標準函式庫用法 05

    linecache — 你所不知道的 Python 標準函式庫用法 05

    linecache – Random access to text lines The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. 官方介紹文件:11.9. linecache — Random access to text lines linecache 這個模組旨在讓其他 Python library 能夠高效率的取得其他模組或套件的原始碼,在整個標準函式庫中被 import 了 21 次,主要是被 traceback 以及…

  • weakref — 你所不知道的 Python 標準函式庫用法 04

    weakref — 你所不知道的 Python 標準函式庫用法 04

    weakref – Weak References The weakref module allows the Python programmer to create weak references to objects. 官方介紹文件:8.8. weakref — Weak references weakref 函式庫是用來建立對物件的弱引用 (weak reference)。弱引用並不能保證被引用的物件能存活而不受 garbage collector 回收。弱引用通常會使用在 circular reference、caches 或是需要存放大型物件的地方 (上一篇文章談到的 abc 就有使用 WeakSet 來存放 Class attributes)。 01. Quickstart Tutorial 要理解前面一句話,你必須要先了解,在 CPython 中是對每個物件 (還記得 Everytething in Python is Object 嗎?) 採用 reference count 這樣的方式來達成記憶體管理。當物件 A 被建立的時候,其 reference count 會被設定為 1,如果有其他物件…

  • abc — 抽象類別 — 你所不知道的 Python 標準函式庫用法 03

    abc — 抽象類別 — 你所不知道的 Python 標準函式庫用法 03

    abc — Abstract Base Classes This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python. (See also PEP 3141 and the numbers module regarding a type hierarchy for numbers based on ABCs.) 官方介紹文件:29.7. abc — Abstract Base Classes 01. Quickstart Tutorial 使用 Python abc 套件的原因,是為了要解決…

  • 說明文件如何運作,並且應用在你的專案 – PyCon 2017 How documentation works, and how to make it work for your project

    說明文件如何運作,並且應用在你的專案 – PyCon 2017 How documentation works, and how to make it work for your project

    How documentation works, and how to make it work for your project By Daniele Procida, video: youtube. article: What nobody tells you about documentation   TL; DR: 4-dimensional goals. TUTORIALS HOW-TO GUIDE DISCUSSIONS REFERENCE

  • random —  你所不知道的 Python 標準函式庫用法 02

    random — 你所不知道的 Python 標準函式庫用法 02

    random – 產生偽隨機亂數 This module implements pseudo-random number generators for various distributions. 官方介紹文件:9.6. random — Generate pseudo-random numbers 如果要產生亂數密碼或是 token,請使用 secrets 模組,絕對不要使用 random 模組來產生密碼。 亂數狀態相關 01. random.seed(a=None, version=2) random.seed() 初始化亂數種子。a 如果是 None 則使用目前的亂數種子。a 可以是 str, bytes, bytearray, 都會被轉型成 int。 02. random.getstate() random.getstate() 取得亂數器內部狀態。 03. random.setstate(state) random.setstate() 設定亂數器內部狀態,state需為 random.getstate() 獲得之 state。 使用範例: >>> import random >>> random.seed(‘foobar’) #…

  • sys — 你所不知道的 Python 標準函式庫用法 01

    sys — 你所不知道的 Python 標準函式庫用法 01

    sys — 系統相關的參數以及函式 This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. 官方介紹文件:29.1 sys – System-specific parameters and functions sys 是一個極其強大的 Python 標準函式庫,大家常常忽略他的存在,但是當要做到一些奇淫技巧的時候,就會需要使用 sys 來達成。 01. sys.argv – dealing with command line arguments sys.argv 用來表示從 command line 傳入的參數,sys.argv[0] 代表著程式的名稱。如果你對 C 熟悉的話,就會想起這個東西 int main(int…

  • Diagnosing and Fixing Reference Leaks in CPython

    Diagnosing and Fixing Reference Leaks in CPython

    CPython’s garbage collection relies on each object’s reference count. Each object has their own reference count, when the object is referenced by others, then we will need to increase object’s reference count by the Py_INCREF macro. In another way, when the referencer don’t need the object anymore, it will need to decrease object’s reference count by the Py_DECREF macro. When object’s…

  • 深入 GIL: 如何寫出快速且 thread-safe 的 Python – Grok the GIL: How to write fast and thread-safe Python

    深入 GIL: 如何寫出快速且 thread-safe 的 Python – Grok the GIL: How to write fast and thread-safe Python

    本文將會探討 Python 內部的 Global Interpreter Lock,以及學習其如何影響 multi-threaded 程式。  原作者:A. Jesse,Twitter: @jessejiryudavis 原文:Grok the GIL: How to write fast and thread-safe Python Louie Lu 經作者同意[1][2]翻譯為正體中文。 當我6歲時,我有一個音樂盒。我將他上緊發條,在上面的芭蕾舞者開始繞圈,而盒子內的機關開始敲打,發出「一閃一閃亮晶晶」的聲音。雖然這東西肯定很廉價,不過我喜歡這個音樂盒,而我想要知道他是怎麼運作的。總之,我打開了這個音樂盒,看到了裏面的裝置 ── 一個我拇指大小的金屬圓筒,安裝得當的讓他可以旋轉,透過凸起的牙齒與鋼梳撞擊後發出音符。 在程式設計師的特點中,關於事情如何運作的好奇心是必不可缺的。當我打開我的音樂盒觀看內部時,我展現我可能是一個 ── 如果不是一個頂尖程式設計師 ── 起碼也會是好奇的一個。 這很奇怪,我在對 Global Interpreter Lock (GIL) 誤解的情況下寫了 Python 程式這麼久,因為我還沒有足夠的好奇心來了解他是如何運作的。我遇到很多人跟我有著同樣的猶豫,以及無知。 該是時候把這個黑盒子翹開了。讓我們閱讀 CPython 原始碼來了解什麼是 GIL,為什麼 Python 會有,以及他是如何影響我們撰寫 multi-threaded 程式。我會給出一些範例來讓你了解 GIL。你會學到如何快速寫出 thread-safe 的 Python,以及如何在 threads 與 processes 之間選擇。…

  • 我對 PyCon US 2017 感興趣的 18 個 Talks

    我對 PyCon US 2017 感興趣的 18 個 Talks

    PyCon US 2017 即將在美國奧瑞岡開始,快速瀏覽了一下 talk list,真不愧是 PyCon US,每個題目都看起來非常的高大上而且紮實。以下是我挑選 18 個我有興趣的主題,記錄下來。 完整的 talk list 可以參考這邊: PyCon US 2017 Talks Schedule: https://us.pycon.org/2017/schedule/talks/ PyCon US 2017 Talks List: https://us.pycon.org/2017/schedule/talks/list/ 排列順序依照 Talks List 由上而下排列,順序不代表我對於主題的興趣多寡: aiosmtpd – A better asyncio based SMTP server Barry Warsaw Sunday 2:30 p.m.–3 p.m. in Oregon Ballroom 203–204 講關於 async 版本的 smtpd 實作。總之因為還沒有搞清楚 3.5 引入的 async / await,因此對於有關…