Purely
Purely is a small, dependency-free library for the parts of everyday Python
that tend to fill up with if x is None, try/except and isinstance
checks. It needs Python 3.12 or later.
pip install purely
This guide follows one small project: the backend of an online bookshop. Each chapter starts with a job the shop needs done, writes it first in plain Python, looks at what goes wrong as it grows, and then fixes it with one or two ideas from purely. Later chapters reuse what the earlier ones introduced, and the last one puts everything together into a checkout.
- A customer's city. Showing a value that may be missing, with
Option. - Reading the shop's settings. Walking nested data with
safe, and insisting on what is required withensure. - Validating a signup. Reporting why something failed, with
OkandErr. - Importing a batch of orders. Running a list through several steps and
stopping cleanly on the first broken row, with
Chain. - Pricing rules. Composing small configurable steps, with
curryandpipe. - Sending notifications. Choosing what to do by the kind of event, with
dispatcher. - Testing the checkout. Swapping a real payment gateway for a fake one,
with
Registryanddepends. - The checkout, end to end. All of the above in one program, with nothing new.
Every code block in this guide runs against the installed package when the
guide is built. The output under a block is what that run printed. Each
chapter's finished program is also saved in the repository under
docs/examples/.