implementing terminal progressbar with data

This is the place for queries that don't fit in any of the other categories.

implementing terminal progressbar with data

Postby metulburr » Wed Mar 27, 2013 2:08 am

I have seen examples of progressbars. I might be retared now, but i cant seem to figure out how to implement the progress bar with the data that is pbar is suppose to represent. One of those "dah" moments!

The original code uses a timestamp as the start of the progress bar, and different points of time as the end of numerous progress bars. (8 hours, 24 hours, 48 hours, one month, etc.). My idea was to have a separate progress bar for each point in time of http://whyquit.com/whyquit/a_benefits_time_table.html.


This is the code i whipped up for a small example.
Code: Select all
import time
import sys

start = time.time()
end = start + 60

def progressbar_disp_full():
    display_char = '#'
    for num in range(101):
        spacer = int(33-int(num/3)) * ' '
        filler = int(num/3)         * display_char
        #time.sleep(.1)
        sys.stdout.write("\r[{0}{1}] {2}%".format(filler, spacer, num))
    print()
progressbar_disp_full()


So because i havent yet used a progress bar, and i feel like my brain is not working 100% lately: The range(101) gets the bar with from 0-100%, but how would you implement the start and end into the pbar. I have yet to see an example to view that hasnt just used time.sleep(1) to increment the pbar, so that might also be a part of my problem.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1116
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: implementing terminal progressbar with data

Postby setrofim » Wed Mar 27, 2013 6:32 am

You can adapt the following to your needs:
Code: Select all
import sys
import time


class SimpleProgressBar(object):

    def __init__(self, max_value, size=80):
        self.max_value = max_value
        self.size = size
        self.value = 0

    def draw(self):
        fill_size = self.size * self.value // self.max_value
        if fill_size > self.size - 2:
            fill_size = self.size - 2
        blank_size = self.size - fill_size -2
        bar = ''.join(['[', '=' * fill_size,
                            ' ' * blank_size, ']'])

        sys.stdout.write('\b' * len(bar))
        sys.stdout.write(bar)
        sys.stdout.flush()

    def step(self, step_size=1):
        self.value += step_size
        self.draw()

    def end(self):
        sys.stdout.write('\n')
        sys.stdout.flush()


if __name__ == '__main__':
    pbar = SimpleProgressBar(10)
    pbar.draw()
    for i in xrange(10):
        time.sleep(1)
        pbar.step()
    pbar.end()
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests