Category: ADAPSL

  • pathlib — 物件導向的檔案系統路徑 — 你所不知道的 Python 標準函式庫用法 12

    pathlib — 物件導向的檔案系統路徑 — 你所不知道的 Python 標準函式庫用法 12

    pathlib – 物件導向的檔案系統路徑 This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations. Source code: Lib/pathlib.py   01. Quickstart Tutorial 使用 pathlib.Path 來表示一個路徑,對於 Path 物件,我們可以透過 / 來串接路徑: >>> import…

  • inspect — 探測 Python 物件內容 — 你所不知道的 Python 標準函式庫用法 11

    inspect — 探測 Python 物件內容 — 你所不知道的 Python 標準函式庫用法 11

    inspect – 探測 Python 物件內容 The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for…

  • secrets — 生成用於密碼層級的亂數 — 你所不知道的 Python 標準函式庫用法 10

    secrets — 生成用於密碼層級的亂數 — 你所不知道的 Python 標準函式庫用法 10

    secrets – 生成用於密碼層級的亂數 The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. Source code: Lib/secrets.py secrets 模組的用意,是為了要在 Python 內提供一個適用於密碼層級的亂數產生機制,透過 secrets 我們可以輕鬆的產生密碼強度的亂數以及亂數選擇元素,從而避免使用者使用 random 模組來產生隨機亂數並將之用於密碼上。這個模組目前只在 Python 3.6 以後的版本才有,請各位更新到最新版本來使用。 01. Quickstart Tutorial 產生密碼強度的隨機亂數: >>> import secrets >>> secrets.token_bytes() b’\x1c\xc7\xd0H\xddY\xdb\x00\xf2\xe0\xd9%\xa2<|T\xcf\xe5\x937\xbc\x15(\xce~p\xca9\xc3u\xed)’ >>> secrets.token_bytes() b’\x13 \x87\xa2\x95]\n\xec\x80\x16=)\x11}4\x17\x07\xf8\t\x99\x16\xb3\xaf\x97u\x8a\x08uY*\xa8\x83′ >>> 產生密碼強度的隨機字串 >>> secrets.token_hex() ‘e41210fd028b4fdb8d6d58f9f22a080068afc1478b866d09a7d220c0229031cd’…

  • tracemalloc — 記憶體配置除錯 — 你所不知道的 Python 標準函式庫用法 09

    tracemalloc — 記憶體配置除錯 — 你所不知道的 Python 標準函式庫用法 09

    tracemalloc – 記憶體配置除錯 The tracemalloc module is a debug tool to trace memory blocks allocated by Python. Source code: Lib/tracemalloc.py 透過 tracemalloc 將可以記錄下 Python 的記憶體區塊配置狀況,同時透過這些記錄來輸出有用的記憶體配置資訊,例如說執行記憶體配置的檔案以及行數、記憶體區塊大小等。也可以透過比對兩個不同的記憶體區塊快照,來找出記憶體洩漏的問題。 01. Quickstart Tutorial 我們透過 start() 開始記憶體除錯的功能: >>> import tracemalloc >>> tracemalloc.start() 結束時使用 stop() 結束除錯: >>> tracemalloc.stop() 想要記錄下當前的記憶體配置就使用 snapshot(): >>> snap = tracemalloc.take_snapshot() 認識這三個基本 API 後,我們就可以開始應用 tracemalloc。 # tm_snap_stats.py import tracemalloc import sys #…

  • atexit — 程式關閉時回呼 — 你所不知道的 Python 標準函式庫用法 08

    atexit — 程式關閉時回呼 — 你所不知道的 Python 標準函式庫用法 08

    atexit – Exit handlers The atexit module defines functions to register and unregister cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination. atexit runs these functions in the reverse order in which they were registered; if you register A, B, and C, at interpreter termination time they will be run in the order C, B, A. 官方介紹文件:29.8. atexit — Exit handlers atexit 套件用來註冊當程式結束時要執行的函式。被 atexit 註冊的函式,會在程式正常結束的時機點被執行。執行的順序為先註冊後執行…

  • enum — 枚舉型態 — 你所不知道的 Python 標準函式庫用法 07

    enum — 枚舉型態 — 你所不知道的 Python 標準函式庫用法 07

    enum – Support for enumerations An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over. 官方介紹文件:8.13. enum — Support for enumerations enum 是 Python 裏用來建立枚舉形態的標準函式庫。enum 算是比較新的標準函式庫,學習 enum 可以讓你輕鬆建立枚舉,改寫以前單獨使用 const variable 的狀況。 01. Quickstart Tutorial 從今天開始,讓我們把這樣枚舉的 Code: #…

  • 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 套件的原因,是為了要解決…