it-artikel:learning-python3-bargraph.py-small-program-to-generate-an-ascii-bargraph-of-any-width

learning python3 - barGraph.py - small program to generate an ascii bargraph of any width

These are my first steps in python programming language. The result is a little program that draws an ascii bargraph on to your console window, platform independend. it comes with 3 internal demos to show what it can do so far. its not really a beauty and could be done better, but for my first steps in python im quiet happy with my progress in learning python so far. So to help out others to help learn python or help with their programming projects , my code is 100% free to use for any non-commercial personal and non-commercial educational use. have fun with it!

If you have suggestions, patches or want to share your own personal version with me just drop me an email. ill be happy to review other solutions too .

See this part of Demo 3 showing a 50 character wide bargraph. :

61% [==========================.................]

The code

barGraph.py
""" display an ascii barGraph within the boundary of a terminal console. 
 
    This is just an experiment for learning and do to some
    practicing on python3.
 
    code is free for non-commercial personal and educational use.
 
    Author:     Axel Werner [axel.werner.1973@gmail.com]
    Version:    2015-08-10
 
    Personal Comments and ToDos:
    =============================
    - i know this code is not very beautiful yet and is very cluttered. if i would
        reuse it, i should convert it into a class and do more re-factoring to make
        it friendlier to read and easy to maintain. 
 
"""
 
import shutil
import random
import time
 
consoleWidth, consoleHeight = shutil.get_terminal_size() # returns a integer tuple
 
leftDelimiter="["
rightDelimiter="]"
progressBarChar="="
progressBarFillerChar="."
 
limiterLastRandNum=0.5 # global 
 
def getProgressBar( progressPercentage=0.0, width=50 ):
    """ return progressBar as string ranging from float 0.0 to 1.0 ."""
 
    if progressPercentage > 1:
        print("ERROR: percentage must be between 0.0 and 1.0")
        print("         instead the percentage i received is:", progressPercentage)
        exit(1)
 
    percentageDisplayStr= str(int(progressPercentage*100))+"%"
 
    LengthOfpercentageDisplayStr=len(percentageDisplayStr)
    maxWidthOfpercentageDisplayStr=4 # '100%'
    numOfFillersNeeded=maxWidthOfpercentageDisplayStr-LengthOfpercentageDisplayStr
    percentageDisplayStr=percentageDisplayStr+numOfFillersNeeded*" "
 
    maxBarWidth=width-1-( len(leftDelimiter)+len(rightDelimiter)+len(percentageDisplayStr) )
    effectiveWidthOfProgressBar=int(maxBarWidth*progressPercentage)
    effectiveWidthOfFillerBar=int(maxBarWidth-effectiveWidthOfProgressBar)
 
 
    progressString=effectiveWidthOfProgressBar*progressBarChar
    fillerString=effectiveWidthOfFillerBar*progressBarFillerChar
    wholeBarStr=percentageDisplayStr+leftDelimiter+progressString+fillerString+rightDelimiter
    return wholeBarStr
 
def limitedRandGen():
    """ return a random Number between 0.0 and 1.0 within limited dynamic range of 3%. """
    global limiterLastRandNum
 
    newRandNum=random.random()
    delta=abs(limiterLastRandNum-newRandNum) # Betrag bestimmen (Vorzeichen ignorieren)
 
    while (delta*100) > 3.0 : # set limitation percentage here
        newRandNum=random.random()
        delta=abs(limiterLastRandNum-newRandNum) # Betrag bestimmen (Vorzeichen ignorieren)
 
    limiterLastRandNum=newRandNum
    return newRandNum
 
 
print("\n"*3)
print("DEMO1: run bargraph straight from 0 to 100%")
for progress in range(100+1):
    print( getProgressBar(progress/100.0, width=consoleWidth) )
 
 
print("\n"*3)
print("DEMO2: displaying 300 random percentages barGraphs on the same line at full width")
numOfFrames=300
for i in range(numOfFrames):
    print( "\r" + getProgressBar( progressPercentage=limitedRandGen(), width=consoleWidth ), end="" )
    time.sleep(50 / 1000) # delays for x seconds
 
 
print("\n"*3)
print("DEMO3: displaying 300 random percentages barGraphs on the same line at width=50")
numOfFrames=300
for i in range(numOfFrames):
    print( "\r" + getProgressBar( progressPercentage=limitedRandGen(), width=50 ), end="" )
    time.sleep(50 / 1000) # delays for x seconds
 
 
print("\n"*3)
print("consoleWidth =", consoleWidth)
 
input("press ENTER to continue...")

Axel Werner 2015-08-10 15:10

it-artikel/learning-python3-bargraph.py-small-program-to-generate-an-ascii-bargraph-of-any-width.txt · Last modified: 2015-08-10 15:18 by mail@awerner.myhome-server.de