python-patterns
This skill should be used for Python idioms, type hints, async/await, pytest, Django, Flask, FastAPI, Python web frameworks, pandas, data processing
When & Why to Use This Skill
This Claude skill provides a comprehensive suite of idiomatic Python patterns and best practices optimized for Python 3.10+. It enables the generation of high-quality, maintainable code by leveraging modern features such as structural type hints, dataclasses, and asynchronous programming (async/await). Additionally, it offers specialized guidance for industry-standard frameworks including Django, FastAPI, Flask, and pandas, ensuring that your Python backend and data processing scripts follow professional architectural standards.
Use Cases
- Modernizing Legacy Code: Refactoring older Python scripts to utilize modern 3.10+ features like type hints and dataclasses for improved type safety and readability.
- Web Backend Development: Implementing robust and scalable API endpoints using idiomatic patterns for FastAPI, Flask, or Django, including proper error handling and context management.
- Automated Testing: Designing comprehensive test suites with pytest, utilizing fixtures and advanced assertions to ensure high code coverage and reliability.
- Asynchronous Programming: Optimizing I/O-bound applications by implementing efficient async/await patterns and concurrency best practices.
- Data Pipeline Optimization: Writing clean and efficient data processing scripts using pandas and Pythonic idioms for better performance and maintainability.
| name | python-patterns |
|---|---|
| description | This skill should be used for Python idioms, type hints, async/await, pytest, Django, Flask, FastAPI, Python web frameworks, pandas, data processing |
| whenToUse | Python code, type hints, async, pytest, .py files, Django, Flask, FastAPI, Python web, Python backend, pandas, data scripts |
| whenNotToUse | Non-Python code, Jupyter-specific (use nbformat docs) |
| - skill | database-patterns |
| when | SQLAlchemy models |
Python Patterns
Idiomatic Python patterns for Python 3.10+.
Type Hints
def process(data: list[str]) -> dict[str, int]:
return {item: len(item) for item in data}
Dataclasses
from dataclasses import dataclass
@dataclass
class User:
name: str
email: str
active: bool = True
Context Managers
with open("file.txt") as f:
content = f.read()
Pytest
import pytest
def test_add():
assert add(2, 3) == 5
@pytest.fixture
def user():
return User(name="test")
Async/Await
async def fetch_data(url: str) -> dict:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
Error Handling
try:
result = risky_operation()
except ValueError as e:
logger.error(f"Invalid value: {e}")
raise