Django can be used to build great websites. But it is also really good for solving small problems quickly. Introducing a new series called “Django Nibbles”, to help you learn an aspect of Django say Templates through a short and simple problem. You can either solve the problem yourself or follow my step-by-step solution.

Q. Create a page that displays a binary clock showing the current time (when the page was loaded).

For instance, if the time is “23:55:02” then we should see:

○○○○○○○○●●○○●●○○○●○●●●○○

Each column represents a binary digit when read from top to bottom. For more details, read the Binary Clock wiki page.

The home view function returns the current time in plain text. We are importing the now function from django.utils.timezone to get the current time in a timezone-aware format. Calling localtime converts it to the timezone defined in the project’s settings.py file.

Invoking HttpResponse gives you a low-level control over the exact string to return to the browser i.e. the first argument. You can also mention the content-type which, by default, is set to HTML i.e. “text/html; charset=utf-8”. Plain text is good enough for our purpose. We will be using unicode characters, so we need to mention the charset as well.

Try running the server now:

$ ./manage.py runserver

If you open the browser and point it to the default URLhttp://127.0.0.1:8000/, you should see the date and time displayed. Unless you are in the UTC timezone, this might be different from your current timezone.

Let’s change this to your current timezone (find yours from the timezone list). Open binclock/settings.py and find the line starting with TIME_ZONE and change it your timezone. Since I live in Bangalore, my line would look like this:

TIME_ZONE='Asia/Calcutta'

Now refresh the browser page and you should see your current time.

Converting to binary

We will convert to the binary format step-by-step. Notice that each digit has to be converted in Packed BCD format rather than its binary form. For e.g. 12 in binary is 1100 but we need it in BCD form, which is 0001 0010.

Python prompt will be indicated by “>>>” (actually, I had used IPython). Open up the shell using ./manage.py shell command:

We are using Python’s bitwise operators ‘&’ and ‘>>’ here. To extract the four rightmost bits, we use a bitmask and then shift the result right. We can convert a single decimal digit to a list of binary digits using bcd_digit. But numbers in a clock can have upto two digits.

The disp_unicode function uses a lot of Python tricks. We can find the transpose of any matrix using the zip(*matrix) trick. We are also using nested list comprehensions to iterate row by row and then column by column. The inline if statement (also called a conditional expressions) transforms bits into pretty unicode characters.

Hi! Welcome to ArunRocks, an odd collection of writeups on programming, travel, gadgets and practically anything under the sun. This state of affairs could be blamed on the ecelectic interests of your host, Arun Ravindran. He loves programming in several languages especially Python. He is currently a developer member of the Django Software Foundation. Read more...