python-design
它能做什么
该技能为 Python 脚本和工具提供设计规则。适合希望代码易于修改、也易于理解的开发者。
适用场景
- ✓当你编写或修改 Python 文件,尤其是命令行脚本的时候。
- ✓当你规划如何把一个 Python 项目拆分为模块的时候。
- ✓当你审阅 Python 代码,并希望发现设计问题的时候。
技能链接
https://github.com/mindfold-ai/Trellis/blob/main/.agents/skills/python-design/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请审阅这个 Python 脚本,并告诉我它的设计在哪里变得混乱或难以修改。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会应用一本广为人知的软件设计书中的规则,例如让模块保持“深”,即接口简单而内部功能丰富,并检查那些使代码难以修改的警示信号。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Python Design for CLI Scripts
Design patterns and principles for writing maintainable Python CLI tools and utilities. Based on A Philosophy of Software Design (Ousterhout), adapted for scripting contexts.
When to Activate
- Writing or modifying Python files
- Planning module decomposition
- Code review of Python changes
- Refactoring scripts that feel "messy"
- Adding a new subcommand or utility function
Core Thesis
The central challenge is managing complexity, not adding features.
Complexity is anything that makes code hard to understand or modify. It has three symptoms:
- Change Amplification — A small change requires edits in many places
- Cognitive Load — You must hold too much context to make a safe change
- Unknown Unknowns — You don't know what you don't know (the most dangerous)
Complexity is incremental. It accumulates through hundreds of small decisions, not one catastrophic mistake. Therefore: sweat the small stuff.
Principle 1: Deep Modules
A module's value is the ratio of functionality hidden vs. interface exposed.
Deep module (good): Shallow module (bad):
┌──────────┐ ┌──────────────────────────┐
│ simple │ │ complex interface │
│ interface│ │ many params, many methods │
├──────────┤ ├──────────────────────────┤
│ │ │ │
│ rich │ │ thin implementation │
│ impl │ │ │
│ │ └──────────────────────────┘
│ │
└──────────┘
Practical test: If a caller must understand how the module works internally to use it correctly, the module is too shallow.
Example: Task Data Access
# Shallow — caller must know JSON structure, file paths, error handling
def _read_json_file(path: Path) -> dict:
with open(path, encoding="utf-8") as f:
return json.load(f)
# Every caller does this independently:
task_path = tasks_dir / name / "task.json"
data = _read_json_file(task_path)
title = data.get("title") or data.get("name", "")
status = data.get("status", "planning")
assignee = data.get("assignee", "")
# Deep — caller gets what they need, module hides JSON/path/parsing
@dataclass(frozen=True)
class TaskInfo:
name: str
title: str
status: str
assignee: str
priority: str
directory: Path
def load_task(tasks_dir: Path, name: str) -> TaskInfo | None:
"""Load task by directory name. Returns None if not found."""
...
def list_active_tasks(tasks_dir: Path) -> list[TaskInfo]:
"""List all non-archived tasks, sorted by priority."""
...
The deep version absorbs complexity: JSON parsing, field defaults, directory scanning, archive filtering. Callers just work with typed data.
Principle 2: Type-First Development
Types define contracts before implementation. This workflow catches design problems early:
- Define data shapes — dataclass or TypedDict first
- Define function signatures — parameter and return types
- Implement to satisfy types — let the type checker guide completeness
- Validate at boundaries — runtime checks only where data enters the system
Frozen Dataclasses for Internal Data
from dataclasses import dataclass
from typing import Literal
@dataclass(frozen=True)
class AgentRecord:
agent_id: str
task_name: str
worktree_path: Path
platform: Literal["Codex", "codex", "cursor"]
status: Literal["running", "done", "failed"]
branch: str
Frozen dataclasses are immutable — no accidental mutation, safe to pass around.
TypedDict for External JSON Shapes
When the data comes from a file (task.json, config.yaml, registry.json), use TypedDict to document the expected shape:
from typing import TypedDict, Required, NotRequired
class TaskData(TypedDict):
title: Required[str]
status: Required[str]
assignee: NotRequired[str]
priority: NotRequired[str]
parent: NotRequired[str]
children: NotRequired[list[str]]
This eliminates scattered .get("field", default) calls — the shape is documented once.
NewType for Domain Primitives
When two strings mean different things, make the type system enforce it:
from typing import NewType
TaskName = NewType("TaskName", str) # directory name like "03-10-v040"
BranchName = NewType("BranchName", str)
常见问题
它会寻找哪些问题?
它会寻找那些会扩散到许多地方的改动、沉重的心智负担,以及使代码修改风险很高的隐藏意外。
它只适用于网页应用吗?
不是。该技能是为脚本和命令行工具编写的,因此最适合这些场景。
它会添加新功能吗?
不会。核心理念是管理复杂性,而不是增加功能。
详情
作者的原始描述
Python design patterns for CLI scripts and utilities — type-first development, deep modules, complexity management, and red flags. Use when reading, writing, reviewing, or refactoring Python files, especially in .trellis/scripts/ or any CLI/scripting context. Also activate when planning module structure, deciding where to put new code, or doing code review.
repo: mindfold-ai/Trellis
热度
- GitHub 星标
- 15k
- 排名
- #11023
- 许可证
- AGPL-3.0
- 更新时间
- 2026/9/29
评论
- …
同一仓库的更多技能
first-principles-thinking
15k帮你的智能体把计划或设计拆解为基本事实,再由此向上推理来检验它,而不是依赖习惯或惯例。
trellis-before-dev
15k帮你的智能体在开始编写代码之前,阅读项目的编码指南和任务说明,使其遵循团队的惯例。
trellis-brainstorm
15k帮你的智能体每次提出一个有价值的问题、检查代码并就狭窄的范围达成一致,从而厘清新功能或不明确的任务。
trellis-break-loop
15k帮你的智能体在错误修复之后,分析其根本原因类型以及先前修复失败的原因,并保存经验,防止再次发生。