Episode 109: Start Testing Your Python with doctest & Pagination in Django
The Real Python Podcast
May 13, 2022 56m
Did you know you can add testing to your Python code while simultaneously documenting it? Using docstrings, you can create examples of how your functions should interact in a Python REPL and test them with the built-in doctest module. This week on the show, Christopher Trudeau is here, bringing another batch of PyCoder’s Weekly articles and projects.
Episode Sponsor:
Christopher shares an article by previous guest Mike Driscoll about testing with doctest. This is a great way to get started with testing your own code, and it offers the added benefit of documenting functionality.
We talk about the recent Real Python article “Pagination for a User-Friendly Django App.” Spreading your content across multiple pages can significantly improve the user experience of your web application. This article takes you through configuring Django’s built-in pagination tool and how to combine it with other web tools.
We discuss a recent article about Python type hints and the author’s disappointment. We also include reactions from a couple of online communities.
We cover several other articles and projects from the Python community, including why it’s important to close files in Python, how dunder methods are awesome, a bidirectional Python dictionary, prettier git diffs, and a command-line game to learn git.
Spotlight: Python Coding Interviews: Tips & Best Practices
In this step-by-step course, you’ll learn how to take your Python coding interview skills to the next level and use Python’s built-in functions and modules to solve problems faster and more easily.
Topics:
- 00:00:00 – Introduction
- 00:02:15 – PyCon US 2022 Follow-up
- 00:07:57 – Why Is It Important to Close Files in Python?
- 00:15:17 – Dunder Methods in Python: The Ugliest Awesome Sauce
- 00:24:26 – Sponsor: Mailtrap
- 00:25:08 – Python Testing With doctest
- 00:28:20 – Python Bidirectional Dictionary
- 00:30:27 – Pagination for a User-Friendly Django App
- 00:36:07 – Video Course Spotlight
- 00:37:27 – Python’s “Type Hints” are a bit of a disappointment to me
- 00:52:25 – dunk: Prettier Git Diffs
- 00:53:43 – git-gud: Command-Line Game to Learn git
- 00:55:40 – Thanks and goodbye
Topic Links:
- Why Is It Important to Close Files in Python? – Model citizens use context managers to open and close file resources in Python, but have you ever wondered why it’s important to close files? In this tutorial, you’ll take a deep dive into the reasons why it’s important to close files and what can happen if you dont.
- Dunder Methods in Python: The Ugliest Awesome Sauce – Double-underscore methods, also known as “dunder methods” or “magic methods” are an ugly way of bringing beauty to your code. Learn about constructors,
__repr__
,__str__
, operator overloading, and getting your classes working with Python functions likelen()
. - Python Testing With doctest – Python’s
doctest
module allows you to write unit tests through REPL-like sessions in your docstrings. Learn how to write and executedoctest
code. Also available in video. - Python Bidirectional Dictionary – Learn about the Bidict library, a bidirectional dictionary where your keys and your values can both be used to look up an item. This can be a useful tool when dealing with mapped data, like country code to country name, where you want to look up either side of the relationship.
- Pagination for a User-Friendly Django App – In this tutorial, you’ll learn how to serve paginated content in your Django apps. Using Django pagination can significantly improve your website’s performance and give your visitors a better user experience.
Discussion:
- Python’s “Type Hints” are a bit of a disappointment to me | Original Article
- Python’s “Type Hints” | Lobsters Thread
- Python’s “Type Hints” | Hacker News Thread
Projects:
Additional Links:
- Episode #47: Unraveling Python’s Syntax to Its Core With Brett Cannon – The Real Python Podcast
- Python Coding Interviews: Tips & Best Practices – Real Python
- Episode #88: Discussing Type Hints, Protocols, and Ducks in Python – The Real Python Podcast
- Up and Running with Git Online Course - Talk Python Training
- Python’s doctest: Document and Test Your Code at Once - Real Python tutorial