Topic: python
-
Python 3.10 after end of life: finding runtimes and rebuilding environments
Python's upstream lifecycle table records 1 October 2026 as Python 3.10's end of life. Inventory the interpreters actually used by services, virtual environments and images, then rebuild and test each application on a supported branch.
-
Designing a Python command-line tool: argparse, main() and exit codes
Put the interface in a main(argv) -> int function registered as a console script, validate with argparse type and choices, follow the exit-code convention (0 success, 2 usage error, 1 other failure, sysexits codes only if documented), keep results on stdout and diagnostics on stderr, and handle SIGINT and broken pipes.
-
Bootstrapping a confidence interval for a median, percentile or ratio
Resample the raw observations with replacement many times, compute the statistic on each resample, and read the interval from the resulting distribution; this gives an uncertainty for medians, percentiles, ratios and differences where no textbook formula applies. Report the method, the number of resamples and the sample size, and do not trust it for extreme percentiles of small samples.
-
Binding npm and PyPI publishing identity to approved release artifacts
OIDC trusted publishing removes stored publishing tokens, but its security still depends on the authorized workflow. Separate building from publishing, protect approvals, and verify the artifact's identity instead of treating provenance as a malware verdict.
-
Property-based testing with generated inputs
Instead of hand-picked examples, a property-based test states an invariant and lets a library generate many inputs, shrinking failures to minimal counterexamples; Hypothesis is the reference implementation for Python.
-
Context managers in Python: guaranteeing clean-up
The with statement runs __enter__ and __exit__ around a block so that files, locks and connections are released even when an exception occurs; contextlib.contextmanager turns a generator into one.
-
Docstrings that tools and readers can use
PEP 257 defines where docstrings go and how they are formatted; a consistent style (Google or NumPy) with a one-line summary, argument and return descriptions and raised exceptions makes them usable by readers, editors and documentation generators.
-
Floating-point numbers: why 0.1 + 0.2 is not 0.3
Binary floating point represents most decimal fractions approximately, so arithmetic accumulates rounding error; compare with tolerances, sum carefully, use integers or decimal types for money and counts, and print with enough digits to round-trip.
-
Loading YAML safely
Full YAML loaders can instantiate arbitrary objects from tagged nodes; always use a safe loader, pin the YAML version semantics, and validate the result against a schema before use.
-
Glob pattern dialects: what * and ** mean in .gitignore, .dockerignore, bash, Python and GitHub Actions
The same star and double-star characters mean different things depending on where the pattern is evaluated: a slash-less .gitignore pattern matches at every depth while the same line in .dockerignore only matches at the context root, bash's globstar recursion is opt-in, and Python's fnmatch is not even path-aware.
-
When asyncio helps and when it does not
asyncio runs many I/O-bound tasks on one thread by suspending at await points; it does not speed up CPU-bound code, and blocking calls inside coroutines stall the whole loop.
-
Automated formatting and linting as a team contract
Delegating layout to a formatter and mechanical checks to a linter removes style debates from review; EditorConfig, PEP 8 and tools such as Ruff show how the contract can be encoded in the repository.
-
Protocol classes: structural typing for duck-typed Python
A typing.Protocol declares the methods and attributes a consumer needs; any object that has them satisfies the type without inheriting from it. Define protocols next to the code that depends on them, keep them minimal, and use runtime_checkable only where isinstance is really needed.
-
Finding a memory leak with tracemalloc snapshots
Start tracing early with PYTHONTRACEMALLOC or tracemalloc.start(nframe), take a snapshot after warm-up and another after N iterations, filter import noise, and read compare_to(..., 'lineno') for lines whose size grows proportionally to N; switch to 'traceback' grouping to see the callers.
-
Money and other exact quantities: use Decimal, not float
Binary floating point cannot represent most decimal fractions exactly, so sums of prices drift; the decimal module provides exact decimal arithmetic with explicit rounding, and integers in minor units are an alternative.
-
Choosing between threads, processes and asyncio for a Python workload
Classify the hot path first: waiting on I/O suits asyncio (many connections, async libraries) or a thread pool (few blocking calls); pure-Python CPU work needs processes or a free-threaded build; native code that releases the GIL can use threads. Bound every pool, choose the process start method explicitly and write the shutdown path.
-
Dataclasses for plain records
dataclasses generate __init__, __repr__ and equality from annotated fields; frozen dataclasses give immutable value objects, slots reduce memory, and field(default_factory=...) avoids shared mutable defaults.
-
At what workload does the free-threaded CPython build beat a process pool for a mixed I/O and CPU service?
Open question: the free-threaded build removes the GIL but adds single-threaded overhead and may fall back to the GIL when an unprepared extension is imported, while process pools pay for pickling and memory duplication; for which CPU-to-wait ratios, working sets and core counts does a thread pool on the free-threaded build deliver more throughput per core?
-
Attempting an exercise before reading its worked solution yields better one-month recall of a language feature than reading the solution first
Hypothesis: for an engineer learning a specific language feature, such as Python's match statement, working an exercise before seeing the worked solution leads to better delayed recall and transfer than studying the solution first and then working a similar exercise; a proposed within-person test with two features, two orders and a one-month delayed test, with no result claimed.
-
asyncio cancellation: perform cleanup without swallowing CancelledError
Keep cancellation observable by the task owner while releasing resources on the interrupted path.
Machine-readable: JSON