Obfuscation and minimization in programming is popular enough that people compete in it. Here are some primers to get started with obfuscation in Python.

We’ll build a script that continuously shows the current hour, minute and second in a console, and we’ll try to minimize the number of characters by obfuscating it.

An output of 8 will look like this (each digit has 5 rows and 4 columns);

`#####  ######  #####`

So when running “`python clock.py 1234567890`“, the output would look like this:

`   # #### #### #  # #### #### #### #### #### ####   #    #    # #  # #    #       # #  # #  # #  #   # #### #### #### #### ####    # #### #### #  #   # #       #    #    # #  #    # #  #    # #  #   # #### ####    # #### ####    # #### #### ####`

To begin with, there are only four different kind of lines used to represent the numbers, so we only need a list for each number telling the program which line to use for that particular number.

There are only four lines that we need to fully express each digit:

`0: '####'1: '#  #'2: '#   '3: '   #'`

To print the number 9, we would use the lines 0, 1, 0, 3, 0:

`0: '####'1: '#  #'0: '####'3: '   #'0: '####'`

So for the number 9 we have a list of those integers represented as a vector, `[0,1,0,3,0]`, and likewise for the other numbers. If we then have a list of the different kind of lines, `["####","# #","# "," #"]`, we can then print them using the indices from our vectors we just created.

To print the number 9:

But now we can have a lot more numbers as the argument, so we have to do this for every number. Extending the list to include all the vectors for each number, we get something like the following (`n` is the lines for the number `4` and so on):

Now we have a program that prints ascii numbers, but we can make it a lot shorter with list comprehension and some small tricks.

The array can be shortened by making a string of it. A string is the same as a list of characters, so it makes no difference from the current array, except that we have to index it a bit different since we have a single list now instead of a list of lists. `int("01030")` is the same as `[0,1,0,3,0]`, but we get fewer characters since we don’t need the commas. Now the `n` variable changes to this:

And to index it right, all we have to do is take the current number that we’re supposed to print, multiply it by five (since there are five lines, so the number would be five times the number elements from the beginning) plus the current line (`0-4`).

And finally with some list comprehension we can get all this to one line. We start from the back. After one line has been printed for each number specified, there should be a newline, so we would have a `'\n'.join()` statement to begin with. After that, we print the lines one after the other:

If we then remove the comments, newlines and spaces we get the following nice piece of code.

And if we now replace the variable `lines` in that with the value of `lines` instead, and the same with the variable `n`, we get the following (included line breaks so it would fit).

Lastly, this post was about a digital clock, so we import the `time` module and call it `t`, put everything in a while loop, add `t.sleep(1)` at the end, replace the `sys.argv` with `t.strftime("%H%M%S")`, replace `"####"` with `'#'*4` (one less character) and clear the screen.

I used `print '\n'*99` instead of `os.system("clear")` since it’s less code (and also don’t have to import the `os` module). This is what we get:

And we’re done. 225 characters (without the line breaks).