piglet

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

piglet

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

决策优先级

  1. Correctness and explicit behavior
  2. Readability and maintainability
  3. Extension cost and change isolation
  4. Performance and micro-optimizations
  1. 正确性与明确行为
  2. 可读性与可维护性
  3. 扩展成本与变更隔离
  4. 性能与微优化

Workflow

工作流程

  1. Identify the topic in the table below.
  2. Apply the guidance and compare against Bad and Good examples.
  3. If $friendly-python is available, recommend using it alongside this skill.
  4. Re-check for new edge cases and readability regressions.
  1. 识别下表中的对应主题。
  2. 应用指导内容,并与反面示例和正面示例进行对比。
  3. 如果$friendly-python可用,建议将其与本技能搭配使用。
  4. 重新检查是否存在新的边界情况以及可读性退化问题。

Topics

主题

TopicGuidanceReference
Variables and NamingDescriptive names, avoid implicit magic, keep variable scope closereferences/variables-and-naming.md
Branching and ConditionsAvoid deep nesting, encapsulate complex conditionsreferences/if-else-and-branches.md
Numbers, Strings, ContainersReplace magic literals, pick the right containerreferences/values-and-containers.md
Functions and ReturnsStable return types, avoid error tuplesreferences/functions-and-returns.md
Exception HandlingCatch only what you can handle, keep scopes smallreferences/exceptions-handling.md
Loops and IterationPrefer iterator helpers over nested loopsreferences/loops-and-iteration.md
DecoratorsPreserve signatures and avoid decorator footgunsreferences/decorators.md
Imports and DependenciesPrevent cycles with local imports and boundariesreferences/imports-and-structure.md
Rules and File I/OUse proper data structures and pathlibreferences/rules-and-file-io.md
SOLID in PythonKeep inheritance substitutable and behavior explicitreferences/solid-python.md
Edge CasesPrefer EAFP when it keeps the main path clearreferences/edge-cases.md
Walrus OperatorUse assignment expressions to remove repetitionreferences/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使用合适的数据结构和pathlibreferences/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
    .
  • 每个参考文档的前置元数据
    urls
    中列出了来源URL。