More in Python
Chunk an iterable into fixed-size batches
Python
itertools.batched on 3.12 and later; the islice version works everywhere and never materialises the whole input.
Read a large file without loading it into memory
Python
Iterating a file object yields one line at a time, which is the difference between a constant footprint and an OOM.
A dataclass with validation
Python
__post_init__ runs after the generated constructor, which is where cheap invariants belong.
Time a block of code
Python
A context manager that prints how long its body took, without scattering time.perf_counter calls through the function.
Merge dictionaries
Python
The | operator returns a new dict; |= updates in place. Later keys win in both.
Retry with exponential backoff and jitter
Python
Backs off on each failure and spreads retries out, so a recovering service is not hit by every client at once.
Parse arguments without a dependency
Python
argparse covers subcommands, types and help text, which is more than most scripts ever need.
Random picks
Table sizes, largest first
Databases
Where the disk actually went, including indexes and TOAST.
Find slow queries in Postgres
Databases
Ranks statements by total time spent. Needs the pg_stat_statements extension enabled.
Compare secrets in constant time
Security
A plain === leaks how much of the token matched through its timing. This does not.
A GitHub Actions workflow for a Node project
Build & CI
Caches the dependency install, runs on a matrix of versions, and fails the build on a lint error.
A test that fails for the right reason
Testing
Assert on the message, not merely that something threw. A typo raising TypeError would otherwise pass.
Group an array by a key
JavaScript
Object.groupBy is available in current runtimes; the reduce version is the fallback for older ones.
Centre a box, two ways
Web & CSS
Grid is one line. Flex is three and gives you control over the axes separately.
Compose file with a healthcheck and dependency order
Containers
Makes the app wait until Postgres actually answers, not merely until its container has started.