mastering-python-skill

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Mastering Python Skill

Python技能精通指南

Production-ready Python patterns with runnable code examples.
包含可运行代码示例的生产级Python模式大全。

Contents

目录

Workflow

工作流程

Phase 1: Setup

第一阶段:环境搭建

  1. Verify Python version
    bash
    python --version  # Require 3.10+, prefer 3.12+
  2. Create and activate virtual environment
    bash
    python -m venv .venv && source .venv/bin/activate
  3. Install dependencies
    bash
    poetry install  # or: pip install -r requirements.txt
  1. 验证Python版本
    bash
    python --version  # 要求3.10+,推荐3.12+
  2. 创建并激活虚拟环境
    bash
    python -m venv .venv && source .venv/bin/activate
  3. 安装依赖
    bash
    poetry install  # 或:pip install -r requirements.txt

Phase 2: Develop

第二阶段:开发实现

  1. Reference appropriate patterns:
    • Types → type-systems.md
    • Async → async-programming.md
    • APIs → fastapi-patterns.md
    • DB → database-access.md
  2. Follow project structure from project-structure.md
  1. 参考对应模式文档:
    • 类型系统 → type-systems.md
    • 异步编程 → async-programming.md
    • Web接口 → fastapi-patterns.md
    • 数据库 → database-access.md
  2. 遵循project-structure.md中的项目结构规范

Phase 3: Validate

第三阶段:验证测试

  1. Run quality checks
    bash
    ruff check . && ruff format --check .
    mypy src/
  2. Run tests with coverage
    bash
    pytest -v --cov=src --cov-report=term-missing
  1. 运行代码质量检查
    bash
    ruff check . && ruff format --check .
    mypy src/
  2. 运行带覆盖率统计的测试
    bash
    pytest -v --cov=src --cov-report=term-missing

Phase 4: Deploy

第四阶段:部署上线

  1. Build and verify package
    bash
    python -m build && twine check dist/*
  2. Deploy per docker-deployment.md or ci-cd-pipelines.md
Pre-Completion Checklist:
- [ ] All tests pass
- [ ] mypy reports no errors
- [ ] ruff check clean
- [ ] Coverage ≥80%
- [ ] No security warnings in dependencies

  1. 构建并验证包
    bash
    python -m build && twine check dist/*
  2. 根据docker-deployment.mdci-cd-pipelines.md进行部署
完成前检查清单:
- [ ] 所有测试通过
- [ ] mypy无错误报告
- [ ] ruff检查无问题
- [ ] 测试覆盖率≥80%
- [ ] 依赖无安全警告

Reference Files

参考文档

CategoryFilesKey Topics
Foundationssyntax-essentials, type-systems, project-structure, code-qualityVariables, type hints, generics, src layout, ruff, mypy
Patternsasync-programming, error-handling, decorators, context-managers, generatorsasync/await, exceptions, Result type, with statements, yield
Testingpytest-essentials, mocking-strategies, property-testingFixtures, parametrize, unittest.mock, Hypothesis
Web APIsfastapi-patterns, pydantic-validation, database-accessDependencies, middleware, validators, SQLAlchemy async
Packagingpoetry-workflow, pyproject-config, docker-deploymentLock files, PEP 621, multi-stage builds
Productionci-cd-pipelines, monitoring, securityGitHub Actions, OpenTelemetry, OWASP, JWT
See TOC.md for detailed topic lookup.

分类文档核心主题
基础部分syntax-essentials, type-systems, project-structure, code-quality变量、类型提示、泛型、src目录结构、ruff、mypy
设计模式async-programming, error-handling, decorators, context-managers, generatorsasync/await、异常处理、Result类型、with语句、yield
测试部分pytest-essentials, mocking-strategies, property-testing夹具、参数化、unittest.mock、Hypothesis
Web接口fastapi-patterns, pydantic-validation, database-access依赖管理、中间件、验证器、SQLAlchemy异步
打包发布poetry-workflow, pyproject-config, docker-deployment锁文件、PEP 621、多阶段构建
生产环境ci-cd-pipelines, monitoring, securityGitHub Actions、OpenTelemetry、OWASP、JWT
查看TOC.md获取详细主题索引。

Sample CLI Tools

示例CLI工具

Runnable examples demonstrating production patterns:
ToolDemonstratesReference
async_fetcher.pyAsync HTTP, rate limiting, error handlingasync-programming.md
config_loader.pyPydantic settings, .env files, validationpydantic-validation.md
db_cli.pySQLAlchemy async CRUD, repository patterndatabase-access.md
code_validator.pyRun→check→fix with ruff and mypycode-quality.md
bash
undefined
展示生产级模式的可运行示例:
工具演示内容参考文档
async_fetcher.py异步HTTP请求、速率限制、错误处理async-programming.md
config_loader.pyPydantic配置、.env文件、验证pydantic-validation.md
db_cli.pySQLAlchemy异步CRUD、仓库模式database-access.md
code_validator.py使用ruff和mypy进行运行→检查→修复流程code-quality.md
bash
undefined

Test examples

测试示例

python sample-cli/async_fetcher.py https://httpbin.org/get python sample-cli/config_loader.py --show-env python sample-cli/db_cli.py init --sample-data && python sample-cli/db_cli.py list python sample-cli/code_validator.py src/

---
python sample-cli/async_fetcher.py https://httpbin.org/get python sample-cli/config_loader.py --show-env python sample-cli/db_cli.py init --sample-data && python sample-cli/db_cli.py list python sample-cli/code_validator.py src/

---

When NOT to Use

不适用场景

  • Non-Python languages: Use language-specific skills
  • ML/AI model internals: Use PyTorch/TensorFlow skills
  • Cloud infrastructure: Use AWS/GCP skills for infra (this covers code)
  • Legacy Python 2: Focus is Python 3.10+
  • 非Python语言:使用对应语言的专属技能
  • 机器学习/AI模型内部实现:使用PyTorch/TensorFlow相关技能
  • 云基础设施:使用AWS/GCP技能处理基础设施(本技能仅覆盖代码层面)
  • Python 2遗留项目:本技能聚焦Python 3.10+