关于 python
用于编写、重构、审查和优化 Python 代码的开发技能。内容涵盖惯用风格、现代语言特性、项目结构、工具配置、错误处理及 pytest 测试模式。
主要功能
- 01PEP 8 风格与类型提示
- 02现代 Python 惯用写法
- 03错误处理指导
- 04项目结构与工具配置
- 05pytest 测试模式
适用场景
- 01编写 Python 代码
- 02重构 Python 项目
- 03审查 Python 代码
- 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+)
- 优先使用 f-strings,而非 .format()
- 使用 pathlib,而非 os.path
现代特性
# 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