Episode 192: Practical Python Decorator Uses & Avoiding datetime Pitfalls
The Real Python Podcast
Feb 16, 2024 57m
What are real-life examples of using Python decorators? How can you harness their power in your code? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder’s Weekly articles and projects.
Episode Sponsor:
We discuss a recent article series that digs into Python decorators. The first two articles discuss the basics of constructing decorators. The third part describes how popular Python libraries use decorators with call interception, function registration, and enriching the behavior of a function.
Christopher shares a piece about the common pitfalls of working with the Pythondatetime
library. The article considers how current third-party libraries don’t address most of these quirks and offers a potential solution with a new library.
We also share several other articles and projects from the Python community, including a couple of news items, a discussion about the popularity of the Rust language, handling unset values in FastAPI with Pydantic, working with Python’s mini-language for formatting strings, mocking Django queryset functions, and a modern replacement for the Requests library.
This week’s episode is brought to you by Sentry.
Course Spotlight: Python Decorators 101
In this course on Python decorators, you’ll learn what they are and how to create and use them. Decorators provide a simple syntax for calling higher-order functions in Python. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
Topics:
- 00:00:00 – Introduction
- 00:02:53 – Django security releases issued: 5.0.2, 4.2.10, and 3.2.24
- 00:03:10 – Python 3.12.2 and 3.11.8 are now available
- 00:03:21 – Introducing PSF Grants Program Office Hours
- 00:04:19 – Python’s Format Mini-Language for Tidy Strings
- 00:12:22 – Ten Python
datetime
Pitfalls - 00:18:34 – Sponsor: Sentry
- 00:19:37 – Real Life Use of Decorators
- 00:29:18 – Handling Unset Values in FastAPI With Pydantic
- 00:35:43 – Video Course Spotlight
- 00:37:06 – The Python Rust-Aissance
- 00:50:19 – django-mock-queries: Mock Django Queryset Functions
- 00:53:09 – niquests: Requests but Multiplexed
- 00:55:55 – Thanks and goodbye
News:
- Django security releases issued: 5.0.2, 4.2.10, and 3.2.24
- Python 3.12.2 and 3.11.8 are now available
- Introducing PSF Grants Program Office Hours
Show Links:
- Python’s Format Mini-Language for Tidy Strings – In this tutorial, you’ll learn about Python’s format mini-language. See how to use it for creating working format specifiers and build nicely formatted strings and messages in your code.
- Ten Python
datetime
Pitfalls – It’s no secret that the Pythondatetime
library has its quirks. Not only are there probably more than you think, but third-party libraries don’t address most of them! Arie created a new library to explore what a betterdatetime
library could look like. - Real Life Use of Decorators – Part 3 in a series on how Python decorators are used. This part covers real-life use cases including call interception, function registration, and behavioral enrichment.
- Handling Unset Values in FastAPI With Pydantic – When using the HTTP PATCH method only those fields that got changed are updated. Pydantic sets fields not given as arguments as
None
so there is no way to distinguish between an explicitNone
value and an unset field. This post explains how you process this scenario.
Discussion:
- The Python Rust-Aissance – Companies like Polars are showing how with Rust, Python developers now have a better, smoother path towards building high-performance libraries.
- Rye: A Python Developer Experience Vision Continued
- PyO3: Rust bindings for the Python interpreter
- Rust in Linux: Where we are and where we’re going next
- RustPython: OSS CPython Written in Rust
- granian: Rust HTTP Server for Python Applications
Projects:
Additional Links:
- Backus–Naur form - Wikipedia
- ANTLR - (ANother Tool for Language Recognition) A Powerful Parser Generator
- Xmas Decoration, Part 1 - Bite code!
- Xmas Decoration, Part 2 - Bite code!
- Falsehoods programmers believe about time - @noahsussman - Infinite Undo
- 10 Reasons You Should Quit Your HTTP Client - Ahmed TAHRI - Dev Genius
- HTTPX - Fully Featured HTTP Client for Python 3