Recent writings

Distil git logs attached to a single file

View git commit history for a single file with git log --follow. Learn to track multiple files with xargs and concatenate their commit logs.

Safer 'operator.itemgetter' in Python

Use operator.itemgetter for faster sorting and element access with graceful KeyError handling via methodcaller for safer operations.

Guard clause and exhaustiveness checking

Master TypeScript's never type and Python's NoReturn for exhaustiveness checking. Flatten nested conditionals with guard clauses for cleaner code.

Health check a server with 'nohup $(cmd) &'

Run background health checks in CI with nohup and ampersand. Test server readiness with retry loops and automatic cleanup on success or failure.

Return JSON error payload instead of HTML text in DRF

Fix Django REST Framework to return JSON error responses for 403, 404, 500 errors using middleware instead of default HTML pages.

Decoupling producers and consumers of iterables with generators in Python

Learn how Python generators decouple data production from consumption, enabling cleaner code for streaming, polling, and pipeline patterns.

Pre-allocated lists in Python

Understand CPython list memory allocation: how lists store pointer references, grow dynamically, and when pre-allocation with [None]*n helps.

In favor of sentence case

Embracing sentence case over title case in technical writing eliminates capitalization ambiguity and improves readability.

Disallow large file download from URLs in Python

Prevent excessive file downloads in Python by streaming with HTTPX and limiting file size with chunk-based validation and memory-safe processing.

Declaratively transform data class fields in Python

Leverage Python's __post_init__ hook to declaratively transform dataclass fields. Automatically serialize data with clean, maintainable code.

Mocking chained methods of datetime objects in Python

Mock chained datetime methods in Python tests using unittest.mock to handle immutable datetime objects without external dependencies.

How not to run a script in Python

Fix Python ModuleNotFoundError by using python -m instead of direct script execution to ensure correct sys.path handling for imports.

Caching connection objects in Python

Learn efficient patterns for caching database connection objects in Python without import-time side effects or lru_cache complexity.

Declarative payloads with TypedDict in Python

Use Python TypedDict to declaratively define API payload structures. Get type safety for nested dictionaries and improve code maintainability.

Parametrized fixtures in pytest

Create dynamic pytest fixtures with @pytest.fixture(params) to run tests with multiple configurations and parameter combinations.