Skip to content

PacktPublishing/Clean-Architecture-with-Python

Repository files navigation

Clean Architecture with Python, First Edition

This is the code repository for Clean Architecture with Python, First Edition, published by Packt.

Implement scalable and maintainable applications using proven architectural principles

Sam Keen

Free PDF       Graphic Bundle       Amazon      

About the book

Clean Architecture with Python

In the rapidly evolving tech industry, software applications struggle to keep pace with changing business needs, leaving developers grappling with complex codebases that resist change, ultimately reducing productivity and increasing technical debt. Clean Architecture with Python offers a powerful approach to address these challenges. Drawing from his extensive experience architecting cloud-native systems, Sam Keen helps you transform complex architectural challenges into digestible, implementable solutions. This book teaches essential principles for effective development, emphasizing the Pythonic implementation of Clean Architecture. Through practical examples, you'll learn how to create modular, loosely coupled systems that are easy to understand, modify, and extend. The book covers key concepts such as the Dependency Rule, separation of concerns, and domain modeling, all tailored for Python development. By the end of this book, you'll be able to apply Clean Architecture principles effectively in your Python projects. Whether you're building new systems or managing existing ones, you'll have the skills to create more maintainable and adaptable applications. This approach will enhance your ability to respond to changing requirements, setting you up for long-term success in your development career.

Key Learnings

  • Apply Clean Architecture principles idiomatically in Python
  • Implement domain-driven design to isolate core business logic
  • Apply SOLID principles in a Pythonic context to improve code quality
  • Structure projects for maintainability and ease of modification
  • Develop testing techniques for cleanly architected Python applications
  • Refactor legacy Python code to adhere to Clean Architecture principles
  • Design scalable APIs and web applications using Clean Architecture

Chapters

Clean Architecture with Python
  1. Clean Architecture Essentials: Transforming Python Development
  2. SOLID Foundations: Building Robust Python Applications
  3. Type-Enhanced Python: Strengthening Clean Architecture
  4. Domain-Driven Design: Crafting the Core Business Logic
  5. The Application Layer: Orchestrating Use Cases
  6. The Interface Adapters Layer: Controllers and Presenters
  7. The Frameworks and Drivers Layer: External Interfaces
  8. Implementing Test Patterns with Clean Architecture
  9. Adding Web UI: Clean Architecture's Interface Flexibility
  10. Implementing Clean Architecture Observability: Monitoring and Verification
  11. Legacy to Clean: Refactoring Python for Maintainability
  12. Your Clean Architecture Journey: Next Steps

Project Structure

The code is organized by chapter. Each `Chapter_X` folder (e.g., `Chapter_1`, `Chapter_2`) represents a progressive snapshot of the application's state at the end of that corresponding chapter in the book.

An example of the structure of a chapter folder is shown below:

  • README.md: Contains instructions for running the application or tests for that chapter.
  • chapter_code_excerpts: Contains the code snippets from the chapter in the order of appearance in the files with numeric indexes (ex: 00_error_class.py). This is provided for reference and is not meant to be runnable.
  • TodoApp: If applicable, contains the companion application code for that chapter. This code is runnable (to the extent of its implementation for the given chapter).
Chapter_4/
├── README.md
│
├── chapter_code_excerpts
│   ├── 00_create_new_task.py
│   ├── 01_create_task_business_rules.py
│   ├── 02_value_objects_in_clean_arch.py
│   ├── 03_project_usage.py
│   ├── ...
│
└── TodoApp
    └── todo_app
        ├── application
        ├── domain
        ├── infrastructure
        └── interfaces

All code has been tested and verified to work with Python 3.13 on MacOS and Windows. With the nature of Python the code should work on other platforms that support a Python runtime, but this has not been verified.

1. Installing dependencies

Dependency management

To simplify setup, this repository uses a single pyproject.toml file located at the root of the repository. This file defines the dependencies for the entire project, effectively installing the union of all packages required across all chapters.

We use UV for managing dependencies. However, a requirements.txt file is also provided for users who prefer to use pip on its own.

Installing dependencies with UV

If you have uv installed, you can use it to create the environment:

# Create the virtual environment
> uv venv

# Activate the environment
# On macOS/Linux:
> source .venv/bin/activate
# On Windows:
> .venv\Scripts\activate

Once your virtual environment is activated, install the required packages using one of the following methods:

# Sync dependencies using uv and pyproject.toml
> uv sync

Installing dependencies with pip

Creating a virtual environment:

# Create the virtual environment
> python -m venv .venv

# Activate the environment
# On macOS/Linux:
> source .venv/bin/activate
# On Windows:
> .venv\Scripts\activate

run pip install:

# Ensure pip is up-to-date
> python -m pip install --upgrade pip

# Install dependencies using pip
> pip install -r requirements.txt

2. Explore and Run Chapter Code

Navigate to the specific chapter folder you're interested in. Each chapter has its own `README.md` file with instructions for running the application or tests for that chapter.

Example: Running tests for Chapter 5

# Navigate to the chapter's application folder
> cd Chapter_5/TodoApp

# Run tests (example command, may vary by chapter)
> pytest

Get to know Author

Sam Keen is a software engineering leader with over 25 years of experience. A polyglot developer who's leveraged Python in varied contexts from small startups to industry giants including AWS, Lululemon, and Nike. His expertise spans cloud architecture, continuous delivery, and building scalable systems. At Lululemon, Sam pioneered the company's first cloud-native software development team, setting standards for distributed cloud architecture within the company. Currently, Sam leverages Python to design and implement internal platform engineering solutions for AWS focusing on clean architecture principles and maintainable code. Sam resides in the US Pacific Northwest with his wife and two cats.

Other Related Books

About

Clean Architecture with Python, Published by Packt

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •