Speeding up your django tests

Running tests can become troublesome, if your tests take too long to execute. Ideally, you want an instant feedback from them. Lets us discuss some ways through which we can speed up the run time for our tests. Move data creation to setUpTestData setUpTestData is pretty much like setUpClass in unittest. This method is run once before the test class runs. One of the main reasons, tests can be slow is if data creation in done in setUp method or inside individual tests....

March 1, 2021 · 4 min

Writing unit tests for migrations in django

Writing tests for migration can be a bit tricky because all of your migrations are generally run before any of your test. In case you want to test your code for a particular migration, you will have to revert the database to that particular state. Make sure you don’t forget to revert to the latest state after this test ends. In this post, we will see who we can do this....

March 1, 2021 · 3 min

Add tests to verify registry of models to django admin

In the last post, we learned how to automate registration of models to the django-admin. In this post, we will try to see how we can add tests to verify that all our models have been registered. Why do we need it? Even though the admin.py module is run internally when running the tests, it doesn’t actually verify whether all models have been registered or not. It is a very common thing to forget registering your model to the admin, especially when you aren’t really customizing them....

July 1, 2021 · 2 min

Separate development and production settings for a django project

When I first started working on Django, I used to edit the settings file every time I had to push the code to deployment. Change the value of DEBUG, read the values for SECURITY_KEY from a config file and many other settings. It used to be a really painful experience and I would curse myself every time I had to do that. Then, one fine day I decided that I have had enough and thought of finding ways through which I don’t need to make any changes when I push my code into deployment....

February 1, 2021 · 4 min

Caching expensive operations in python

Problem You have a function that takes a lot of time to execute. You need to call that function frequently, at different points in your code with the same arguments. This expensive function seems to be the bottleneck. Hopefully, after reading this post you will be able to deal with this situation and make your code work faster. Caching the output You can cache the output of the expensive function using lru_cache....

August 15, 2021 · 3 min

Debugging Python code

Much of a programmer’s time is spent debugging code than writing one. While I agree that it’s not a great thing, we can’t do much about it. But what we can do is maybe make this process more efficient and fun. In this post, we will try to discuss some techniques that can be useful for debugging. The order goes from a beginner level to a more advanced one. Using print Caution: This is not one of the recommended ways to debug things....

May 1, 2021 · 6 min

Use named tuples to stop yourself from remembering order of tuples

Let’s consider an example where you’re returning some info about a Person object from a function in the form of a tuple. def info(): ... return name, address, age Now, in this scenario the receiving function needs to know the sequence in which the values are being returned. Although, this might not seem to be much of an issue for a one-time use case, let me try to explain some possible issues with this....

April 15, 2021 · 3 min

An awesome django filter for displaying numbers

Displaying large numbers like 1234, 1243423, etc. in their raw form can be less intuitive for viewers. Such large numbers make it difficult to calculate the actual value of the number. Hence I thought of making a filter inside my Django application(you may call it app), for displaying numbers in a more informative format(and awesome too, I guess). The file structure and format might be specific to Django, but the logic can be used in any other framework or language....

April 1, 2021 · 4 min

Automating registry of django models to the admin

Registering models to django-admin(in general) is not much of a task, but is mostly boring. Many times it might feel like doing a chore. In this post, we’ll try to see how we can automate this process. Registering models Normally, models are registered inside the admin.py module of the apps. To automatically register all of our models, we can directly add these lines of code to this file. from django.contrib import admin from django....

April 1, 2021 · 2 min

Enabling tab completion in pdb

I will admit being a big fan of pdb, and the debugging functionalities it provides. It still could do more by adding tab completion to guess attributes for python objects. In this post, we will try to address this caveat. Setting up Tab Completion Create a .pdbrc in your home directory. Add the following contents to the file # this adds tab completion import rlcompleter __import__('pdb').Pdb.complete = rlcompleter.Completer(locals()).complete Just save this file and use breakpoint(or import pdb;pdb....

April 1, 2021 · 1 min