Code Explainers

Python code explainers

python
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.mail import EmailMultiAlternatives
from django.db.models.signals import post_save

Sending a welcome email with Django signals

signals email user-activation
Intermediate 8 steps
python
import csv
import io
from datetime import date
 

Streaming a CSV export in FastAPI

streaming async-generators csv
Advanced 8 steps
python
import json
import logging
 
import stripe

Handling Stripe webhooks in Django

webhooks signature-verification idempotency
Intermediate 7 steps
python
from collections import deque
 
 
class RollingAverage:

A rolling average over a fixed window

sliding-window running-sum deque
Intermediate 7 steps
python
from collections import OrderedDict
from typing import Callable, Hashable, Iterable, Iterator, TypeVar
 
T = TypeVar("T")

Two ways to dedupe while keeping order

deduplication generators ordered-data
Intermediate 7 steps
python
from django.db.models import Sum, Count, F, DecimalField
from django.db.models.functions import TruncMonth, Coalesce
 
from .models import Order

Build a monthly revenue report in Django

aggregation orm group-by
Advanced 7 steps
python
from pathlib import Path
 
 
def print_tree(root, prefix="", show_hidden=False):

Printing a directory tree with recursion

recursion filesystem sorting
Intermediate 6 steps
python
from dataclasses import dataclass, fields, MISSING
from typing import get_type_hints, get_origin, get_args, Union
 
 

Type-checking dataclasses at runtime

dataclasses type-hints runtime-validation
Advanced 8 steps
python
import time
import threading
 
 

How a thread-safe token bucket rate limiter works

rate-limiting concurrency locking
Intermediate 6 steps
python
from flask import Blueprint, render_template, redirect, url_for, flash
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SubmitField

Handling a post form with a Flask Blueprint

blueprints form-validation post-redirect-get
Intermediate 7 steps
python
import random
import time
import logging
from functools import wraps

A retry decorator with exponential backoff

decorators retry exponential-backoff
Intermediate 6 steps
python
from django.db import models
from django.utils import timezone
 
 

How soft deletes work in Django

soft-delete querysets managers
Intermediate 10 steps
python
from functools import wraps
 
 
def memoize(func):

Building a memoize decorator in Python

decorators closures memoization
Intermediate 5 steps
python
import os
import tempfile
from pathlib import Path
from typing import Union

How atomic file writes work in Python

atomicity filesystem durability
Advanced 7 steps
python
import re
from datetime import datetime
from dataclasses import dataclass
 

Parsing access logs with named regex groups

regex named-groups dataclasses
Intermediate 6 steps
python
import gzip
from pathlib import Path
from typing import Iterator
 

Streaming TSV records with Python generators

generators lazy-evaluation file-io
Intermediate 5 steps
python
import hashlib
import hmac
import os
 

Verifying signed payment webhooks in Flask

hmac webhooks signature verification
Intermediate 7 steps
python
from flask import Blueprint, render_template, request, abort
from sqlalchemy import select
 
from .models import Article

Paginating articles in a Flask Blueprint

pagination blueprints input-validation
Intermediate 6 steps