Skip to content

Python

The Python skill provides expertise in modern Python development including type hints, async patterns, dataclasses, and Pythonic idioms.

  • Working with Python files (.py)
  • Writing Python scripts or applications
  • Using Python frameworks (Django, FastAPI, Flask)
  • Data processing and automation
from typing import Optional, List, Dict, Union
from collections.abc import Callable
def process_items(
items: List[str],
callback: Callable[[str], None],
config: Optional[Dict[str, Any]] = None
) -> List[str]:
"""Process items with optional callback."""
return [callback(item) for item in items]
import asyncio
from typing import List
async def fetch_data(url: str) -> dict:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async def fetch_all(urls: List[str]) -> List[dict]:
return await asyncio.gather(*[fetch_data(url) for url in urls])
from contextlib import contextmanager
@contextmanager
def managed_resource():
resource = acquire_resource()
try:
yield resource
finally:
release_resource(resource)
# Usage
with managed_resource() as r:
r.do_something()
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class User:
id: int
email: str
name: str
created_at: datetime = field(default_factory=datetime.now)
def __post_init__(self):
self.email = self.email.lower()
from pydantic import BaseModel, EmailStr, Field
class UserCreate(BaseModel):
email: EmailStr
name: str = Field(min_length=1, max_length=100)
password: str = Field(min_length=8)
class Config:
str_strip_whitespace = True
  1. Use type hints for all public functions
  2. Use dataclasses or Pydantic for data models
  3. Prefer context managers for resource management
  4. Use async for I/O-bound operations
  5. Follow PEP 8 style guidelines
# ❌ BAD: Mutable default
def add_item(item, items=[]):
items.append(item)
return items
# ✅ GOOD: Use None and initialize
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
# ❌ BAD: Manual resource management
file = open('data.txt')
data = file.read()
file.close()
# ✅ GOOD: Use with statement
with open('data.txt') as file:
data = file.read()
# ❌ BAD: Blocking call in async function
async def process():
result = expensive_cpu_work() # Blocks event loop
return result
# ✅ GOOD: Run in thread pool
async def process():
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, expensive_cpu_work)
return result
# ❌ BAD: Catching everything
try:
risky_operation()
except:
handle_error()
# ✅ GOOD: Specific exceptions
try:
risky_operation()
except (ValueError, TypeError) as e:
handle_error(e)

See FastAPI skill for API development patterns.

See Django skill for web application patterns.

See pytest skill for testing patterns.