4
# A Python Library to create a Progress Bar.
5
# Copyright (C) 2008 BJ Dierkes <wdierkes@5dollarwhitebox.org>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
# This class is an improvement from the original found at:
23
# http://code.activestate.com/recipes/168639/
25
# Precision Translation Tools downloaded from: http://www.5dollarwhitebox.org/drupal/node/65
26
# (moved ProgressBar._init_() to ProgressBar.reset() for reuse.)
28
''' Modifications for Corpus Filtergraph v4.0
29
Copyright © 2009-2012 Thomas A. Hoar
31
This program is free software: you can redistribute it and/or modify
32
it under the terms of the GNU Lesser General Public License as published by
33
the Free Software Foundation, either version 3 of the License, or
34
(at your option) any later version.
36
This program is distributed in the hope that it will be useful,
37
but WITHOUT ANY WARRANTY; without even the implied warranty of
38
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39
GNU Lesser General Public License for more details.
41
You should have received a copy of the GNU Lesser General Public License
42
along with this program. If not, see http://www.gnu.org/licenses/.'''
58
def reset( self, min_value=0, max_value=100, width=75, **kwargs ):
59
self.char = kwargs.get('char', '#')
60
self.mode = kwargs.get('mode', 'dynamic') # fixed or dynamic
61
if not self.mode in ['fixed', 'dynamic']:
67
self.span = max_value - min_value
69
self.amount = 0 # When amount == max, we are 100% done
72
def increment_amount( self, add_amount=1 ):
74
Increment self.amount by 'add_ammount' or default to incrementing
75
by 1, and then rebuild the bar string.
77
new_amount = self.amount + add_amount
78
if new_amount < self.min: new_amount = self.min
79
if new_amount > self.max: new_amount = self.max
80
self.amount = new_amount
83
def update_amount( self, new_amount=None ):
85
Update self.amount with 'new_amount', and then rebuild the bar
88
if not new_amount: new_amount = self.amount
89
if new_amount < self.min: new_amount = self.min
90
if new_amount > self.max: new_amount = self.max
91
self.amount = new_amount
94
def build_bar( self ):
96
Figure new percent complete, and rebuild the bar string base on
99
diff = float(self.amount - self.min)
100
self.percent_done = int(round((diff / float(self.span)) * 100.0))
102
# figure the proper number of 'character' make up the bar
103
all_full = self.width - 7
104
num_hashes = int(round((self.percent_done * all_full) / 100))
106
if self.mode == 'dynamic':
107
# build a progress bar with self.char (to create a dynamic bar
108
# where the percent string moves along with the bar progress.
109
self.bar = self.char * num_hashes
111
# build a progress bar with self.char and spaces (to create a
112
# fixe bar (the percent string doesn't move)
113
self.bar = self.char * num_hashes + ' ' * (all_full-num_hashes)
115
percent_str = str(self.percent_done) + '%'
116
self.bar = ' [ %s ] %s\r'%(self.bar,percent_str)
120
return str( self.bar )
126
print 'Example 1: Fixed Bar'
127
prog = ProgressBar(0, limit, 75, mode='fixed')
129
for i in xrange(limit+1):
130
prog.update_amount(i)
131
if oldprog != str(prog):
138
print 'Example 2: Dynamic Bar'
139
prog = ProgressBar(0, limit, 75, mode='dynamic', char='-')
141
for i in xrange(limit+1):
142
prog.increment_amount()
143
if oldprog != str(prog):
150
if __name__ == '__main__':
154
Now add it to your app:
157
from progress_bar import ProgressBar
163
prog = ProgressBar(count, total, 75, mode='fixed', char='#')
164
while count <= total:
166
prog.increment_amount()