Console screensaver in Python

Python is a very powerful programming language and previously I worked with it little bit but not seriously. Recently, I have started to learn it again.

The very first thing I really like about Python is its inline compilation which is a feature of dynamic programming languages.

The other feature I like is the size of program and its complexity. Since Python is very rich in library, you do not need to work so hard and write a lot of code and this also keeps your program less buggy and more readable.

However, the thing which I do not like that much is optional use of semicolon, anyway I am old school programmer and still use it in Python even though that it is optional. 🙂

Anyhow, in the process of learning Python I have decided to write some simple silly programs for better understanding and learning Python.

From my perspective learning a new programming language is similar with learning how to fix a car, you won’t learn until your hands get dirty like a real mechanic. Same concept applies to programming means you will not learn anything until you start coding by yourself.

In order to do so I have made my hands dirty and have written my very first Python program with lot of help from my good friend Google which I have criticized it heavily in my previous post.

The following code is a simple colorful screensaver for terminal.

#!/usr/bin/python
import os;
import time;
import sys;
from termcolor import colored;
from random import randint;
string = "";
list = ['grey','red','green','yellow','blue','cyan','white'];
rows, columns = os.popen('stty size', 'r').read().split();
while(1):
    for i in range(int(rows)*int(columns)):
        rnd = randint(0,len(list)-1);
        string+=colored(chr(randint(32,126)),list[rnd]);
    sys.stdout.write(string);
    time.sleep(2);

In order to run the above code you have two ways which are,

$ python code.py

Or

$ chmod a+x code.py
$ ./code.py

And if you got any error regarding ‘colored’ class, just install the package with the below commands easily.

$ sudo apt-get install python-pip
$ sudo pip install termcolor

And don’t forget about indentation in Python. Have a great weekend!

Send your idea and information to kasra@madadipouya.com

Leave a Reply