Skip to content

Repository files navigation

python-snippets

A mono repo to contain my personal snippets for Python, with most my endeavor to port my C++ snippets to Python.

Algorithms

A Python implementation of the QuickSort algorithm, ported from C++. Features the Lomuto partition scheme, depth control for recursion safety, and tail recursion optimization. Includes comprehensive testing for various edge cases and a detailed explanation of the algorithm's history and mechanics.

Setup and Usage

cd algorithms/quick-sort
uv venv
. .venv/bin/activate  # On Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"

# Run the main example
python -m quicksort

# Run examples using launcher scripts
./run_quicksort.py
./run_examples.py

# Run tests
pytest

# Run linting
ruff check .

# Run type checking
mypy src/quicksort

Design Patterns

Creational Patterns

A Python implementation of the Abstract Factory design pattern, ported from C++. Provides an interface for creating families of related objects without specifying their concrete classes. This implementation demonstrates UI component theming with light and dark variants, ensuring consistent visual presentation across an application.

Setup and Usage
cd design-patterns/creational/abstract-factory
uv venv
. .venv/bin/activate  # On Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"

# Run the main example
python -m abstract_factory

# Run examples using launcher scripts
./run_abstract_factory.py
./run_example.py

# Run tests
pytest

# Run linting
ruff check .

# Run type checking
mypy src

Structural Patterns

A Python implementation of the Adapter design pattern, ported from C++. Allows objects with incompatible interfaces to collaborate by wrapping one object to provide a compatible interface to another. Includes file system adapters that standardize operations across different file systems (APFS, FAT32) and a power adapter example.

A Python implementation of the Proxy design pattern, ported from C++. Provides a surrogate or placeholder for another object to control access to it. This implementation demonstrates a protection proxy with authentication and logging capabilities, commonly used in enterprise systems where access control and audit trails are crucial.

Setup and Usage (Adapter)
cd design-patterns/structural/adapter
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m adapter_pattern

# Run specific examples
python -m adapter_pattern.examples.file_system_example
python -m adapter_pattern.examples.basic_adapter_example

# Or use the launcher scripts
./run_adapter_demo.py
./run_file_system_example.py
./run_basic_example.py

# Run tests
pytest

# Run linting
ruff check --fix .
Setup and Usage (Proxy)
cd design-patterns/structural/proxy
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m proxy_pattern

# Run specific examples using launcher scripts
./run_proxy_demo.py
./run_vector_example.py
./run_file_example.py

# Run tests
pytest

# Run linting
ruff check --fix .

Behavioral Patterns

A Python implementation of the Command design pattern, ported from C++. Turns requests into stand-alone objects that contain all information about the request. Includes document editing system and smart home automation examples with undo/redo functionality.

Setup and Usage
cd design-patterns/behavioral/command/command_pattern
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m command_pattern

# Run specific examples
python -m command_pattern.examples.document_example
python -m command_pattern.examples.smart_home_example

# Run tests
pytest

# Run linting
ruff check .

A Python implementation of the Chain of Responsibility design pattern, ported from C++. Uses modern Python features like type hints, dataclasses, and abstract base classes. Includes an expense approval system example and a document workflow example.

Setup and Usage
cd design-patterns/behavioral/chain-of-responsibility
uv venv
. .venv/bin/activate
uv pip install -e .

# Run the main example
python -m src.chain_of_responsibility

# Run the document approval example
python examples/custom_chain_example.py

# Run tests
pytest

A Python implementation of the Fail-Fast pattern, ported from C++. Uses modern Python with type hints and proper error handling. Demonstrates immediate error detection through state validation and early exception raising. Includes a banking system implementation with comprehensive validation.

Setup and Usage
cd design-patterns/behavioral/fail-fast
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m fail_fast

# Run the banking example
python examples/banking_example.py

# Run tests
pytest

# Run type checking
mypy src

# Run linting
ruff check src tests examples

A Python implementation of the Interpreter pattern, ported from C++. Defines a grammar for a language and provides an interpreter to deal with this grammar. Includes a mathematical expression evaluator and a business rule engine example.

Setup and Usage
cd design-patterns/behavioral/interpreter
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m interpreter_pattern

# Run the calculator example
python examples/calculator_example.py

# Run the rule engine example
python examples/rule_engine_example.py

# Run tests
pytest

# Run type checking
mypy src

# Run linting
ruff check src tests examples

A Python implementation of the Visitor pattern, ported from C++. Allows adding new operations to existing object structures without modifying them. Includes geometric shape processing with multiple visitors for calculating area, perimeter, and generating descriptions.

Setup and Usage
cd design-patterns/behavioral/vistor
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m visitor_pattern

# Run examples using launcher scripts
./run_visitor.py
./run_example.py

# Run tests
pytest

# Run linting
ruff check --fix .

A Python implementation of the Strategy pattern, ported from C++. Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Includes payment processing examples with multiple payment strategies like credit card, PayPal, and cryptocurrency.

