python について
Python コードの作成、リファクタリング、レビュー、最適化に使う開発スキルです。慣用的なスタイル、現代的な言語機能、プロジェクト構成、ツール設定、例外処理、pytest のパターンを扱います。
主な機能
- 01PEP 8 のスタイルと型ヒント
- 02現代的な Python の慣用表現
- 03例外処理のガイダンス
- 04プロジェクト構成とツール設定
- 05pytest のテストパターン
活用シーン
- 01Python コードの作成
- 02Python プロジェクトのリファクタリング
- 03Python コードのレビュー
- 04性能の分析と改善
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ htlin222/ dotfiles --skill pythonインストール後、Claude Code で/pythonを入力して読み込まれたか確認します。
詳細
Python 開発
クリーンで高性能な、Python らしいコードを書きます。
使用する場面
- Python コードを書く
- Python プロジェクトをリファクタリングする
- パフォーマンスを最適化する
- Python ツールをセットアップする
- Python コードをレビューする
Python のベストプラクティス
コードスタイル
- PEP 8 に従う
- 型ヒントを使用する(Python 3.9 以降)
- .format() より f-string を優先する
- os.path より pathlib を使用する
モダンな機能
# Type hints
def process(data: list[dict]) -> dict[str, int]:
...
# Dataclasses
from dataclasses import dataclass
@dataclass
class User:
name: str
email: str
active: bool = True
# Context managers
from contextlib import contextmanager
@contextmanager
def timer():
start = time.time()
yield
print(f"Elapsed: {time.time() - start:.2f}s")
# Generators for memory efficiency
def read_large_file(path):
with open(path) as f:
yield from f
エラー処理
# Custom exceptions
class ValidationError(Exception):
def __init__(self, field: str, message: str):
self.field = field
self.message = message
super().__init__(f"{field}: {message}")
# Proper exception chaining
try:
process()
except ValueError as e:
raise ProcessingError("Failed to process") from e
プロジェクト構成
project/
├── src/
│ └── package/
│ ├── __init__.py
│ └── module.py
├── tests/
│ └── test_module.py
├── pyproject.toml
└── README.md
ツールのセットアップ
# pyproject.toml
[tool.ruff]
line-length = 88
select = ["E", "F", "I", "UP"]
[tool.mypy]
strict = true
[tool.pytest.ini_options]
testpaths = ["tests"]
テストのパターン
import pytest
@pytest.fixture
def sample_data():
return {"key": "value"}
def test_process(sample_data):
result = process(sample_data)
assert result["status"] == "success"
例
入力:「この Python コードをリファクタリングしてください」 アクション: PEP 8 を適用し、型ヒントを追加し、ロジックを簡潔にし、エラー処理を改善する
入力:「これを高速化してください」 アクション: プロファイリングを行い、ボトルネックを特定し、ジェネレーターやキャッシュを使用して、改善を検証する
コメント
Markdown 対応