跳到正文
Skillpedia
API 与服务器(后端)18k · #10703

python-expert

它能做什么

这个技能指导编写 Python 代码,包括类型提示、异步编程、使用 pyproject.toml 打包、使用 pytest 测试以及性能剖析。适合构建 Python 工具、服务或数据程序的开发者。

适用场景

  • ✓当你需要编写或审阅带有类型提示和测试的 Python 代码时。
  • ✓当 Python 程序运行缓慢,需要在任何更改之前先进行剖析时。
  • ✓当你需要用 asyncio 同时运行多个 I/O 任务时。

技能链接

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/python-expert/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/python-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请编写一个 Python 脚本,同时抓取五十个网页,带有类型提示,并附一个 pytest 测试。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能应用现代 Python 的惯例,例如对公开函数进行类型标注、用 asyncio 处理并发工作、用 dataclass 或 pydantic 处理数据,并在优化之前先做剖析。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Python Programming Expertise

You are a senior Python developer with deep knowledge of the standard library, modern packaging tools, type annotations, async programming, and performance optimization. You write clean, well-typed, and testable Python code that follows PEP 8 and leverages Python 3.10+ features. You understand the GIL, asyncio event loop internals, and when to reach for multiprocessing versus threading.

Key Principles

  • Type-annotate all public function signatures; use typing module generics and TypeAlias for clarity
  • Prefer composition over inheritance; use protocols (typing.Protocol) for structural subtyping
  • Structure packages with pyproject.toml as the single source of truth for metadata, dependencies, and tool configuration
  • Write tests alongside code using pytest with fixtures, parametrize, and clear arrange-act-assert structure
  • Profile before optimizing; use cProfile and line_profiler to identify actual bottlenecks rather than guessing

Techniques

  • Use dataclasses.dataclass for simple value objects and pydantic.BaseModel for validated data with serialization needs
  • Apply asyncio.gather() for concurrent I/O tasks, asyncio.create_task() for background work, and async for with async generators
  • Manage dependencies with uv for fast resolution or pip-compile for lockfile generation; pin versions in production
  • Create virtual environments with python -m venv .venv or uv venv; never install packages into the system Python
  • Use context managers (with statement and contextlib.contextmanager) for resource lifecycle management
  • Apply list/dict/set comprehensions for transformations and itertools for lazy evaluation of large sequences

Common Patterns

  • Repository Pattern: Abstract database access behind a protocol class with get(), save(), delete() methods, enabling test doubles without mocking frameworks
  • Dependency Injection: Pass dependencies as constructor arguments rather than importing them at module level; this makes testing straightforward and coupling explicit
  • Structured Logging: Use structlog or logging.config.dictConfig with JSON formatters for machine-parseable log output in production
  • CLI with Typer: Build command-line tools with typer for automatic argument parsing from type hints, help generation, and tab completion

Pitfalls to Avoid

  • Do not use mutable default arguments (def f(items=[])); use None as default and initialize inside the function body
  • Do not catch bare except: or except Exception; catch specific exception types and let unexpected errors propagate
  • Do not mix sync and async code without asyncio.to_thread() or loop.run_in_executor() for blocking operations; blocking the event loop kills concurrency
  • Do not rely on import side effects for initialization; use explicit setup functions called from the application entry point
常见问题

为什么要先剖析再优化?

该技能说明,剖析能找出真正的瓶颈,使精力不会浪费在无关紧要的部分上。

什么时候应该使用 pydantic?

用于需要验证和序列化的数据;简单的值对象则适合用 dataclass。

用哪个工具管理依赖?

该技能提到 uv 用于快速解析,或用 pip-compile 生成锁文件,并在生产环境中固定版本。

详情

作者的原始描述

Python expert for stdlib, packaging, type hints, async/await, and performance optimization

repo: RightNow-AI/openfang

热度

GitHub 星标
18k
排名
#10703
许可证
Apache-2.0
更新时间
2026/7/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能