Skip to content

Latest commit

 

History

History
154 lines (105 loc) · 3.67 KB

File metadata and controls

154 lines (105 loc) · 3.67 KB

Python Design Patterns

This directory contains idiomatic Python implementations of classic software design patterns. The focus is on readability, explicit behavior, and testability, using Python’s dynamic features responsibly.

Each pattern is implemented independently and validated with unit tests using pytest.


Project Structure

python/
├── src/
│   ├── behavioral/                 # Behavioral design patterns
│   ├── creational/                 # Creational design patterns
│   └── structural/                 # Structural design patterns
├── tests/
│   ├── behavioral/                 # Tests for behavioral patterns
│   ├── creational/                 # Tests for creational patterns
│   └── structural/                 # Tests for structural patterns
├── pyproject.toml                  # Tooling configuration (pytest, black, mypy, etc.)
├── requirements.txt                # Python dependencies
├── Makefile                        # Test and tooling automation
└── README.md                       # This file

Each pattern directory contains:

  • One or more implementation modules
  • Corresponding test modules
  • Minimal, explicit dependencies

Design Pattern Categories

Behavioral patterns focus on object interaction and responsibility.


Creational patterns address object creation while keeping usage flexible and explicit.


Structural patterns focus on composing objects into larger structures.


Environment Setup

Python requires an explicit virtual environment.

Create and activate a virtual environment:

python -m venv venv
source venv/bin/activate      # Linux / macOS
venv\Scripts\activate       # Windows

Install dependencies:

make install

All Makefile commands assume an active virtual environment.


Testing

All implementations are validated using pytest.

Run tests:

make test

Verbose output:

pytest -vv

Coverage

Generate a coverage report:

pytest --cov=src --cov-report=xml

The coverage report will be generated as coverage.xml.


Makefile Usage

A Makefile is provided to standardize common tasks:

make install   # Install dependencies
make format    # Format code (black)
make lint      # Static type checking (mypy)
make test      # Run tests with coverage
make clean     # Remove caches and artifacts

Design Principles

  • Pythonic and readable code
  • Explicit is better than implicit
  • Favor composition and clear data flow
  • Minimal abstractions
  • Tests as first-class citizens

This directory is intended as a reference and learning resource for applying classic design patterns in Python while respecting Python’s design philosophy.