写作与翻译
Python 代码风格与文档规范
为团队项目制定命名、类型注解和文档字符串规范,并配置 Ruff 和 mypy 自动检查。
# Python 代码风格与文档
统一的代码风格和清晰的文档让代码库易于维护和协作。本技能涵盖现代 Python 工具、命名约定和文档标准。
## 何时使用本技能
- 为新项目设置代码检查和格式化
- 编写或审查文档字符串
- 制定团队编码标准
- 配置 ruff、mypy 或 pyright
- 审查代码风格的一致性
- 创建项目文档
## 核心概念
### 1. 自动格式化
让工具解决格式争议。配置一次,自动执行。
### 2. 统一命名
遵循 PEP 8 约定,使用有意义、描述性强的名称。
### 3. 将文档视为代码
文档字符串应与其描述的代码一同维护。
### 4. 类型注解
现代 Python 代码应为所有公共 API 提供类型提示。
## 快速开始
```bash
# Install modern tooling
pip install ruff mypy
# Configure in pyproject.toml
[tool.ruff]
line-length = 120
target-version = "py312" # Adjust based on your project's minimum Python version
[tool.mypy]
strict = true
```
## 基础模式
### 模式 1:现代 Python 工具
使用 `ruff` 作为集代码检查和格式化于一体的工具。它以一个快速工具取代 flake8、isort 和 black。
```toml
# pyproject.toml
[tool.ruff]
line-length = 120
target-version = "py312" # Adjust based on your project's minimum Python version
[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"UP", # pyupgrade
"SIM", # flake8-simplify
]
ignore = ["E501"] # Line length handled by formatter
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
```
运行命令:
```bash
ruff check --fix . # Lint and auto-fix
ruff format . # Format code
```
### 模式 2:类型检查配置
为生产代码配置严格的类型检查。
```toml
# pyproject.toml
[tool.mypy]
python_version = "3.12"
strict = true
warn_return_any = true
warn_unused_ignores = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
[[tool.mypy.overrides]]
module = "tests.*"
disallow_untyped_defs = false
```
替代方案:使用 `pyright` 加快检查速度。
```toml
[tool.pyright]
pythonVersion = "3.12"
typeCheckingMode = "strict"
```
### 模式 3:命名约定
遵循 PEP 8,优先保证清晰,而不是追求简短。
**文件和模块:**
```python
# Good: Descriptive snake_case
user_repository.py
order_processing.py
http_client.py
# Avoid: Abbreviations
usr_repo.py
ord_proc.py
http_cli.py
```
**类和函数:**
```python
# Classes: PascalCase
class UserRepository:
pass
class HTTPClientFactory: # Acronyms stay uppercase
pass
# Functions and variables: snake_case
def get_user_by_email(email: str) -> User | None:
retry_count = 3
max_connections = 100
```
**常量:**
```python
# Module-level constants: SCREAMING_SNAKE_CASE
MAX_RETRY_ATTEMPTS = 3
DEFAULT_TIMEOUT_SECONDS = 30
API_BASE_URL = "https://api.example.com"
```
### 模式 4:导入组织
按统一顺序分组导入:标准库、第三方库、本地模块。
```python
# Standard library
import os
from collections.abc import Callable
from typing import Any
# Third-party packages
import httpx
from pydantic import BaseModel
from sqlalchemy import Column
# Local imports
from myproject.models import User
from myproject.services import UserService
```
只使用绝对导入:
```python
# Preferred
from myproject.utils import retry_decorator
# Avoid relative imports
from ..utils import retry_decorator
```
## 进阶模式
### 模式 5:Google 风格文档字符串
为所有公共类、方法和函数编写文档字符串。
**简单函数:**
```python
def get_user(user_id: str) -> User:
"""Retrieve a user by their unique identifier."""
...
```
**复杂函数:**
```python
def process_batch(
items: list[Item],
max_workers: int = 4,
on_progress: Callable[[int, int], None] | None = None,
) -> BatchResult:
"""Process items concurrently using a worker pool.
Processes each item in the batch using the configured number of
workers. Progress can be monitored via the optional callback.
Args:
items: The items to process. Must not be empty.
max_workers: Maximum concurrent workers. Defaults to 4.
on_progress: Optional callback receiving (completed, total) counts.
Returns:
BatchResult containing succeeded items and any failures with
their associated exceptions.
Raises:
ValueError: If items is empty.
ProcessingError: If the batch cannot be processed.
Example:
>>> result = process_batch(items, max_workers=8)
>>> print(f"Processed {len(result.succeeded)} items")
"""
...
```
**类文档字符串:**
```python
class UserService:
"""Service for managing user operations.
Provides methods for creating, retrieving, updating, and
deleting users with proper validation and error handling.
Attributes:
repository: The data access layer for user persistence.
logger: Logger instance for operation tracking.
Example:
>>> service = UserService(repository, logger)
>>> user = service.create_user(CreateUserInput(...))
"""
def __init__(self, repository: UserRepository, logger: Logger) -> None:
"""Initialize the user service.
Args:
repository: Data access layer for users.
logger: Logger for tracking operations.
"""
self.repository = repository
self.logger = logger
```
### 模式 6:行长度与格式
为适应现代显示设备,将行长度设为 120 个字符,同时保持可读性。
```python
# Good: Readable line breaks
def create_user(
email: str,
name: str,
role: UserRole = UserRole.MEMBER,
notify: bool = True,
) -> User:
...
# Good: Chain method calls clearly
result = (
db.query(User)
.filter(User.active == True)
.order_by(User.created_at.desc())
.limit(10)
.all()
)
# Good: Format long strings
error_message = (
f"Failed to process user {user_id}: "
f"received status {response.status_code} "
f"with body {response.text[:100]}"
)
```
### 模式 7:项目文档
**README 结构:**
```markdown
# Project Name
Brief description of what the project does.
## Installation
\`\`\`bash
pip install myproject
\`\`\`
## Quick Start
\`\`\`python
from myproject import Client
client = Client(api_key="...")
result = client.process(data)
\`\`\`
## Configuration
Document environment variables and configuration options.
## Development
\`\`\`bash
pip install -e ".[dev]"
pytest
\`\`\`
```
**CHANGELOG 格式(Keep a Changelog):**
```markdown
# Changelog
## [Unreleased]
### Added
- New feature X
### Changed
- Modified behavior of Y
### Fixed
- Bug in Z
```
## 最佳实践总结
1. **使用 ruff** - 用一个工具完成代码检查和格式化
2. **启用严格 mypy 检查** - 在运行前捕获类型错误
3. **每行 120 个字符** - 兼顾可读性的现代标准
4. **使用描述性名称** - 清晰优先于简短
5. **使用绝对导入** - 比相对导入更易维护
6. **Google 风格文档字符串** - 统一、易读的文档
7. **为公共 API 编写文档** - 每个公共函数都需要文档字符串
8. **保持文档更新** - 将文档视为代码
9. **在 CI 中自动执行** - 每次提交都运行代码检查工具
10. **目标版本为 Python 3.10+** - 对于新项目,建议使用 Python 3.12+ 以获得现代语言特性同类提示词
写作与翻译
按叙事节奏拆分短片制作计划
提供短片构想,可补充时长和风格偏好,把故事拆成带开场钩子的场次,明确景别、动作、服装和剪辑节奏。
# 视频策划 使用此技能将用户的短片创意转化为实用的逐场景制作方案。该方案应适用于拍摄、剪辑、AI 视频生成,或与摄影师/剪辑师沟通。 最终方案应使用用户偏好的本地语言撰写。如果用户使用中文,除非明确要求其他语言,否则用中文输出方案。技能说明保持英文,但交付内容的语言应适应用户。 ## 主要目标 制作一份完整的短视频方案,满足以下要求: - 协商或推断总时长。 - 将视频划分为多个场景,每个场景至少为 4 seconds,且不超过 15 seconds。 - 在可行时优先采用 4、9、12 或 15 seconds 的场景时长。 - 使用清晰可
写作与翻译
三十天多渠道营销内容日历
给品牌资料、渠道、月目标和制作资源,按漏斗排三十天主题、发布及行动号召,发布前评分。
# 全球内容日历 > 规划一份为期 30 天的多渠道内容日历,确定漏斗比例、内容支柱和来源类型组合,并在发布前进行 AI 评分。 --- ## 第 0 步——读取上下文文件 生成日历前,读取 `.agents/product-marketing-context-global.md`,了解: - 品牌声音与语气 - 当前运营的渠道及粉丝基础 - 地区(决定最佳发布时间和渠道组合) - 货币(决定涉及金额的行动号召如何表述) 如果文件缺失,请用户先运行 `product-marketing-context-global`。 --- ## 信息
可填 1 项
写作与翻译
单场景分镜图与视频生成脚本
提供要制作的场景,按镜头时长生成分镜图和视频脚本,有现成人物图时沿用其造型。
# 视频分镜 使用此 skill 为特定场景制作一套可直接用于制作的分镜方案: 1. 将分镜图保存到 `storyboard/scene-XX.png`。 2. 将视频生成提示词脚本保存到 `storyboard/scene-XX.md`。 用英文编写 skill 指令,但面向用户的 `.md` 脚本应使用用户偏好的本地语言,除非用户明确要求使用其他语言。 ## 图像生成要求 分镜图必须由图像生成模型生成。不要用代码、SVG、HTML/CSS、canvas、图表、占位画或确定性形状脚本制作分镜画面。这些方法只可用于非视觉元数据或文件操作,绝不能
需参考图
写作与翻译
可灵短视频剧情与图生视频脚本
按单场景或多角色剧情编写生成指令,覆盖动作、运镜和音频;图生视频需上传参考图并说明变化。
# kling-prompter Kling 3.0(可灵 3.0,快手)的三套主流写法:4 部分基础 / 5 层进阶 / 图生视频。 ## 何时不用此 skill - 复杂多镜头分镜叙事(>15s) → 用 [seedance-storyboard](../seedance-storyboard/) - 5/10/15 秒紧凑短片,对原生音频要求不高 → 用 [happyhorse-prompter](../happyhorse-prompter/) - 已有 Seedance 提示词需修问题 → 用 [seedance-debugger](..
需参考图中文原创
写作与翻译
明确交付物和负责人的工作坊
提供工作坊目标与参会情况,围绕一个成果设计议程,安排分组讨论、决策和会后负责人。
# 工作坊设计师 大多数工作坊都以同一种方式失败:讨论了很多,产生了无人记录的“共识”,然后又发出下一场工作坊的日历邀请。这个技能设计的会议以**确定的成果和明确的负责人**收尾,因为只有这样才值得占用参与者一周中的时间。 检验任何工作坊的标准:**这间会议室里会产出什么成果,谁负责下一步?** 如果答不上来,就不要举办工作坊。 --- ## 设计选择 - **只设一个结果,不要三个。** 有三个目标的工作坊一个也达不成。强制排序,确定一个决策或一项成果。 - **先发散,再收敛,绝不同时进行。** 头脑风暴和决策需要不同的思维模式。把两者混在
写作与翻译
视频拍摄与剪辑交付说明
提供口播稿和品牌资料,填写日期并附参考视频,明确前三秒、拍摄、剪辑、字幕和各渠道导出规格。
# 视频剪辑简报(全局) 一份好的视频简报,应让拍摄人员清楚如何布置现场,让剪辑人员知道如何剪辑,无须再打电话解释。开头 three seconds 必须能单独成片。 **与 `04-script-video-global` 的区别:**脚本是口播内容(说什么,按钩子—主体—CTA组织)。这份简报是制作指导(拍什么、怎么剪、用什么音乐、叠加什么内容、如何导出)。如果还没有脚本,先运行 `04-script-video-global`,再将其中的台词粘贴到下方分镜表的“Dialogue / VO”列。 ## 信息收集 阅读 `.agents/pro
可填 1 项