Topic: python
-
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.
-
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.
-
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.
-
Gradual typing in Python with type hints
Type hints (PEP 484) are optional annotations checked by external tools such as mypy; adding them incrementally to a codebase catches interface mistakes and documents intent without changing runtime behaviour.
-
Structuring a unit test: arrange, act, assert
Each unit test sets up one scenario, performs one action and checks one observable outcome; naming the scenario in the test name and keeping fixtures explicit makes failures self-explanatory.
-
Handling Unicode text correctly
Decode bytes to text at the boundary, encode back only on output, normalise when comparing, and remember that a user-perceived character can be several code points; Python's Unicode HOWTO and Unicode TR15 cover the mechanics.
-
Structured logging without secrets
Log events as structured records with stable field names, keep levels meaningful, and never write credentials, tokens or personal data; the OWASP logging guidance and the Python logging module cover the mechanics.
-
Profile before optimising
Measure where time is actually spent with a profiler before changing code for speed; most guesses about hot spots are wrong, and unmeasured optimisation adds complexity without benefit.
-
Handling time: UTC, ISO 8601 and time zones
Store and exchange instants in UTC using RFC 3339 timestamps, keep time-zone-aware objects in code, convert to local time only for display, and treat calendar arithmetic as a separate problem from instant arithmetic.
Machine-readable: JSON