mirror of
https://github.com/duthaho/claudekit.git
synced 2026-06-10 20:24:57 +03:00
3.9 KiB
3.9 KiB
title, description
| title | description |
|---|---|
| Python | Modern Python development with type hints, async, and best practices |
The Python skill provides expertise in modern Python development including type hints, async patterns, dataclasses, and Pythonic idioms.
When Activated
- Working with Python files (
.py) - Writing Python scripts or applications
- Using Python frameworks (Django, FastAPI, Flask)
- Data processing and automation
Core Patterns
Type Hints
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]
Async/Await
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])
Context Managers
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()
Dataclasses
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()
Pydantic Models
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
Best Practices
- Use type hints for all public functions
- Use dataclasses or Pydantic for data models
- Prefer context managers for resource management
- Use async for I/O-bound operations
- Follow PEP 8 style guidelines
Common Pitfalls
Mutable Default Arguments
# ❌ 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
Not Closing Resources
# ❌ 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()
Blocking in Async
# ❌ 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
Catching Bare Exceptions
# ❌ BAD: Catching everything
try:
risky_operation()
except:
handle_error()
# ✅ GOOD: Specific exceptions
try:
risky_operation()
except (ValueError, TypeError) as e:
handle_error(e)
Integration with Frameworks
With FastAPI
See FastAPI skill for API development patterns.
With Django
See Django skill for web application patterns.
With pytest
See pytest skill for testing patterns.
Related Skills
- FastAPI - REST API development
- Django - Web applications
- pytest - Testing framework
- PostgreSQL - Database integration