Cover of Architecture Patterns with Python by Percival and Gregory, featuring a symbolic architectural or structural design on a clean background

Pages

304

Published

2020

Python ✨ New

Architecture Patterns with Python

Enabling Test-Driven Development, Domain-Driven Design, and the Clean Architecture in Python

Learn to build large-scale Python applications using proven architectural patterns that keep your codebase maintainable as complexity grows.

Most Python tutorials stop at getting code to work. Architecture Patterns with Python picks up where they leave off, showing you how to structure applications that stay manageable as they grow. Harry Percival and Bob Gregory walk through test-driven development, domain-driven design, and clean architecture using concrete Python examples, giving you patterns used by experienced engineers to tame real-world complexity.

About this book

Writing Python that works is one skill. Writing Python that a team can maintain, extend, and test confidently over years is another. Most developers hit a wall when their codebase grows beyond a handful of modules and the quick fixes stop working. This book is the practical guide to breaking through that wall.

Harry Percival and Bob Gregory draw on hard-won production experience to show you how professional engineers structure complex Python applications. Rather than presenting abstract theory, they build a real e-commerce system step by step, introducing each pattern at the moment you actually need it. You see the problem before you see the solution, which means the patterns stick.

The book covers three interlocking ideas. Test-driven development gives you a harness that makes refactoring safe. Domain-driven design keeps your business logic clean and independent of frameworks and databases. The clean architecture — built around ports, adapters, and dependency inversion — lets you swap infrastructure without rewriting your core. Together, these make large codebases feel manageable rather than frightening.

Along the way you will work through:

  • The Repository, Unit of Work, and Aggregate patterns from domain-driven design
  • Command and Query separation and the message bus pattern
  • Event-driven architecture and how to use events to decouple services
  • Dependency injection in Python without a heavy framework
  • Trade-offs you will actually face: when a pattern earns its complexity and when it does not

The authors are honest about costs. Every pattern adds indirection, and the book helps you decide when that indirection pays off. There are no silver bullets here, just clear reasoning and code you can read and run.

If you are comfortable with Python but want to write software that behaves like it was designed rather than assembled, this book gives you the vocabulary, the patterns, and the practice to do it.

🎯 What you'll learn

  • Apply the Repository pattern to decouple your domain model from database implementation details
  • Use the Unit of Work pattern to manage transactions and keep your service layer clean
  • Model business rules with Aggregates that enforce invariants without leaking infrastructure concerns
  • Build a message bus that routes commands and events through your application in a consistent, testable way
  • Implement event-driven communication between services without tight coupling
  • Structure your application with ports and adapters so infrastructure is replaceable
  • Practice test-driven development at the architectural level, writing tests that survive large refactors
  • Evaluate the real trade-offs of each pattern and decide when the added complexity is justified

👤 Who is this book for?

  • Intermediate Python developers who can write working code but want to learn how to structure it for long-term maintainability
  • Backend engineers moving from small scripts or single-file apps to multi-team, production-grade Python services
  • Developers familiar with Django or Flask who want to understand how to separate business logic from framework concerns
  • Engineers who have heard terms like DDD, hexagonal architecture, or CQRS and want a Python-centric explanation with real code
  • Technical leads responsible for a Python codebase that is becoming harder to change safely

Table of contents

  1. 01

    Domain Modelling

    You write your first domain model in plain Python, completely free of frameworks and databases, and learn why keeping business logic isolated makes it easier to test and reason about.

  2. 02

    The Repository Pattern

    You wrap your data access behind an abstract repository interface, so your domain model never needs to know whether data lives in Postgres, a file, or a fake in-memory store.

  3. 03

    A Brief Interlude: Coupling and Abstractions

    You examine what coupling actually means in code and see how introducing the right abstractions reduces the cost of change without adding unnecessary indirection.

  4. 04

    The Service Layer and Use Cases

    You extract a service layer that orchestrates domain logic and handles use cases, keeping your domain model free of application concerns and your tests fast.

  5. 05

    TDD in High Gear and Low Gear

    You learn when to test at the unit level and when to test through the service layer, developing a practical instinct for writing tests that survive refactoring.

  6. 06

    Unit of Work Pattern

    You implement the Unit of Work pattern to manage database transactions in one place, giving your service layer a clean way to commit or roll back a set of operations atomically.

  7. 07

    Aggregates and Consistency Boundaries

    You model business invariants using Aggregates, learning how to define consistency boundaries that protect your data integrity without locking too much of your database.

  8. 08

    Events and the Message Bus

    You introduce domain events and a message bus that routes them to handlers, decoupling the side effects of business operations from the operations themselves.

  9. 09

    Commands and Command Handler Patterns

    You separate commands from events to clarify intent in your message bus, and refactor your application so every operation is initiated by an explicit, named command.

  10. 10

    Dependency Injection and Bootstrapping

    You wire your application together using dependency injection without a heavy framework, and build a bootstrap script that assembles all the components for production and for tests.

Frequently asked questions

What Python experience do I need before reading this book?

You should be comfortable writing Python — functions, classes, list comprehensions, and basic testing. The book does not teach Python syntax and moves quickly through code. Prior experience with a web framework like Flask or Django is helpful but not required.

Do I need prior knowledge of DDD or clean architecture?

No. The book introduces every concept from scratch in a Python context. Familiarity with the terms helps you move faster, but the authors explain each idea before applying it.

Is the code in the book available to run locally?

The book was published by O'Reilly and the authors have made the accompanying code available on GitHub. Check the book's preface or the O'Reilly product page for the exact repository link.

Is this book still relevant given it was published in 2020?

Yes. The architectural patterns covered — domain modelling, repositories, unit of work, message bus — are language-agnostic ideas that do not expire. The Python code targets Python 3 and the concepts apply to any modern Python project.

Does the book cover specific frameworks like Django or SQLAlchemy?

SQLAlchemy is used throughout as the persistence layer to illustrate the Repository and Unit of Work patterns. Django is not the focus, but the architectural lessons apply directly to Django projects.

Who is this book not for?

If you are still learning Python fundamentals or are building a small personal project that will never grow, the patterns here will feel like overkill. The book earns its complexity when you are working on a codebase that multiple people maintain over time.

You might also like

📬 Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.