Recent writings

Static typing Python decorators

Type Python decorators accurately using ParamSpec and Concatenate to preserve wrapped function signatures and enable proper static analysis.

Inspect docstrings with Pydoc

View Python docstrings from the command line with pydoc or serve them as HTML documentation for modules, classes, and functions.

Check whether an integer is a power of two in Python

Use Python's bit_count() method to elegantly check if an integer is a power of two by counting on-bits in binary representation.

Uniform error response in Django Rest Framework

Standardize Django REST Framework error responses using custom exception handlers to match Microsoft's REST API guidelines format.

Difference between constrained 'TypeVar' and 'Union' in Python

Understand when to use constrained TypeVar vs Union in Python type hints for consistent types across function parameters and return values.

Don't wrap instance methods with 'functools.lru_cache' decorator in Python

Avoid memory leaks when caching instance methods with lru_cache by making cache containers local to instances instead of global.

Cropping texts in Python with 'textwrap.shorten'

Crop text to character limits without breaking words using Python's textwrap.shorten for clean truncation with customizable placeholders.

String interning in Python

Learn how Python's string interning optimizes memory by caching strings and using sys.intern() for custom string caching to improve performance.

Structural subtyping in Python

Implement Go-style structural subtyping in Python with Protocol for duck typing and interface-based APIs without inheritance dependencies.

Automatic attribute delegation in Python composition

Implement automatic attribute delegation in Python composition using __getattr__ magic method for inheritance-like attribute access patterns.

Access 'classmethod's like 'property' methods in Python

Learn how to access Python classmethods like property methods using metaclasses or Python 3.9+ decorator stacking for Enum classes.

Don't add extensions to shell executables

Why executable scripts shouldn't have extensions. Learn GitHub's scripts-to-rule-them-all pattern for language-agnostic project automation.

Use __init_subclass__ hook to validate subclasses in Python

Validate subclass schemas at definition time with Python's __init_subclass__ hook, a cleaner alternative to metaclasses for enforcing structure.

Use daemon threads to test infinite while loops in Python

Test Python infinite loops safely using daemon threads with timeouts, allowing tests to complete without hanging or blocking execution.

Running tqdm with Python multiprocessing

Integrate tqdm progress bars with multiprocessing.imap_unordered for visual feedback on parallel task execution across worker processes.