mastering-python-skill
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMastering Python Skill
Python技能精通指南
Production-ready Python patterns with runnable code examples.
包含可运行代码示例的生产级Python模式大全。
Contents
目录
- Workflow
- Reference Files
- Sample CLI Tools
- When NOT to Use
- Full Table of Contents
Workflow
工作流程
Phase 1: Setup
第一阶段:环境搭建
-
Verify Python versionbash
python --version # Require 3.10+, prefer 3.12+ -
Create and activate virtual environmentbash
python -m venv .venv && source .venv/bin/activate -
Install dependenciesbash
poetry install # or: pip install -r requirements.txt
-
验证Python版本bash
python --version # 要求3.10+,推荐3.12+ -
创建并激活虚拟环境bash
python -m venv .venv && source .venv/bin/activate -
安装依赖bash
poetry install # 或:pip install -r requirements.txt
Phase 2: Develop
第二阶段:开发实现
-
Reference appropriate patterns:
- Types → type-systems.md
- Async → async-programming.md
- APIs → fastapi-patterns.md
- DB → database-access.md
-
Follow project structure from project-structure.md
-
参考对应模式文档:
- 类型系统 → type-systems.md
- 异步编程 → async-programming.md
- Web接口 → fastapi-patterns.md
- 数据库 → database-access.md
-
遵循project-structure.md中的项目结构规范
Phase 3: Validate
第三阶段:验证测试
-
Run quality checksbash
ruff check . && ruff format --check . mypy src/ -
Run tests with coveragebash
pytest -v --cov=src --cov-report=term-missing
-
运行代码质量检查bash
ruff check . && ruff format --check . mypy src/ -
运行带覆盖率统计的测试bash
pytest -v --cov=src --cov-report=term-missing
Phase 4: Deploy
第四阶段:部署上线
-
Build and verify packagebash
python -m build && twine check dist/* -
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-
构建并验证包bash
python -m build && twine check dist/* -
根据docker-deployment.md或ci-cd-pipelines.md进行部署
完成前检查清单:
- [ ] 所有测试通过
- [ ] mypy无错误报告
- [ ] ruff检查无问题
- [ ] 测试覆盖率≥80%
- [ ] 依赖无安全警告Reference Files
参考文档
| Category | Files | Key Topics |
|---|---|---|
| Foundations | syntax-essentials, type-systems, project-structure, code-quality | Variables, type hints, generics, src layout, ruff, mypy |
| Patterns | async-programming, error-handling, decorators, context-managers, generators | async/await, exceptions, Result type, with statements, yield |
| Testing | pytest-essentials, mocking-strategies, property-testing | Fixtures, parametrize, unittest.mock, Hypothesis |
| Web APIs | fastapi-patterns, pydantic-validation, database-access | Dependencies, middleware, validators, SQLAlchemy async |
| Packaging | poetry-workflow, pyproject-config, docker-deployment | Lock files, PEP 621, multi-stage builds |
| Production | ci-cd-pipelines, monitoring, security | GitHub 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, generators | async/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, security | GitHub Actions、OpenTelemetry、OWASP、JWT |
查看TOC.md获取详细主题索引。
Sample CLI Tools
示例CLI工具
Runnable examples demonstrating production patterns:
| Tool | Demonstrates | Reference |
|---|---|---|
| async_fetcher.py | Async HTTP, rate limiting, error handling | async-programming.md |
| config_loader.py | Pydantic settings, .env files, validation | pydantic-validation.md |
| db_cli.py | SQLAlchemy async CRUD, repository pattern | database-access.md |
| code_validator.py | Run→check→fix with ruff and mypy | code-quality.md |
bash
undefined展示生产级模式的可运行示例:
| 工具 | 演示内容 | 参考文档 |
|---|---|---|
| async_fetcher.py | 异步HTTP请求、速率限制、错误处理 | async-programming.md |
| config_loader.py | Pydantic配置、.env文件、验证 | pydantic-validation.md |
| db_cli.py | SQLAlchemy异步CRUD、仓库模式 | database-access.md |
| code_validator.py | 使用ruff和mypy进行运行→检查→修复流程 | code-quality.md |
bash
undefinedTest 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+