Check list in Python

After adding a progress-bar to my scripts I would add a check list showing the end status of each one of its parts.

To achieve it I used the package colorama which allows me to add color to the text. An alternative package could be blessing.

The following code shows the behavior I give to some of my scripts:

from time import sleep
import sys
from colorama import init, Fore

init() # needed for colorama

checks = [("Load BAM files", 2, True),
    ("Variant Calling", 7, True),
    ("Variant Annotation", 5, True),
    ("Variant Evaluation", 3, False),
    ("Select Variants (INDEL)", 10, False),
    ("Variant Filtration (INDEL)", 1, False),
    ("Select Variants (SNP)", 5, True),
    ("Variant Filtration (SNP)", 5, True),
    ("Variant Combination", 2, True),
    ("Save Variant file", 4, True)
]

ml = max([ len(x[0]) for x in checks ])
nc = 20

for row in checks:
    inf = row[0] + " " * (ml - len(row[0]) + 4 )
    frc = nc / row[1]
    lim = row[1] * frc
    sp = 0.25 * row[1] / nc
    for i in range(lim + 1):
        sys.stdout.write('r')
        sys.stdout.write('%-s [%-20s] %d%%' % (inf, '=' * i, i * 5))
        sys.stdout.flush()
        sleep(sp)
    sys.stdout.write('r%-s  %-14s [' % (inf, ' ' * 14))
    if row[2]:
        sys.stdout.write(Fore.GREEN + ' OK ' + Fore.RESET)
    else:
        sys.stdout.write(Fore.RED + 'FAIL' + Fore.RESET)
    sys.stdout.write(']     n')

References:

  • colorama: colorama at PyPI – link
  • blesing: blesing at PyPI – link
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: