AgentSkillsCN

python-development

涵盖热评级模型(CIGRE TB 601、IEEE 738)、导线温度限值、悬链线物理(弧垂、张力、净空距离)、电气特性(电阻、电抗),以及标准合规性(CIGRE、IEEE、IEC)。在实施热模型、计算导线物理,或当用户询问“CIGRE 方程有哪些?”或“如何验证这一计算结果?”时,可加以应用。

SKILL.md
--- frontmatter
name: python-development
description: >-
  Covers Python 3.12+ development with uv project setup, FastAPI web services,
  Pydantic validation, async/await patterns, type hints with mypy, pytest testing, SQLAlchemy
  database operations, Polars data processing, httpx API clients, and Docker deployment.
  Use when building Python APIs, writing async code, or when the user asks "how do I validate
  data?" or "what's the best way to structure a Python project?"

Python Development

Comprehensive guide for modern Python 3.12+ development with FastAPI ecosystem.

When to Use This Skill

  • Starting or configuring Python projects
  • Building REST APIs with FastAPI
  • Defining data models with Pydantic
  • Implementing async/await patterns
  • Adding type hints and mypy configuration
  • Writing tests with pytest
  • Working with databases (SQLAlchemy 2.0)
  • Building data pipelines with Polars
  • Integrating external APIs
  • Deploying to production with Docker

Stack Overview

LayerDefaultAlternatives
RuntimePython 3.12+-
Package Manageruvrye, poetry
Linter/Formatterruffblack + flake8
Type Checkermypypyright
Web FrameworkFastAPIFlask, Django
ValidationPydantic v2-
ORMSQLAlchemy 2.0-
Data ProcessingPolarsPandas
HTTP Clienthttpxaiohttp
Testingpytest-
ContainerizationDocker-

Core Philosophy

Follows foundations principles. Python-specific emphasis:

  • Async by default — non-blocking I/O for web services
  • Strict typing — catch errors at development time with mypy
  • Pydantic everywhere — validate at trust boundaries
  • 12-factor methodology — environment-based configuration

Quick Reference

Project Setup with uv

bash
uv init my-project --python 3.12
uv add fastapi uvicorn pydantic-settings
uv add --dev pytest ruff mypy
uv run pytest

pyproject.toml Essentials

toml
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.12"

[tool.ruff]
target-version = "py312"
select = ["E", "F", "I", "N", "W", "UP"]

[tool.mypy]
python_version = "3.12"
strict = true

FastAPI Endpoint

python
from fastapi import FastAPI, Depends, HTTPException, status
from pydantic import BaseModel, EmailStr, Field

app = FastAPI()

class UserCreate(BaseModel):
    email: EmailStr
    username: str = Field(..., min_length=3, max_length=50)

class UserResponse(BaseModel):
    id: int
    email: EmailStr
    username: str
    model_config = {"from_attributes": True}

@app.post("/users", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
async def create_user(user: UserCreate, db: AsyncSession = Depends(get_db)):
    db_user = User(**user.model_dump())
    db.add(db_user)
    await db.commit()
    await db.refresh(db_user)
    return db_user

Pydantic Model with Validation

python
from pydantic import BaseModel, Field, field_validator

class UserRegistration(BaseModel):
    email: EmailStr
    password: str = Field(..., min_length=8)

    @field_validator("password")
    @classmethod
    def password_strength(cls, v: str) -> str:
        if not any(c.isupper() for c in v):
            raise ValueError("Must contain uppercase")
        return v

Async Database Query

python
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

async def get_user(session: AsyncSession, user_id: int) -> User | None:
    result = await session.execute(select(User).where(User.id == user_id))
    return result.scalar_one_or_none()

pytest Test

python
@pytest.mark.asyncio
async def test_create_user(client: AsyncClient):
    response = await client.post("/users", json={"email": "test@example.com", "username": "test"})
    assert response.status_code == 201
    assert response.json()["email"] == "test@example.com"

Topics

TopicUse For
CoreProject setup, pyproject.toml, modern Python features
FastAPIREST APIs, routing, dependency injection, middleware
PydanticData models, validation, settings management
Asyncasync/await, TaskGroup, context managers
TypesType hints, mypy, Protocol, generics
Testingpytest, fixtures, mocking, async tests
DatabaseSQLAlchemy 2.0, Alembic migrations, transactions
DataPolars, ETL pipelines, schema validation
API Clientshttpx, retries, rate limiting, error handling
DeploymentDocker, logging, OpenTelemetry, health checks
Debuggingpdb, structlog, pytest debugging, remote debugging

Critical Rules

Always

  • Use async def for I/O-bound operations
  • Use Pydantic models for external input validation
  • Use pathlib.Path for file operations
  • Run mypy in CI/CD pipeline

Never

  • Block the event loop with sync I/O in async code
  • Use mutable default arguments (def foo(items=[]))
  • Skip validation on external input
  • Hardcode configuration values (use pydantic-settings)