python-patterns

Applies Pythonic design patterns including protocols, dataclasses, decorators, and async/await to Python code.

Updated Sep 8, 2026
One-click install
npx skills add https://github.com/salomepoulain/makery-stations --skill python-patterns-salomepoulain
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-patterns
Source: https://github.com/salomepoulain/makery-stations/tree/main/stations/claude/workbench/pantry/skills/python-patterns
Command: npx skills add https://github.com/salomepoulain/makery-stations --skill python-patterns-salomepoulain

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing idiomatic, maintainable Python requires knowing which language features fit which problems, and developers often default to verbose or non-Pythonic solutions when structuring classes, handling resources, or managing concurrency. ## Core Features & Use Cases - Structural Typing & DTOs: Use Protocol for duck typing with type safety and dataclasses for immutable value objects and request models. - Resource & Concurrency Patterns: Implement context managers, generators, decorators, and async/await with asyncio.gather for I/O-bound work. - Project Structure & Error Handling: Organize packages with domain/services/infrastructure layers, define custom exception hierarchies, and apply advanced type hints with generics and ParamSpec. - Use Case: When refactoring a Python service, apply constructor-based dependency injection with a Protocol-typed repository, frozen dataclass entities, and an async context manager for database sessions. ## Quick Start Review my Python module and refactor it to use Pythonic patterns such as protocols, dataclasses, and async context managers.

Frequently Asked Questions about python-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use Protocol for duck typing in Python?

Define a class inheriting from typing.Protocol with the required method signatures, then annotate function parameters with that protocol. Any class implementing those methods satisfies the type checker without explicit inheritance, enabling flexible, loosely coupled code.

When should I use dataclasses instead of regular classes?

Use dataclasses for data transfer objects and value objects that mainly store fields. They auto-generate __init__, __repr__, and __eq__, support frozen=True for immutability, and field(default_factory=...) for mutable defaults like lists.

How do I run multiple async functions concurrently in Python?

Build a list of coroutines and pass them to asyncio.gather, which executes them concurrently and returns results in order. Entry points use asyncio.run to start the event loop for the top-level async function.

What is the difference between a generator and a list comprehension?

Generator expressions use parentheses and evaluate lazily, producing items one at a time without storing the full sequence in memory. List comprehensions eagerly build the entire list, which is wasteful for large or pipelined data processing.

How do I create a custom exception hierarchy in Python?

Define a base exception class for your domain, then subclass it for specific errors like UserNotFoundError or ValidationError. Store contextual attributes such as user_id or field in __init__ and pass a formatted message to super().

Does Python support structural pattern matching with type hints?

Python 3.10+ supports match statements that combine with union type hints like str | int | None. You can match on types using case str(), case int(), or case None to branch logic declaratively.