python-patterns

Decision guidance for building Python software, covering framework choice, asynchronous versus synchronous code, type annotations, and project structure. Python is a programming language used for scripts, web services, and data applications.

In plain words
What is it for?
Use it when planning Python APIs, full web applications, simple scripts, AI services, or background jobs, and when deciding how to handle I/O, computation, and existing dependencies.
Why use it?
It helps choose an approach based on the application's needs instead of applying the same framework or concurrency style everywhere.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/misonl/ling/python-patterns
Any agent
npx skills add MisonL/Ling --skill python-patterns
Clone the repo
git clone --depth 1 https://github.com/MisonL/Ling

Made for: Claude Code, Codex.

Per session 43 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,860 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00043 $0.02860
Opus 5 $0.00022 $0.01430
Sonnet 5 $0.00009 $0.00572
Haiku 4.5 $0.00004 $0.00286

Measured yesterday against content hash 8de8dd5e7263, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

python-patterns scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.agents/skills/python-patterns/SKILL.md · 442 lines

How it starts

The opening of the file, as written. The whole thing — 442 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Python 模式

面向 2025 的 Python 开发原则与决策方法。
学习如何思考,不要只记模式。


[WARN] 本技能使用方式

本技能强调决策原则,不是固定代码模板。

  • 需求不明确时,先询问用户框架偏好
  • 根据 Context(上下文)决定 async(异步)或 sync(同步)
  • 不要每次都默认同一框架

1. 框架选型(2025)

决策树

你要构建什么?
|
+-- API 优先(API-first)/ 微服务
|   +-- FastAPI(async、现代、速度快)
|
+-- 全栈 Web / CMS / 管理后台
|   +-- Django(开箱即用,batteries-included)
|
+-- 简单应用 / 脚本 / 学习
|   +-- Flask(极简、灵活)
|
+-- AI/ML API 服务化
|   +-- FastAPI(Pydantic、async、Uvicorn)
|
+-- 后台任务
    +-- Celery + 任意 Web 框架

对比原则

维度 FastAPI Django Flask
适用场景 API、微服务 全栈、CMS 简单项目、学习
异步支持 原生支持 Django 5.0+ 依赖扩展
管理后台 手动构建 内置 Admin(管理后台) 依赖扩展
ORM(对象关系映射) 自由选择 Django ORM 自由选择
学习曲线

选型前必须询问:

  1. 是纯 API 还是全栈应用?
  2. 是否需要后台管理界面?
  3. 团队是否熟悉 async?
  4. 是否有现有基础设施约束?

2. 异步与同步决策

何时使用异步(async)

async def(异步函数)更适合:
+-- I/O 密集操作(数据库、HTTP、文件)
+-- 大量并发连接
+-- 实时交互场景
+-- 微服务通信
+-- FastAPI/Starlette/Django ASGI(异步服务器网关接口)

def(同步函数)更适合:
+-- CPU 密集操作
+-- 简单脚本
+-- 遗留代码库
+-- 团队不熟悉 async
+-- 依赖阻塞型库(无 async 版本)

黄金法则

I/O bound(I/O 密集)-> async(等待外部资源)
CPU bound(CPU 密集)-> sync + multiprocessing(本地计算)

不要:
+-- 随意混用 sync 与 async
+-- 在 async 代码里调用阻塞库
+-- 为 CPU 任务强行上 async

异步库选型

需求 异步库
HTTP 客户端 httpx
PostgreSQL asyncpg
Redis(内存数据库) aioredis / redis-py async
文件 I/O aiofiles
数据库 ORM SQLAlchemy 2.0 async, Tortoise

3. 类型标注策略

哪些地方要标注

必须标注:
+-- 函数参数
+-- 返回类型
+-- 类属性
+-- 对外公开 API

可省略:
+-- 局部变量(让推断工作)
+-- 一次性脚本
+-- 测试代码(通常可选)

常见类型模式

# 下面是模式示例,请理解其语义:

# Optional(可选)-> 可能为 None
from typing import Optional
def find_user(id: int) -> Optional[User]: ...

# Union(联合类型)-> 多类型之一
def process(data: str | dict) -> None: ...

# 泛型集合
def get_items() -> list[Item]: ...
def get_mapping() -> dict[str, int]: ...

# Callable(可调用)
from typing import Callable
def apply(fn: Callable[[int], str]) -> str: ...

Read the full file on GitHub · 442 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 442 lines · 43 tokens per session scan A 8de8dd5e7263

Subscribe to this mod's changes

python-patterns is a skill published in the GitHub repository MisonL/Ling (9 stars, last pushed 5mo ago), licensed MIT. It adds 43 tokens to every session and 2,860 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

systematic-debugging

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes.

obra/superpowers · 21 tokens

next-cache-components-adoption

Turn on Cache Components in a Next.js app and resolve the blocking routes it surfaces. Use when the user wants to enable, adopt, or migrate to Cache Components, flip the cacheComponents flag, work through a flood of blocking-prerender / instant validation errors, run the cache-components-instant-false codemod, or…

vercel/next.js · 95 tokens

babysit-pr

Babysit a GitHub pull request after creation by continuously polling review comments, CI checks/workflow runs, and mergeability state until the PR is merged/closed or user help is required. Diagnose failures, retry likely flaky failures up to 3 times, auto-fix/push branch-related issues when appropriate, and keep…

openai/codex · 114 tokens

imagegen

Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output…

openai/codex · 113 tokens

cpu-profile-analysis

Analyze V8/Chrome CPU profiles (.cpuprofile) and DevTools trace files (Trace-.json). Use when: profiling performance, investigating slow functions, comparing code paths, finding bottlenecks, analyzing timeToRequest, understanding call trees from sampling profiler data, analyzing layout/paint/rendering, investigating…

microsoft/vscode · 71 tokens

next-cache-components-optimizer

Drive a Next.js route to instant navigation by setting up an agentic loop, under Cache Components / PPR, on initial load (hard navigation) and client-side navigation (soft navigation). Encode the goal as a failing @next/playwright instant() e2e and work it to green, one verified route at a time; the shipped test then…

vercel/next.js · 170 tokens