Most Python web frameworks make you choose between testability and convenience. You either have clean code with complex test setup, or you use global state and hope your tests don’t interfere with each other. FastAPI’s Depends() solves this elegantly. Here is an example how you would use it in your API: What’s happening here: Type-safe – Your editor knows repo is a SnippetRepository. Full autocomplete, type checking works. Automatic cleanup – The context manager ensures the database session closes, even if an exception occurs. No global state – Every request gets its own session. No risk of one request interfering with another. Testable –…
Author
Bob Belderbos
Latest articles
by
-
-
In this post, I will build a simple fitness tracker app using Python Reflex. Reflex is a Python library that allows you to create reactive applications using a functional and declarative approach. We will use Reflex to create a simple fitness tracker app that allows you to log the amount of workouts completed per week. This is how it will look: Install Reflex After making a new directory and cd’ing into it, I’ll use uv (anybody not yet using this amazing tool? 💡) to initialize a project and install Reflex: The awesome thing about uv is that you don’t have…
-
The repository pattern is a design pattern that helps you separate business logic from data access code. It does so by providing a unified interface for interacting with different data sources, bringing the following advantages to your system: A practical example Let’s use Python and sqlmodel (PyPI) to demonstrate this pattern (code here): Note: In this implementation, we did not add error handling to keep the example relatively small. You should ensure that if something goes wrong (e.g., database connection issues or file access errors), the program can handle the error gracefully and provide useful feedback. The example might be a bit…
-
In this article I will show you how to deploy a FastAPI app as a function in Azure. Prerequisites are that you have an Azure account and have the Azure CLI installed (see here). Setup Azure First you need to login to your Azure account: It should show your subscriptions and you select the one you want to use. Create a resource group Like this: A resource group is a container that holds related resources for an Azure solution (of course use your own names throughout this guide). It comes back with the details of the resource group you just…
-
Introduction Two weeks ago I wrote an article about ABCs and interface enforcement. Shortly after I learned that you can do this as well with protocols. Python 3.8 introduced quite a groundbreaking feature that further advanced the language’s capabilities in type checking: the typing.Protocol which allows Python developers to define and enforce interface contracts in their code. Unlike traditional dynamic typing, which Python is well-known for, typing.Protocol brings a layer of static type checking that allows for more explicit, readable, and robust code by defining expected behaviors and structures. This feature not only enhances code quality and developer productivity but…
-
Introduction As a Python developer I want to stay up2date with trends and useful tips & tricks. Of course there are great newsletters like Pycoders, but those are already hitting my inbox. Let’s look at Planet Python in this article, an aggregation site/ service that indexes a lot of good Python blog feeds. Keeping an eye on that resource will be useful. In this article we’ll build a tool to parse this resource and get a daily email with new articles. We will use Sendgrid for the emailing and GitHub Actions to run it automatically. Planet Python parse and email…
-
Introduction One cool object-oriented programming (OOP) technique / pattern is enforcing consistent interfaces. In Python you can use Abstract Base Classes (ABCs) for that. 🐍 Using ABCs ensures that all subclasses implement the required methods. This can make it easier to maintain and extend the existing code base. Update Feb 2024: you can also leverage typing.Protocol (>= 3.8) for this, see this article. A real world Pybites example Check out this code example: Note: In the example above, we’ve used the ABCMeta metaclass directly for defining the abstract base class. However, Python provides a more modern and succinct way to…
-
Introduction Python’s versatility in supporting different programming paradigms, including procedural, object-oriented, and functional programming, opens up a rich landscape for software design and development. Among these paradigms, the use of static methods in Python, particularly in an object-oriented context, has been a topic of debate. This article delves into the role and implications of static methods in Python, weighing them against a more functional approach that leverages modules and functional programming principles. The Nature of Static Methods in Python Definition and Usage: Static methods in Python are defined within a class using the @staticmethod decorator. Unlike regular methods, they do…