Reusing Pydantic validators and serializers

Posted on 2025-04-21 in Trucs et astuces • Tagged with Python, Pydantic

I’ll give some tips to reuse field and model validators and serializers. All my examples will use validators, but it works exactly the same for serializers.

Field validators

Assign the validator to a variable like so:

def _is_above_ten(value: int):
    if value > 10:
        return value

    raise ValueError(f"{value …

Continue reading

Using Pydantic with custom classes

Posted on 2025-04-19 in Programmation • Tagged with Python, Pydantic

Pydantic is an awesome data validation library for Python. It’s getting very popular these days because it’s fast, has lots of features and is pleasant to use. I’m using it at work and it personal projects. It’s one of the strong points of FastAPI, the new …


Continue reading

Converting jupyter notebooks

Posted on 2025-04-17 in Trucs et astuces • Tagged with Python

You can export a notebook directly from the interface under File > Save and Export Notebook As. You can also do it in CLI with:

jupyter nbconvert --execute --to <FORMAT> my_notebook.ipynb

The --execute will execute the notebook to make sure all output cells are up to date. Many export formats …


Continue reading

Some tips on Python’s enums

Posted on 2025-04-12 in Programmation • Tagged with Python

I’d like to share a few tips I recently (re)discovered about Python’s enums. While interesting on its own, this article is also paving the way on a more advanced article I hope to write soon. Let’s dive right in!

auto

By using the function enum.auto …


Continue reading

My take on UV and Ruff

Posted on 2025-01-25 in Programmation • Tagged with Python

I recently tried the new and shiny tools made by astral. I only used them on my personal projects yet, but I’m still very impressed! You may already have heard of them. I’ll try to keep the article concise and won’t dig too deep into the tools …


Continue reading

Weird test behavior in my Django project test suite after an IntegrityError

Posted on 2024-12-01 in Programmation • Tagged with Python, Web, Django

Recently I encountered a very weird behavior in my Django project test suite. I created a view that caught an IntegrityError from the database (caused by duplicates in a unique index). When this error occurs, I want to respond with a 409 - CONFLICT status code and an error message.

TL …


Continue reading

Django async

Posted on 2023-12-10 in Programmation • Tagged with Web, Django, Python

Now that Django is fully async (views, middleware and ORM), I though it was a good time to test how it behaves when run asynchronously. I’ll try to keep this article concise with only relevant data and resources. Code can be seen in a sample project so you can …


Continue reading

Use the same function as context manager and decorator

Posted on 2022-09-25 in Trucs et astuces • Tagged with Python

I recently learned that context managers created with @contextmanager can be used either as a context manager or a decorator:

from contextlib import contextmanager

@contextmanager
def test_context():
    print('Entering')
    yield
    print('Leaving')

with test_context():
    print('Inside')

@test_context()
def test_decorated():
    print('Decorated')

test_decorated()

We will yield:

Entering
Inside
Leaving

Entering
Decorated …

Continue reading

Exploring a weird HTTP issues

Posted on 2022-05-04 in Programmation • Tagged with Python, Django, nginx, GCP, devops

Today I'd like to explain how I tried to solve a weird HTTP issues that I found at work. I hope you will find the method and the trials I used the useful/interesting if/when you will encounter something similar.

The issue was this: I needed to dynamically generate …


Continue reading

How to avoid CSRF token issues with Django when running on different sub-domains

Posted on 2022-02-13 in Trucs et astuces • Tagged with Python, Django

If you deploy multiple Django websites on your infrastructure on various subdomains, you may get issues about invalid CSRF tokens. This is happening either because:

  • You erase the cookie by using the same domain. For instance, if you have your prod API at api.jujens.eu and pre-production one at …

Continue reading