Posts
-
How to dump Django ORM data to JSON while debugging?
Sometimes, I need to debug specific high-level tests by inspecting what gets created in the database as a side effect. I could use a debugger and poke around the Django ORM at a breakpoint - but quite often it’s simply faster to dump the entire table to JSON, see what’s there, and then apply fixes accordingly.
-
Why using [n] on a Django QuerySet can be unsafe?
What if I told you that the following way of taking the second object from a Django
QuerySetmight be unreliable and can lead to non-deterministic failures under some circumstances?second_service = Service.objects.filter(service_type="L3Connection")[1] -
How to parametrize exception testing in PyTest?
Sometimes it’s useful to provide different input data and test different exceptions being raised. Not every exception deserves its own unit test, though. In such cases, I tend to combine PyTest’s
parametrizemarker with Python’scontextlib.nullcontextbuiltin. -
How to reuse Pydantic model_validator across multiple models without boilerplate code?
Recently I’ve been working with Pydantic models quite a lot. And I mean multiple models with multiple fields and validators. I noticed that in some cases, reusing validators in some smart way would reduce my codebase by even hundredths of lines of code. So I tried to come up with a solution for that problem…

-
How to check whether Python script has elevated privileges?
It may happen that a Python script needs
rootprivileges on Linux / macOS oradminprivileges on Windows to run properly. If it does not have them, there is no point in continuing. Let’s see how to quickly check whether the current runtime has those privileges. -
>>> print("Hello World!")
subscribe via RSS