While working with Django, I often find it difficult to handle list of key value pairs. This generally happens when we are dealing with responses from a web client or doing some manipulation on a list of same kind of objects.

Let us say we have a schedule of a room in form of time-slots and each slot represents an hour of the day, so there are 24 time-slots starting form 0:00 till 23:00. Each time-slot has a price associated with it eg 10:00 has €20 etc. For simplicity I am refer this combined unit of hour and price as a time-slot.

price as a time-slot

Suppose a user has booked slots from 10:00 to 18:00 hours on some future date, and at some point the host of the room wants to change the price of these time-slots on that particular date, then we need to show him the conflicting time-slots ie 10:00 to 18:00 hours.

I need to create a list of time-slots from the response, when host submits the new schedule, then compare it with the available booking schedule, all the time-slots which are there in booking schedule but not in new schedule are the conflicting time-slots.

There are 3 ways of doing this.

  1. Create a dictionary with hour and price key values and create a lamda function to do the set operations. Since we cannot put a dictionary in a set we need to use a list.
  2. Create a separate TimeSlot class with hour and price as variables and override its equals method to enable us to perform comparison and set operations.

This approach is a bit more sophisticated, but it also requires us to create an additional class. Also since our requirement is restricted to just set operations and we are not going to reuse it anywhere else, we should look for another option.

Remember simple is better than complex,

  1. How about having a temporary class whose instance can work as a normal objects and which also does not require us to write all those __init__() and __eq__() code.

Namedtuples to the rescue

namedtuple instances are just as memory efficient as regular tuples because they do not have per-instance dictionaries. Each kind of namedtuple is represented by its own class, created by using the namedtuple() factory function. The arguments are the name of the new class and a string containing the names of the elements.

We define a namedtuple with hour and price as its attributes,

Now as discussed previously, we shall have 2 sets of schedule a new one and the booked one. We can easily perform set operation to identify the conflicting time-slots,

conflicting_timeslots represents all the time-slots which were booked and whose price has changed. Since we are using tuples we do not require to override __equ__() and its way simpler than class implementation. If you dig a deeper you come to know that namedtuple are used in QuerySet for Django ORM.

Click here for more details…

At BoTree Technologies, we build enterprise applications with our Python team of 15+ engineers.

We also specialize in RPA, AI, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!