python-expert
Guidance on writing clean, maintainable, and idiomatic Python code.
Install
mkdir -p .claude/skills/python-expert-deepanshu0504 && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/15824" && unzip -o skill.zip -d .claude/skills/python-expert-deepanshu0504 && rm skill.zipInstalls to .claude/skills/python-expert-deepanshu0504
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Python expert for writing clean, well-structured, and idiomatic code. Use for: code organization, PEP 8 compliance, type hints, design patterns, best practices, refactoring, testing, and modern Python features. Covers async/await, decorators, context managers, and pythonic idioms.Key capabilities
- →Organize Python modules and classes
- →Apply type hints to functions and complex types
- →Implement Python idioms like list comprehensions and context managers
- →Handle errors with specific exception handling
- →Use modern Python features like pattern matching and the walrus operator
- →Write testable code with dependency injection
How it works
The skill provides guidance on code structure, type hinting, Python idioms, error handling, and modern features to improve code quality.
Inputs & outputs
When to use python-expert
- →Refactor python code
- →Add type hints
- →Write clean modules
- →Implement design patterns
About this skill
Python Expert
Expert guidance for writing clean, maintainable, and idiomatic Python code following best practices and modern Python standards.
When to Use
- Writing new Python functions, classes, or modules
- Refactoring existing code for better structure
- Applying design patterns and best practices
- Adding type hints and documentation
- Improving code readability and maintainability
- Implementing error handling and logging
- Writing tests and test fixtures
- Using async/await and concurrent programming
- Optimizing Python code performance
Core Principles
1. Code Structure and Organization
Module Organization:
"""
Module docstring describing purpose and usage.
This module handles user authentication and session management.
"""
# Standard library imports
import os
import sys
from datetime import datetime
from typing import Optional, List, Dict
# Third-party imports
import requests
from sqlalchemy import create_engine
# Local application imports
from .models import User
from .exceptions import AuthenticationError
# Constants
MAX_LOGIN_ATTEMPTS = 3
SESSION_TIMEOUT = 3600
# Module-level code
logger = logging.getLogger(__name__)
Class Structure:
class UserManager:
"""Manage user operations with clean, single-responsibility methods.
Attributes:
db_connection: Database connection instance
cache: Optional cache for user data
"""
def __init__(self, db_connection, cache=None):
"""Initialize the UserManager.
Args:
db_connection: Active database connection
cache: Optional cache instance for performance
"""
self.db_connection = db_connection
self.cache = cache
self._session = None
def get_user(self, user_id: int) -> Optional[User]:
"""Retrieve user by ID with caching support."""
if self.cache:
cached_user = self.cache.get(f"user:{user_id}")
if cached_user:
return cached_user
user = self.db_connection.query(User).filter_by(id=user_id).first()
if user and self.cache:
self.cache.set(f"user:{user_id}", user, timeout=300)
return user
def __repr__(self):
return f"UserManager(cache={'enabled' if self.cache else 'disabled'})"
2. Type Hints and Annotations
Use type hints for better code clarity and IDE support:
from typing import List, Dict, Optional, Union, Tuple, Callable, TypeVar, Generic
# Function signatures
def process_data(
items: List[str],
threshold: int = 10,
callback: Optional[Callable[[str], None]] = None
) -> Dict[str, int]:
"""Process items and return frequency count."""
result: Dict[str, int] = {}
for item in items:
result[item] = result.get(item, 0) + 1
if callback:
callback(item)
return result
# Complex types
UserData = Dict[str, Union[str, int, List[str]]]
def get_user_data(user_id: int) -> Optional[UserData]:
"""Fetch user data with structured typing."""
pass
# Generic types
T = TypeVar('T')
class Repository(Generic[T]):
"""Generic repository pattern."""
def find_by_id(self, id: int) -> Optional[T]:
"""Find entity by ID."""
pass
def save(self, entity: T) -> T:
"""Save entity to repository."""
pass
3. Python Idioms and Best Practices
List Comprehensions:
# Good - clear and efficient
squares = [x**2 for x in range(10)]
even_squares = [x**2 for x in range(10) if x % 2 == 0]
# Dictionary comprehensions
word_lengths = {word: len(word) for word in words}
# Set comprehensions
unique_lengths = {len(word) for word in words}
Context Managers:
# File handling
with open('data.txt', 'r') as f:
data = f.read()
# Custom context manager
from contextlib import contextmanager
@contextmanager
def database_transaction(connection):
"""Ensure transaction is committed or rolled back."""
try:
yield connection
connection.commit()
except Exception:
connection.rollback()
raise
# Usage
with database_transaction(db_conn) as conn:
conn.execute("INSERT INTO users VALUES (...)")
Generators for Memory Efficiency:
def read_large_file(file_path: str):
"""Read large file line by line without loading into memory."""
with open(file_path, 'r') as f:
for line in f:
yield line.strip()
def fibonacci(n: int):
"""Generate Fibonacci sequence up to n terms."""
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
# Usage
for num in fibonacci(10):
print(num)
Unpacking and Multiple Assignment:
# Tuple unpacking
first, *middle, last = [1, 2, 3, 4, 5]
# Dictionary unpacking
defaults = {'timeout': 30, 'retries': 3}
config = {'timeout': 60, 'endpoint': '/api'}
final_config = {**defaults, **config}
# Function arguments
def process(name, age, city='Unknown'):
pass
user_data = {'name': 'Alice', 'age': 30, 'city': 'NYC'}
process(**user_data)
4. Error Handling
Specific Exception Handling:
def safe_divide(a: float, b: float) -> Optional[float]:
"""Safely divide two numbers with proper error handling."""
try:
result = a / b
except ZeroDivisionError:
logger.error(f"Cannot divide {a} by zero")
return None
except TypeError as e:
logger.error(f"Invalid types for division: {e}")
raise
else:
return result
finally:
logger.debug("Division operation completed")
# Custom exceptions
class ValidationError(Exception):
"""Raised when data validation fails."""
pass
class DataNotFoundError(Exception):
"""Raised when requested data doesn't exist."""
def __init__(self, entity_type: str, entity_id: int):
self.entity_type = entity_type
self.entity_id = entity_id
super().__init__(f"{entity_type} with ID {entity_id} not found")
# Usage
def get_user(user_id: int) -> User:
user = db.query(User).get(user_id)
if not user:
raise DataNotFoundError("User", user_id)
return user
5. Decorators and Higher-Order Functions
Common Decorator Patterns:
from functools import wraps
import time
def timer(func):
"""Measure function execution time."""
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
end = time.perf_counter()
logger.info(f"{func.__name__} took {end - start:.4f}s")
return result
return wrapper
def retry(max_attempts: int = 3, delay: float = 1.0):
"""Retry function on failure."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
except Exception as e:
if attempt == max_attempts - 1:
raise
logger.warning(f"Attempt {attempt + 1} failed: {e}")
time.sleep(delay)
return wrapper
return decorator
def validate_types(**type_hints):
"""Validate function argument types."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Bind arguments to parameter names
bound = inspect.signature(func).bind(*args, **kwargs)
bound.apply_defaults()
for param_name, expected_type in type_hints.items():
if param_name in bound.arguments:
value = bound.arguments[param_name]
if not isinstance(value, expected_type):
raise TypeError(
f"{param_name} must be {expected_type.__name__}"
)
return func(*args, **kwargs)
return wrapper
return decorator
# Usage
@timer
@retry(max_attempts=3, delay=2.0)
def fetch_data(url: str):
response = requests.get(url)
response.raise_for_status()
return response.json()
6. Data Classes and Named Tuples
Modern Data Structures:
from dataclasses import dataclass, field
from typing import List
from datetime import datetime
@dataclass
class User:
"""User data structure with automatic methods."""
id: int
username: str
email: str
created_at: datetime = field(default_factory=datetime.now)
tags: List[str] = field(default_factory=list)
def __post_init__(self):
"""Validate data after initialization."""
if '@' not in self.email:
raise ValueError(f"Invalid email: {self.email}")
# Frozen dataclass (immutable)
@dataclass(frozen=True)
class Point:
x: float
y: float
def distance_from_origin(self) -> float:
return (self.x**2 + self.y**2) ** 0.5
# Named tuples for simple structures
from collections import namedtuple
Coordinate = namedtuple('Coordinate', ['latitude', 'longitude'])
location = Coordinate(40.7128, -74.0060)
7. Async/Await for Concurrent Operations
Asynchronous Programming:
import asyncio
from typing import List
async def fetch_url(session, url: str) -> str:
"""Fetch single URL asynchronously."""
async with session.get(url) as response:
return await response.text()
async def fetch_all_urls(urls: List[str]) -> List[str]:
"""Fetch multiple URLs concurrently."""
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
return await asyncio.gather(*tasks)
async def process_with_timeout(coro, timeout: float):
"""Execute coroutine with timeout."""
try:
return await asyncio.wait_for(coro, timeout=timeout)
except asyncio.TimeoutError:
logger.error(f"Operatio
---
*Content truncated.*
When not to use it
- →When using mutable default arguments in function definitions
- →When using bare except clauses
- →When performing string concatenation in loops
Limitations
- →The skill does not support mutable default arguments
- →The skill does not support bare except clauses
- →The skill does not support string concatenation in loops
How it compares
This skill provides structured guidance and examples for writing Python code according to best practices, contrasting with ad-hoc coding that may lack consistency or modern features.
Compared to similar skills
python-expert side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| python-expert (this skill) | 0 | 4mo | Review | Intermediate |
| jupyter-notebook | 30 | 6mo | Review | Intermediate |
| unsloth | 15 | 8mo | No flags | Intermediate |
| clojure-write | 16 | 3mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
jupyter-notebook
davila7
Use when the user asks to create, scaffold, or edit Jupyter notebooks (`.ipynb`) for experiments, explorations, or tutorials; prefer the bundled templates and run the helper script `new_notebook.py` to generate a clean starting notebook.
unsloth
zechenzhangAGI
Expert guidance for fast fine-tuning with Unsloth - 2-5x faster training, 50-80% less memory, LoRA/QLoRA optimization
clojure-write
metabase
Guide Clojure and ClojureScript development using REPL-driven workflow, coding conventions, and best practices. Use when writing, developing, or refactoring Clojure/ClojureScript code.
add-uint-support
pytorch
Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.
python-pro
sickn33
Master Python 3.12+ with modern features, async programming, performance optimization, and production-ready practices. Expert in the latest Python ecosystem including uv, ruff, pydantic, and FastAPI. Use PROACTIVELY for Python development, optimization, or advanced Python patterns.
copilot-sdk
github
Build agentic applications with GitHub Copilot SDK. Use when embedding AI agents in apps, creating custom tools, implementing streaming responses, managing sessions, connecting to MCP servers, or creating custom agents. Triggers on Copilot SDK, GitHub SDK, agentic app, embed Copilot, programmable agent, MCP server, custom agent.