piglet
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesepiglet
piglet
Concise guidance for writing and reviewing Python code using patterns from One Python Craftsman.
基于《Python工匠》中的模式,提供编写和评审Python代码的简洁指导。
Purpose and Triggers
用途与触发场景
- Use for Python (.py) code creation, refactoring, or code review.
- Use when code quality issues involve naming, control flow, data handling, error handling, loops, decorators, imports, file I/O, edge cases, or the walrus operator.
- Prefer clarity and maintainability. Optimize performance only when it is a real constraint.
- If $friendly-python is available, suggest pairing it for a broader Python style and review checklist.
- 适用于Python(.py)代码的创建、重构或代码评审。
- 当代码质量问题涉及命名、控制流、数据处理、错误处理、循环、装饰器、导入、文件I/O、边界情况或Walrus Operator(海象运算符)时使用。
- 优先考虑代码的清晰度和可维护性。仅当性能确实是约束条件时才进行性能优化。
- 如果$friendly-python可用,建议搭配使用它以获得更全面的Python风格与评审检查清单。
Decision Order
决策优先级
- Correctness and explicit behavior
- Readability and maintainability
- Extension cost and change isolation
- Performance and micro-optimizations
- 正确性与明确行为
- 可读性与可维护性
- 扩展成本与变更隔离
- 性能与微优化
Workflow
工作流程
- Identify the topic in the table below.
- Apply the guidance and compare against Bad and Good examples.
- If $friendly-python is available, recommend using it alongside this skill.
- Re-check for new edge cases and readability regressions.
- 识别下表中的对应主题。
- 应用指导内容,并与反面示例和正面示例进行对比。
- 如果$friendly-python可用,建议将其与本技能搭配使用。
- 重新检查是否存在新的边界情况以及可读性退化问题。
Topics
主题
| Topic | Guidance | Reference |
|---|---|---|
| Variables and Naming | Descriptive names, avoid implicit magic, keep variable scope close | references/variables-and-naming.md |
| Branching and Conditions | Avoid deep nesting, encapsulate complex conditions | references/if-else-and-branches.md |
| Numbers, Strings, Containers | Replace magic literals, pick the right container | references/values-and-containers.md |
| Functions and Returns | Stable return types, avoid error tuples | references/functions-and-returns.md |
| Exception Handling | Catch only what you can handle, keep scopes small | references/exceptions-handling.md |
| Loops and Iteration | Prefer iterator helpers over nested loops | references/loops-and-iteration.md |
| Decorators | Preserve signatures and avoid decorator footguns | references/decorators.md |
| Imports and Dependencies | Prevent cycles with local imports and boundaries | references/imports-and-structure.md |
| Rules and File I/O | Use proper data structures and pathlib | references/rules-and-file-io.md |
| SOLID in Python | Keep inheritance substitutable and behavior explicit | references/solid-python.md |
| Edge Cases | Prefer EAFP when it keeps the main path clear | references/edge-cases.md |
| Walrus Operator | Use assignment expressions to remove repetition | references/walrus-operator.md |
| 主题 | 指导原则 | 参考文档 |
|---|---|---|
| 变量与命名 | 使用描述性名称,避免隐式魔法,缩小变量作用域 | references/variables-and-naming.md |
| 分支与条件 | 避免深层嵌套,封装复杂条件 | references/if-else-and-branches.md |
| 数字、字符串与容器 | 替换魔法字面量,选择合适的容器类型 | references/values-and-containers.md |
| 函数与返回值 | 保持返回类型稳定,避免使用错误元组 | references/functions-and-returns.md |
| 异常处理 | 仅捕获能够处理的异常,缩小作用域 | references/exceptions-handling.md |
| 循环与迭代 | 优先使用迭代器辅助工具而非嵌套循环 | references/loops-and-iteration.md |
| 装饰器 | 保留函数签名,避免装饰器陷阱 | references/decorators.md |
| 导入与依赖 | 通过局部导入和边界控制避免循环依赖 | references/imports-and-structure.md |
| 规则与文件I/O | 使用合适的数据结构和pathlib | references/rules-and-file-io.md |
| Python中的SOLID原则 | 保持继承的可替换性与行为的明确性 | references/solid-python.md |
| 边界情况 | 当能让主路径更清晰时,优先使用EAFP(先尝试后处理)原则 | references/edge-cases.md |
| Walrus Operator | 使用赋值表达式消除重复代码 | references/walrus-operator.md |
References
参考资料
- Each reference file lists source URLs in its frontmatter .
urls
- 每个参考文档的前置元数据中列出了来源URL。
urls