# Introduction to obfuscated Python code

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 “

“, the output would look like this:**python clock.py 1234567890**

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

# # # # # # # # # # # # # #

# #### #### #### #### #### # #### #### # #

# # # # # # # # # # # # #

# #### #### # #### #### # #### #### ####

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,

, and likewise for the other numbers. If we then have a list of the different kind of lines, **[0,1,0,3,0]**

, 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 (

is the lines for the number **n[4]**

and so on):**4**

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.

is the same as **int("01030"[1])**

, but we get fewer characters since we don’t need the commas. Now the **[0,1,0,3,0][1]**

variable changes to this:**n**

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

statement to begin with. After that, we print the lines one after the other:**'\n'.join()**

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

And if we now replace the variable

in that with the value of **lines**

instead, and the same with the variable **lines**

, we get the following (included line breaks so it would fit).**n**

Lastly, this post was about a digital clock, so we import the

module and call it **time**

, put everything in a while loop, add **t**

at the end, replace the **t.sleep(1)**

with **sys.argv[1]**

, replace **t.strftime("%H%M%S")**

with **"####"**

(one less character) and clear the screen.**'#'*4**

I used

instead of **print '\n'*99**

since it’s less code (and also don’t have to import the **os.system("clear")**

module). This is what we get:**os**

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

*Thanks for reading, and feel free to leave a comment or question!*