with one click
python-patterns
Pythonic 惯用语、PEP 8 标准、类型提示和构建健壮、高效、可维护的 Python 应用的最佳实践。
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
Pythonic 惯用语、PEP 8 标准、类型提示和构建健壮、高效、可维护的 Python 应用的最佳实践。
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
REST API 设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和生产 API 的速率限制。
后端架构模式、API 设计、数据库优化和 Node.js、Express、Next.js API 路由的服务器端最佳实践。
通用编码标准、最佳实践和模式,适用于 TypeScript、JavaScript、React 和 Node.js 开发。
Everything Claude Code 的交互式安装向导——引导用户选择和安装技能和规则到用户级或项目级目录,验证路径,并可选地优化已安装的文件。
自动从 Claude Code 会话中提取可复用模式,并保存为学习技能供将来使用。
基于 instinct 的学习系统,通过 hooks 观察会话,创建带置信度评分的原子级 instincts,并将它们进化为技能/命令/代理。
| name | python-patterns |
| description | Pythonic 惯用语、PEP 8 标准、类型提示和构建健壮、高效、可维护的 Python 应用的最佳实践。 |
构建健壮、高效、可维护应用的 Pythonic 模式和最佳实践。
Python 优先考虑可读性。代码应该明显且易于理解。
# 好的写法:清晰可读
def get_active_users(users: list[User]) -> list[User]:
"""从提供的列表中返回活跃用户。"""
return [user for user in users if user.is_active]
# 不好的写法:巧妙但令人困惑
def get_active_users(u):
return [x for x in u if x.a]
避免魔法,清楚说明你的代码做什么。
# 好的写法:显式配置
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# 不好的写法:隐藏的副作用
import some_module
some_module.setup() # 这做什么?
Python 偏好异常处理而非检查条件。
# 好的写法:EAFP 风格
def get_value(dictionary: dict, key: str) -> Any:
try:
return dictionary[key]
except KeyError:
return default_value
# 不好的写法:LBYL(三思而后行)风格
def get_value(dictionary: dict, key: str) -> Any:
if key in dictionary:
return dictionary[key]
else:
return default_value
from typing import Optional, List, Dict, Any
def process_user(
user_id: str,
data: Dict[str, Any],
active: bool = True
) -> Optional[User]:
"""处理用户并返回更新的 User 或 None。"""
if not active:
return None
return User(user_id, data)
# Python 3.9+ - 使用内置类型
def process_items(items: list[str]) -> dict[str, int]:
return {item: len(item) for item in items}
# Python 3.8 及更早版本 - 使用 typing 模块
from typing import List, Dict
def process_items(items: List[str]) -> Dict[str, int]:
return {item: len(item) for item in items}
from typing import TypeVar, Union
# 复杂类型的类型别名
JSON = Union[dict[str, Any], list[Any], str, int, float, bool, None]
def parse_json(data: str) -> JSON:
return json.loads(data)
# 泛型类型
T = TypeVar('T')
def first(items: list[T]) -> T | None:
"""返回第一个项目,如果列表为空则返回 None。"""
return items[0] if items else None
from typing import Protocol
class Renderable(Protocol):
def render(self) -> str:
"""将对象渲染为字符串。"""
def render_all(items: list[Renderable]) -> str:
"""渲染所有实现 Renderable 协议的项目。"""
return "\n".join(item.render() for item in items)
# 好的写法:捕获特定异常
def load_config(path: str) -> Config:
try:
with open(path) as f:
return Config.from_json(f.read())
except FileNotFoundError as e:
raise ConfigError(f"Config file not found: {path}") from e
except json.JSONDecodeError as e:
raise ConfigError(f"Invalid JSON in config: {path}") from e
# 不好的写法:裸 except
def load_config(path: str) -> Config:
try:
with open(path) as f:
return Config.from_json(f.read())
except:
return None # 静默失败!
def process_data(data: str) -> Result:
try:
parsed = json.loads(data)
except json.JSONDecodeError as e:
# 链接异常以保留回溯
raise ValueError(f"Failed to parse data: {data}") from e
class AppError(Exception):
"""所有应用错误的基异常。"""
pass
class ValidationError(AppError):
"""输入验证失败时抛出。"""
pass
class NotFoundError(AppError):
"""请求的资源未找到时抛出。"""
pass
# 使用方式
def get_user(user_id: str) -> User:
user = db.find_user(user_id)
if not user:
raise NotFoundError(f"User not found: {user_id}")
return user
# 好的写法:使用上下文管理器
def process_file(path: str) -> str:
with open(path, 'r') as f:
return f.read()
# 不好的写法:手动资源管理
def process_file(path: str) -> str:
f = open(path, 'r')
try:
return f.read()
finally:
f.close()
from contextlib import contextmanager
@contextmanager
def timer(name: str):
"""计时代码块的上下文管理器。"""
start = time.perf_counter()
yield
elapsed = time.perf_counter() - start
print(f"{name} took {elapsed:.4f} seconds")
# 使用方式
with timer("data processing"):
process_large_dataset()
自动生成 __init__、__repr__ 和 __eq__。
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class User:
"""带有自动生成方法的数据容器。"""
id: str
name: str
email: str
created_at: datetime = field(default_factory=datetime.now)
is_active: bool = True
# 使用方式
user = User(
id="123",
name="Alice",
email="alice@example.com"
)
@dataclass
class User:
email: str
age: int
def __post_init__(self):
# 验证邮箱格式
if "@" not in self.email:
raise ValueError(f"Invalid email: {self.email}")
# 验证年龄范围
if self.age < 0 or self.age > 150:
raise ValueError(f"Invalid age: {self.age}")
import functools
import time
def timer(func: Callable) -> Callable:
"""计时函数执行的装饰器。"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
elapsed = time.perf_counter() - start
print(f"{func.__name__} took {elapsed:.4f}s")
return result
return wrapper
@timer
def slow_function():
time.sleep(1)
import concurrent.futures
def fetch_url(url: str) -> str:
"""获取 URL(I/O 密集型操作)。"""
import urllib.request
with urllib.request.urlopen(url) as response:
return response.read().decode()
def fetch_all_urls(urls: list[str]) -> dict[str, str]:
"""使用线程并发获取多个 URL。"""
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
future_to_url = {executor.submit(fetch_url, url): url for url in urls}
results = {}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
results[url] = future.result()
except Exception as e:
results[url] = f"Error: {e}"
return results
def process_data(data: list[int]) -> int:
"""CPU 密集型计算。"""
return sum(x ** 2 for x in data)
def process_all(datasets: list[list[int]]) -> list[int]:
"""使用多进程处理多个数据集。"""
with concurrent.futures.ProcessPoolExecutor() as executor:
results = list(executor.map(process_data, datasets))
return results
import asyncio
async def fetch_async(url: str) -> str:
"""异步获取 URL。"""
import aiohttp
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def fetch_all(urls: list[str]) -> dict[str, str]:
"""并发获取多个 URL。"""
tasks = [fetch_async(url) for url in urls]
results = await asyncio.gather(*tasks, return_exceptions=True)
return dict(zip(urls, results))
myproject/
├── src/
│ └── mypackage/
│ ├── __init__.py
│ ├── main.py
│ ├── api/
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── models/
│ │ ├── __init__.py
│ │ └── user.py
│ └── utils/
│ ├── __init__.py
│ └── helpers.py
├── tests/
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_api.py
│ └── test_models.py
├── pyproject.toml
├── README.md
└── .gitignore
# 不好的写法:可变默认参数
def append_to(item, items=[]):
items.append(item)
return items
# 好的写法:使用 None 并创建新列表
def append_to(item, items=None):
if items is None:
items = []
items.append(item)
return items
# 不好的写法:用 type() 检查类型
if type(obj) == list:
process(obj)
# 好的写法:使用 isinstance
if isinstance(obj, list):
process(obj)
# 不好的写法:用 == 比较 None
if value == None:
process()
# 好的写法:使用 is
if value is None:
process()
# 不好的写法:裸 except
try:
risky_operation()
except:
pass
# 好的写法:特定异常
try:
risky_operation()
except SpecificError as e:
logger.error(f"Operation failed: {e}")
记住:Python 代码应该是可读的、显式的,并遵循最小惊讶原则。有疑问时,优先考虑清晰而非巧妙。