Day 69: Make Good Looking Tables With PrettyTable (Module)


Scripts running in the shell are pretty cool. But sometime you want to display tables that have no real format.


The module PrettyTable generates some – well pretty – tables with just a couple of lines.


The script takes a dictionary with the sizes of all planets in the solar system.


It exports it in a PrettyTable in descending order.


# PrettyTable module
# Create handsome tables in your shell
from prettytable import PrettyTable

planets = {"Mercury" : "4879",
        "Venus" : "12104",
        "Earth" : "12756",
        "Mars" : "6779",
        "Jupiter" : " 142984",
        "Saturn" : "120536",
        "Uranus" : "50724",
        "Neptune" : "49244"

# create the table with two colums
size_table = PrettyTable(["Planet", "Diameter (km)"])

# go over every planet in dict and add a row for each
for item in planets:
    size_table.add_row([item, planets[str(item)]])

# sort planets from big to small
size_table.sort = True


The output looks like this:



Day 58: Using Progress Bars In The Shell (Module)


The module ProgressBar generates timed progress bars in the terminal while you execute a program.


A progress bar is seldomly really necessary but sometimes it is just neat to display some progress for the user


You can find the documentation here


An easy example:


from time import sleep                          # to use the timer
from progressbar import ProgressBar             # progressbar module

print("Multiplying 987.000 * 1.210.100")        

num = 987000 * 1210100

bar = ProgressBar()                         
for i in bar(range(100)):

print("Result: " + str(num))

And this is what it looks like in the terminal:


Day 28: A Simple Password Generator With random.choice()

What is the most elegant way to choose a random char from a string, list or tuple?


It’s random.choice(some_string)


I build a simple password generator that will build a password the length the user has chosen.


If the length is smaller than 8 though the user will have to redo his entry.


This is what the output looks like:



This is what the code looks like:


# A simple password generator
# creates a random String
# will ask anew if length was too short

import random

# the string contains all the chars we want to use.
alphabet = "1234567890abcdefghijklmnopqrstuvwxyz!@#$%^&<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def pass_gen(pass_length):
    i = 1
    password = ""
    if (int(pass_length) >= 8):
        while i <= int(pass_length):
            # random.choice() is the shortest and lean way
            # to select a random char from a string, list or tuple
            password = password + random.choice(alphabet)
            i += 1
        pass_gen(input("Chosen length too short. Enter a length (min 8): "))


pass_gen(input("Enter the maximum length of your password (min 8): "))

You find more information about random.choice() here.

Day 13/14: Cracking the Code – the string maketrans function


In this episode we are cracking one challenge from the amazon Python Challenge


In entry two there is a encrypted code that needs some cracking!

If you look closely at the string you notice that all letters are shifted by two. Instead of “abc” its “cde” and so on.

To crack this code we use the maketrans-function.


Here is the code:

import string

raw = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle grgl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."

# the maketrans function transforms our coded string with a new set of letters
table = string.maketrans(
    "abcdefghijklmnopqrstuvwxyz", "cdefghijklmnopqrstuvwxyzab"

# setting up the decoded string for printing.
result = raw.translate(table)


The output looks like this:


Day 2: A real classic – guess a number with Python

Today we code a little app that will take in a users number between 1 and 10. The program will generate a random number between 1 and 10 and will compare the two. If the user guessed right, he will get a friendly message and vice versa.

The Code:

from random import randint #so we can use randint

def guess_number(your_guess):
    random_nr = randint(1,10) # generate a random number between 1 and 10
    if (int(your_guess)==random_nr):
        print("You guessed right!")
        print("Sorry,you guessed wrong.")

guess_number(input("Guess a number between 1 and 10: "))