Setup and Usage
cd design-patterns/behavioral/strategy
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m strategy_pattern

# Run the interactive payment example
python examples/payment_example.py

# Or use the launcher script
./run_strategy_demo.py

# Run tests
pytest

# Run linting
ruff check .

Concurrency Patterns

A Python implementation of the Pipeline concurrency pattern, ported from C++. Implements a thread-safe pipeline where each processing stage runs in its own thread. Data flows through stages sequentially, enabling parallel processing of different items at different stages. Includes generic stage definitions, thread-safe queues, and comprehensive examples.

Setup and Usage

cd concurrency/pipelining
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m pipeline

# Run specific examples using launcher scripts
./run_pipeline.py
./run_data_example.py
./run_custom_example.py

# Run tests
pytest

# Run type checking
mypy src

# Run linting
ruff check --fix .

A Python implementation of the Readers-Writers concurrency pattern, ported from C++. Provides a thread-safe way for multiple readers and writers to access a shared resource, with writer preference to prevent writer starvation. Includes thread-safe logging and RAII-style context management.

Setup and Usage

cd concurrency/reader-writer
uv venv
. .venv/bin/activate
uv pip install -e .

# Run the main example
python -m reader_writer

# Run specific examples
python run_basic_example.py
python run_advanced_example.py

# Run tests
pytest

# Run type checking
mypy src

# Run linting
ruff check --fix .

A Python implementation of the Producer-Consumer concurrency pattern, ported from C++. Provides a thread-safe queue with multiple producers and consumers, with condition variables for efficient thread coordination. Includes generic type support, bounded queue with backpressure, and comprehensive thread management.

Setup and Usage

cd concurrency/producer-consumer
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m producer_consumer

# Run specific examples using launcher scripts
./run_producer_consumer.py
./run_custom_example.py

# Run tests
pytest

# Run type checking
mypy src

# Run linting
ruff check --fix .

A Python implementation of the Thread Pool concurrency pattern, ported from C++. Manages a collection of worker threads that can execute tasks asynchronously, providing an efficient way to parallelize work across multiple threads. Includes Future-based result handling, exception propagation, and clean shutdown mechanism.

Setup and Usage

cd concurrency/thread-pool
uv venv
. .venv/bin/activate
uv pip install -e .

# Run the main example
python -m thread_pool

# Run specific examples using launcher scripts
./run_thread_pool.py
./examples/basic_usage.py
./examples/advanced_usage.py

# Run tests
pytest

# Run linting
ruff check .

A Python implementation of the Barrier concurrency pattern, ported from C++. Provides a synchronization mechanism to ensure multiple threads wait for each other to reach a specific point before proceeding further. Includes both a custom implementation using locks and condition variables, and a modern implementation using Python's threading.Barrier.

Setup and Usage

cd concurrency/barrier-example
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m barrier_example

# Run specific examples using launcher scripts
./barrier_example_run.py
./examples/custom_example.py
./examples/modern_example.py

# Run tests
pytest

# Run type checking
mypy .

# Run linting
ruff check .
ruff format .

Programming Paradigms

A Python implementation of the Negative Space programming paradigm, ported from C++. Demonstrates designing software by focusing on what cannot happen rather than what can. Includes a SafeString class with constraint-based validation and a generic NegativeSpaceContainer that uses functional constraints.

Setup and Usage

cd programming-paradigms/negative-space
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m negative_space

# Run the advanced example
python -m negative_space.examples.advanced_example

# Or use the launcher scripts
./run_negative_space.py
./run_advanced_example.py

# Run tests
pytest

# Run linting
ruff check --fix .

# Run type checking
mypy .

Data Structures

A Python implementation of a binary search tree that works with any comparable type. Includes in-order, pre-order, and post-order traversals.

Setup and Usage

cd data-structures/binary-tree
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run tests
pytest

# Run linting
ruff check .

Utilities

A Python implementation of a high-resolution timer, ported from C++. Provides accurate timing measurements with nanosecond precision using time.perf_counter_ns. Supports multiple time unit outputs and automatic formatting.

Setup and Usage

cd utilities/timer/timer_py
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m timer_py

# Run the example scripts
./run_example.py
./run_advanced_example.py

# Run the timer demo
./run_timer.py

# Run tests
pytest

# Run linting
ruff check .

Odds and Ends

A Python example demonstrating common pitfalls related to object copying, inheritance, and serialization. While Python doesn't have the same slicing issues as C++, it has similar problems that can lead to unexpected behavior. Includes demonstrations of improper inheritance, shallow vs. deep copying, and serialization type issues.

Setup and Usage

cd odds-and-ends/slicing
uv venv
. .venv/bin/activate
uv pip install -e ".[dev]"

# Run the main example
python -m slicing

# Run specific examples using launcher scripts
./run_slicing.py
./run_advanced_example.py

# Run tests
pytest

# Run type checking
mypy src

# Run linting
ruff check .

About

A mono repo to contain my efforts to port my C++ Snippets to Python.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages