Learn Pythonic patterns, type hints, and best practices for maintainable code.
Copy the install command and let the AI configure it · recommended for beginners
Please install the "python-patterns" skill from askskill: 1. Download https://raw.githubusercontent.com/affaan-m/ECC/main/skills/python-patterns/SKILL.md 2. Save it as ~/.claude/skills/python-patterns/SKILL.md 3. Reload skills and tell me it's ready
Please refactor the following Python code to be more Pythonic, follow PEP 8, and explain what you improved: [paste code]
Returns cleaner, standards-compliant Python code with a list of improvements.
Please add type hints to the following Python functions, adjust signatures if needed, and point out any potential type risks: [paste code]
Outputs fully type-annotated code and recommendations for better type safety.
Please review this Python module for readability, error handling, performance, naming, and maintainability, then provide an improved version: [paste code]
Provides a structured review and a revised code version aligned with best practices.
Idiomatic Python patterns and best practices for building robust, efficient, and maintainable applications.
Python prioritizes readability. Code should be obvious and easy to understand.
# Good: Clear and readable
def get_active_users(users: list[User]) -> list[User]:
"""Return only active users from the provided list."""
return [user for user in users if user.is_active]
# Bad: Clever but confusing
def get_active_users(u):
return [x for x in u if x.a]
Avoid magic; be clear about what your code does.
# Good: Explicit configuration
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Bad: Hidden side effects
import some_module
some_module.setup() # What does this do?
Python prefers exception handling over checking conditions.
# Good: EAFP style
def get_value(dictionary: dict, key: str, default_value: Any = None) -> Any:
try:
return dictionary[key]
except KeyError:
return default_value
# Bad: LBYL (Look Before You Leap) style
def get_value(dictionary: dict, key: str, default_value: Any = None) -> Any:
if key in dictionary:
return dictionary[key]
else:
return default_value
from typing import Optional, List, Dict, Any
def process_user(
user_id: str,
data: Dict[str, Any],
active: bool = True
) -> Optional[User]:
"""Process a user and return the updated User or None."""
if not active:
return None
return User(user_id, data)
# Python 3.9+ - Use built-in types
def process_items(items: list[str]) -> dict[str, int]:
return {item: len(item) for item in items}
# Python 3.8 and earlier - Use typing module
from typing import List, Dict
def process_items(items: List[str]) -> Dict[str, int]:
return {item: len(item) for item in items}
from typing import TypeVar, Union
# Type alias for complex types
JSON = Union[dict[str, Any], list[Any], str, int, float, bool, None]
def parse_json(data: str) -> JSON:
return json.loads(data)
# Generic types
T = TypeVar('T')
def first(items: list[T]) -> T | None:
"""Return the first item or None if list is empty."""
return items[0] if items else None
from typing import Protocol
class Renderable(Protocol):
def render(self) -> str:
"""Render the object to a string."""
def render_all(items: list[Renderable]) -> str:
"""Render all items that implement the Renderable protocol."""
return "\n".join(item.render() for item in items)
# Good: Catch specific exceptions
def load_config(path: str) -> Config:
try:
with open(path) as f:
return Config.from_json(f.read())
except FileNotFoundError as e:
raise ConfigError(f"Config file not found: {path}") from e
except json.JSONDecodeError as e:
raise ConfigError(f"Invalid JSON in config: {path}") from e
# Bad: Bare except
def load_config(path: str) -> Config:
try:
with open(path) as f:
return Config.from_json(f.read())
except:
return None # Silent failure!
def process_data(data: str) -> Result:
try:
parsed = json.loads(data)
except json.JSONDecodeError as e:
# Chain exceptions to preserve the traceback
raise ValueError(f"Failed to parse data: {data}") from e
…
Apply modern, safe, idiomatic C++ standards for writing, review, and refactoring.
Refine retrieved context iteratively to improve subagent understanding and output quality.
Fetches up-to-date framework docs for setup, APIs, and code examples.
Conduct multi-source web research and produce cited, source-attributed reports.
Design adaptive agent workflows with eval gates and reusable skill extraction.
Speed up complex tasks with parallel execution while preserving correctness.
Learn idiomatic Go patterns and best practices for robust, maintainable applications.
Apply idiomatic Kotlin patterns to build robust, efficient, maintainable applications.
Learn idiomatic Rust patterns, ownership, concurrency, and robust error handling practices.
Get idiomatic C# and .NET guidance for architecture, async, and dependency injection.
Apply modern Perl 5.36+ idioms and best practices for maintainable applications.
Apply PyTorch best practices for robust, efficient, reproducible deep learning pipelines.