Clean Architectures in Python
Minimum price
Suggested price

Clean Architectures in Python

A practical approach to better software design

About the Book

The clean architecture is the opposite of spaghetti code, where everything is interlaced and there are no single elements that can be easily detached from the rest and replaced without the whole system collapsing. The main point of the clean architecture is to make clear "what is where and why", and this should be your first concern while you design and implement a software system, whatever architecture or development methodology you want to follow.

This book is divided into two parts.

The first part is about Test-driven Development (TDD), a programming technique that will help you more reliable and easily modifiable software. I will first guide you through a very simple example in chapter 1, demonstrating how to use TDD to approach a project, and how to properly create tests from requirements. In chapter 2 I will then discuss unit testing from a more theoretical point of view, categorising functions and their tests. Chapter 3 will introduce mocks, a powerful tool that helps to test complex scenarios.

The second part introduces the clean architecture. The first chapter discusses briefly the components and the ideas behind this software structure, while chapter 2 runs through a concrete example of clean architecture for a very simple web service. Chapter 3 discusses error management and improvements to the Python code developed in the previous chapter. Finally, chapter 4 shows how to plug different database systems to the web service created previously.

About the Author

Leonardo Giordani
Leonardo Giordani

My name is Leonardo Giordani, I was born in 1977 with Star Wars, bash, Apple ][, BSD, Dire Straits, The Silmarillion. I'm interested in operating systems and computer languages, photography, fantasy and science fiction, video and board games, guitar playing, and (too) many other things.

I studied and used several programming languages, from the Z80 and x86 Assembly to Python and Scala. I love mathematics and cryptography. I'm mainly interested in open source software, and I like both the theoretical and practical aspects of computer science.

For 13 years I was a C/Python programmer and devops for a satellite imagery company. and I am currently infrastructure engineer at WeGotPOP, a UK company based in London and New York that creates innovative software for film productions.

In 2013 I started publishing some technical thoughts on my blog, The Digital Cat. In 2018 I published my first book with Leanpub, “Clean Architectures in Python”, which has been already downloaded more than 11,000 times.

Table of Contents

    • Introduction
      • What is a software architecture?
      • Why is it called “clean” architecture?
      • Why Python?
      • Acknowledgments
    • About the book
      • A brief history of this book
      • How this book is structured
      • Typographic conventions
      • Why this book comes for free
      • Submitting issues or patches
      • About the author
    • Setup a Python Project
      • Virtual environments
      • Python projects with Cookiecutter
  • Part 1 - Tools
    • Chapter 1 - Introduction to TDD
      • Introduction
      • A real-life example
      • A simple TDD project
      • Setup the project
      • Requirements
      • Step 1 - Adding two numbers
      • Step 2 - Adding three numbers
      • Step 3 - Adding multiple numbers
      • Step 4 - Subtraction
      • Step 5 - Multiplication
      • Step 6 - Refactoring
      • Step 7 - Division
      • Step 8 - Testing exceptions
      • Step 9 - A more complex set of requirements
      • Recap of the TDD rules
      • How many assertions?
      • How to manage bugs or missing features
    • Chapter 2 - On unit testing
      • Introduction
      • Tests should be fast
      • Tests should be idempotent
      • Tests should be isolated
      • External systems
      • Focus on messages
      • The testing grid
      • Conclusions
    • Chapter 3 - Mocks
      • Basic concepts
      • First steps
      • Simple return values
      • Complex return values
      • Asserting calls
      • A simple example
      • Patching
      • The patching decorator
      • Multiple patches
      • Checking call parameters
      • Patching immutable objects
      • Mocks and proper TDD
      • A warning
      • Recap
  • Part 2 - The clean architecture
    • Chapter 1 - Components of a clean architecture
      • Layers and data flow
      • Main layers
      • APIs and shades of grey
    • Chapter 2 - A basic example
      • Project overview
      • Project setup
      • Domain models
      • Serializers
      • Use cases
      • The storage system
      • A command-line interface
      • HTTP API
      • Conclusions
    • Chapter 3 - Error management
      • Introduction
      • Basic requests and responses
      • Requests and responses in a use case
      • Request validation
      • Responses and failures
      • Error management in a use case
      • Integrating external systems
      • The HTTP server
      • The repository
      • Conclusions
    • Chapter 4 - Database repositories
      • Introduction
      • A repository based on PostgreSQL
      • A repository based on MongoDB
      • Conclusions
  • Part 3 - Appendices
    • Changelog
  • Notes

Authors have earned$9,537,060writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

Learn more about writing on Leanpub

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses! Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks. Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. It really is that easy.

Learn more about writing on Leanpub