Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/lessons/14-advanced-python/01-closures-and-decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Learn how closures remember values, write your own decorators with

# Closures and Decorators

![Illustration: Closures and Decorators](/images/topics-webp/closures-and-decorators.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- What a **closure** is, and when an inner function needs `nonlocal`
- How to write a **decorator** step by step, with `*args`, `**kwargs`, and `functools.wraps`
Expand Down Expand Up @@ -166,6 +168,8 @@ By hand, this is `fetch_rate = retry(times=3)(fetch_rate)`. `times` lives in the

You can put several decorators on one function. They are **applied bottom-up** (the one nearest the `def` wraps first), so at call time the **top one runs outermost**:

![Diagram: Stacked Decorators](/images/companions-webp/decorator-stack.webp){.lesson-diagram width=1536 height=1024 loading=lazy}

```python
from functools import wraps

Expand Down
2 changes: 2 additions & 0 deletions docs/lessons/14-advanced-python/02-python-data-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Look under the hood of Python objects: types and __dict__, attribu

# The Python Data Model

![Illustration: The Python Data Model](/images/topics-webp/python-data-model.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- Why **everything is an object**: `type()`, `__class__`, and `__dict__`
- How `__getattr__`, `__getattribute__`, `__setattr__`, and `__delattr__` hook into attribute access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Write class-based context managers with __enter__ and __exit__, co

# Context Managers in Depth

![Illustration: Context Managers in Depth](/images/topics-webp/context-managers-in-depth.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- The exact **context manager protocol**: what `__enter__` returns and what `__exit__(exc_type, exc, tb)` receives
- How the return value of `__exit__` decides whether an exception is **swallowed** or **propagated**
Expand Down Expand Up @@ -63,6 +65,8 @@ If `__exit__` returns a **true** value, the exception is **swallowed**. `None` o

This `Transaction` works on a copy of the accounts, commits on success, rolls back on error, and swallows only `ValueError`:

![Diagram: Context Manager Lifecycle](/images/companions-webp/context-manager-lifecycle.webp){.lesson-diagram width=1536 height=1024 loading=lazy}

```python
class Transaction:
def __init__(self, accounts):
Expand Down
4 changes: 4 additions & 0 deletions docs/lessons/14-advanced-python/04-async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Write asynchronous Python with async def and await: run coroutines

# Async and await

![Illustration: Async and await](/images/topics-webp/async-await.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- What a **coroutine** is, and what `await` does while a program waits
- How to run coroutines **concurrently** with `asyncio.gather`, `create_task` and `TaskGroup`
Expand Down Expand Up @@ -103,6 +105,8 @@ The total is the **longest** wait (0.3 s), not the sum (0.6 s), and there is sti

`asyncio.create_task(coro)` wraps a coroutine in a **task** that starts as soon as the current coroutine pauses. `await` the task later to get its result:

![Diagram: Tasks and the Event Loop](/images/companions-webp/async-task-timeline.webp){.lesson-diagram width=1536 height=1024 loading=lazy}

```python
import asyncio

Expand Down
2 changes: 2 additions & 0 deletions docs/lessons/14-advanced-python/05-memory-and-performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Measure before you optimise: time code with timeit, inspect memory

# Memory and Performance

![Illustration: Memory and Performance](/images/topics-webp/memory-and-performance.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- How to **measure** speed with `timeit` and `time.perf_counter`
- What `sys.getsizeof` reports, and how `tracemalloc` shows **where** memory goes
Expand Down
2 changes: 2 additions & 0 deletions docs/lessons/14-advanced-python/06-design-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Use classic design patterns the Python way: strategies as function

# Pythonic Design Patterns

![Illustration: Pythonic Design Patterns](/images/topics-webp/design-patterns.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- Why **design patterns** are shared vocabulary, not code to copy
- How to write **strategy**, **factory**, and **registry** with functions, classmethods, dicts, and decorators
Expand Down
4 changes: 4 additions & 0 deletions docs/lessons/14-advanced-python/07-threads-and-processes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Run work concurrently with threads and processes: I/O-bound versus

# Threads and Processes

![Illustration: Threads and Processes](/images/topics-webp/threads-and-processes.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- **I/O-bound** versus **CPU-bound** work, and what the **GIL** means for each
- How to start and `join` a **thread**, and why shared data needs a **`Lock`**
Expand Down Expand Up @@ -32,6 +34,8 @@ CPython has a **Global Interpreter Lock (GIL)**: only one thread runs Python cod

Python 3.14 also offers an optional **free-threaded build** (`python3.14t`) without a GIL. The normal `python3.14` still has the GIL, and this lesson assumes it.

![Diagram: Shared or Separate Memory?](/images/companions-webp/thread-process-memory.webp){.lesson-diagram width=1536 height=1024 loading=lazy}

## Starting threads

`threading.Thread(target=..., args=...)` creates a thread. `start()` runs it; `join()` waits until it finishes.
Expand Down
4 changes: 4 additions & 0 deletions docs/lessons/14-advanced-python/08-testing-in-depth.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Organise a test suite with conftest.py and fixture scopes, use mon

# Testing in Depth

![Illustration: Testing in Depth](/images/topics-webp/testing-in-depth.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- How to organise tests with a **`tests/` folder**, **`conftest.py`** and `-k`
- **Fixture scopes** and **`yield` fixtures** for setup and teardown
Expand Down Expand Up @@ -44,6 +46,8 @@ shop_project/

A fixture that uses **`yield`** instead of `return` gets a **teardown**: the code after `yield` runs after the test, even a failed one. `tests/conftest.py`:

![Diagram: Fixture Setup and Teardown](/images/companions-webp/fixture-lifecycle.webp){.lesson-diagram width=1536 height=1024 loading=lazy}

```py
import pytest

Expand Down
4 changes: 4 additions & 0 deletions docs/lessons/14-advanced-python/09-advanced-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: "Build a log pipeline that combines generators, frozen dataclasses,

# Project: Log Pipeline

![Illustration: Project: Log Pipeline](/images/topics-webp/advanced-project.webp){.lesson-cover width=1536 height=1024}

::: tip What you'll learn
- How to build a **lazy pipeline** of generators that parses a log line by line
- How to register pipeline steps with a **decorator** and choose them by name
Expand Down Expand Up @@ -36,6 +38,8 @@ Your program reads an access log, skips broken lines, filters and groups the req

A log line looks like `2026-09-16T10:03:21 GET /products/12 200 0.128 192.168.1.20`. The fields are time, method, path, status code, duration in seconds, and client IP. The data flows through small stages:

![Diagram: Log Pipeline Architecture](/images/companions-webp/log-pipeline-architecture.webp){.lesson-diagram width=1536 height=1024 loading=lazy}

```text
access.log
|
Expand Down
Loading
Loading