Token bucket rate limiter
Allow bursts but cap the sustained request rate with a small thread-safe token bucket.
Scripts, data handling and standard-library recipes.
Loading snippets…
21 snippets
Allow bursts but cap the sustained request rate with a small thread-safe token bucket.
Measure elapsed wall-clock time of a with-block using time.perf_counter.
Turn text into a lowercase, hyphenated, ASCII-only slug using only the standard library.
Yield overlapping windows of fixed size, useful for moving averages and n-grams.
Stream CSV rows as dicts keyed by header with csv.DictReader, handling UTF-8 BOMs and quoted commas.
Convert ISO timestamps to timezone-aware UTC datetimes and back, avoiding naive datetime bugs.
Memoize pure functions with a bounded least-recently-used cache and inspect or clear it.
Collect a list of records into a dictionary of lists keyed by a field or function result.
Walk a path through nested dicts and lists and return a default instead of raising KeyError.
Convert a byte count into KB, MB, GB and so on with a configurable number of decimals.
Flatten arbitrarily nested lists and tuples into a single flat iterator, leaving strings intact.
Turn nested dicts into a single-level dict with dotted keys, handy for config and logging.