--- layout: page status: publish published: true title: progress.py author: display_name: Tim login: admin email: blog@casualhacker.net url: http://www.casualhacker.net/ author_login: admin author_email: blog@casualhacker.net author_url: http://www.casualhacker.net/ wordpress_id: 388 wordpress_url: http://www.casualhacker.net/blog/?page_id=388 date: '2009-06-07 15:37:30 -0700' date_gmt: '2009-06-07 23:37:30 -0700' categories: - RtW tags: [] comments: [] ---

The progress module is a simple python module that easily lets you add progress display and time remaining estimates to any script. Just download it and copy it to a directory python looks for modules. I do this by having a python directory in my home directory, and adding

export PYTHONPATH=~/python

to my .bashrc, but there are other ways of doing the same thing.

The module provides 2 classes to simply add progress display to any application. Programs with more complex GUIs might still want to use it for the estimates of time remaining it provides.

Use the ProgressDisplay class if your work is done in a simple loop:

from progress import *
for i in ProgressDisplay(range(500)):
do_work()

If do_work() doesn't send any output to stdout you can use the following, which will cause a single status line to be printed and updated:

from progress import *
for i in ProgressDisplay(range(500), display=SINGLE_LINE):
do_work()

For more complex applications, you will probably need to manage a Progress object yourself:

from progress import *
progress = Progress(task_size)
for part in task:
do_work()
progress.increment()
progress.print_status_line()

If you have a more sophisticated GUI going on, you can still use Progress objects to give you a good estimate of time remaining:

from progress import *
progress = Progress(task_size)
for part in task:
do_work()
progress.increment()
update_gui(progress.percentage(), progress.time_remaining())