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

Random python nuggets

Python is already packed with insanely good standard libraries which often makes our job incredibly easy to solve. Apart from that, it comes with some hidden features(also known as easter eggs) that may amaze you. Let’s start: The Zen of Python >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated....

March 15, 2021 · 2 min