テーマ: python
-
サポート終了後の Python 3.10:ランタイムの洗い出しと環境の再構築
Python の上流のライフサイクル表では、Python 3.10 のサポート終了日は2026年10月1日とされている。サービス、仮想環境、イメージが実際に使っているインタープリターを洗い出し、各アプリケーションをサポート中のブランチで再構築してテストする。
-
Pythonのコマンドラインツールを設計する: argparse、main()、終了コード
インターフェースはコンソールスクリプトとして登録した`main(argv) -> int`関数にまとめ、argparseの`type`と`choices`で検証し、終了コードの慣習(0は成功、2は使用法エラー、1はその他の失敗、sysexitsのコードは文書化されている場合にのみ)に従い、結果はstdoutに、診断情報はstderrに出力し、SIGINTとbroken pipeを処理する。
-
中央値・パーセンタイル・比率のブートストラップ信頼区間
生の観測値を復元抽出で何度もリサンプリングし、各リサンプルについて統計量を計算し、得られた分布から区間を読み取る。これにより、教科書の公式が通用しない中央値・パーセンタイル・比率・差分についての不確実性を求められる。手法名、リサンプリング回数、サンプルサイズを報告し、小さなサンプルの極端なパーセンタイルについては信用しないこと。
-
npmとPyPIの公開IDを承認済みリリースアーティファクトに結び付ける
OIDCのtrusted publishingは公開トークンの保存を不要にするが、その安全性は引き続き承認されたワークフローに依存する。ビルドと公開を分離し、承認を保護したうえで、provenanceをマルウェア判定と見なさず、アーティファクトが意図したものかを検証する。
-
生成した入力によるプロパティベーステスト
手作業で選んだ例の代わりに、不変条件をテストとして記述し、ライブラリに多数の入力を生成させる。失敗した入力は最小の反例へ縮小される。Hypothesis は Python における参照実装である。
-
Python のコンテキストマネージャー:後片付けを確実に行う
with 文はブロックの前後で __enter__ と __exit__ を実行し、例外が起きてもファイル、ロック、接続を解放する。contextlib.contextmanager はジェネレーターをコンテキストマネージャーに変える。
-
ツールにも読み手にも役立つ docstring
PEP 257 は docstring の配置と書式を定めています。Google または NumPy などの統一したスタイルで、一行の要約、引数と戻り値の説明、送出する例外を記すと、読み手、エディター、ドキュメント生成ツールが活用できます。
-
浮動小数点数:0.1 + 0.2 が 0.3 にならない理由
二進浮動小数点は、多くの十進小数を近似して表すため、計算で丸め誤差が蓄積します。許容誤差で比較し、慎重に合計し、金額や件数には整数か十進型を使い、元の値に戻せるだけの桁数で出力します。
-
Loading YAML safely
Full YAML loaders can instantiate arbitrary objects from tagged nodes; always use a safe loader, pin the YAML version semantics, and validate the result against a schema before use.
-
Glob pattern dialects: what * and ** mean in .gitignore, .dockerignore, bash, Python and GitHub Actions
The same star and double-star characters mean different things depending on where the pattern is evaluated: a slash-less .gitignore pattern matches at every depth while the same line in .dockerignore only matches at the context root, bash's globstar recursion is opt-in, and Python's fnmatch is not even path-aware.
-
asyncio が役立つ場合と役立たない場合
asyncio は await の地点で処理を中断し、一つのスレッドで多数の I/O 待ちのタスクを動かす。CPU 処理が支配的なコードを速くするものではなく、コルーチン内のブロッキング呼び出しはループ全体を止める。
-
自動整形と lint をチームの約束にする
レイアウトをフォーマッターに、機械的なチェックをリンターに任せると、レビューからスタイルの議論を取り除ける。EditorConfig、PEP 8、Ruff などのツールは、その約束をリポジトリに記述する方法を示している。
-
Protocol classes: structural typing for duck-typed Python
A typing.Protocol declares the methods and attributes a consumer needs; any object that has them satisfies the type without inheriting from it. Define protocols next to the code that depends on them, keep them minimal, and use runtime_checkable only where isinstance is really needed.
-
tracemalloc のスナップショットでメモリーリークを探す
PYTHONTRACEMALLOC または tracemalloc.start(nframe) で早めに追跡を開始します。ウォームアップ後と N 回の反復後にスナップショットを取り、インポートの雑音を除いて、compare_to(..., 'lineno') からサイズが N に比例して増える行を探します。呼び出し元を見るには 'traceback' 単位の集計に切り替えます。
-
Money and other exact quantities: use Decimal, not float
Binary floating point cannot represent most decimal fractions exactly, so sums of prices drift; the decimal module provides exact decimal arithmetic with explicit rounding, and integers in minor units are an alternative.
-
Pythonの処理にスレッド、プロセス、asyncioのどれを選ぶか
まず頻繁に通る処理経路を分類します。I/O待ちにはasyncio(多数の接続、非同期ライブラリ)かスレッドプール(少数のブロッキング呼び出し)が向きます。純粋なPythonのCPU処理にはプロセスかフリースレッドビルドが必要で、GILを解放するネイティブコードにはスレッドを使えます。各プールに上限を設け、プロセスの起動方式を明示し、終了処理を書きます。
-
Dataclasses for plain records
dataclasses generate __init__, __repr__ and equality from annotated fields; frozen dataclasses give immutable value objects, slots reduce memory, and field(default_factory=...) avoids shared mutable defaults.
-
At what workload does the free-threaded CPython build beat a process pool for a mixed I/O and CPU service?
Open question: the free-threaded build removes the GIL but adds single-threaded overhead and may fall back to the GIL when an unprepared extension is imported, while process pools pay for pickling and memory duplication; for which CPU-to-wait ratios, working sets and core counts does a thread pool on the free-threaded build deliver more throughput per core?
-
Attempting an exercise before reading its worked solution yields better one-month recall of a language feature than reading the solution first
Hypothesis: for an engineer learning a specific language feature, such as Python's match statement, working an exercise before seeing the worked solution leads to better delayed recall and transfer than studying the solution first and then working a similar exercise; a proposed within-person test with two features, two orders and a one-month delayed test, with no result claimed.
-
asyncio のキャンセル:CancelledError を握りつぶさずに後始末する
中断された経路でリソースを解放しながら、タスクの所有者がキャンセルを観測できる状態を保ちます。
機械可読: JSON