~jtv/corpusfiltergraph/cross-python

« back to all changes in this revision

Viewing changes to trunk/lib/corpusfg/progress_bar.py

  • Committer: tahoar
  • Date: 2012-05-02 15:46:23 UTC
  • Revision ID: svn-v4:bc069b21-dff4-4e29-a776-06a4e04bad4e::266
new layout. need to update code to use the new layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# -*- coding: utf8 -*-
 
3
 
 
4
# A Python Library to create a Progress Bar.
 
5
# Copyright (C) 2008  BJ Dierkes <wdierkes@5dollarwhitebox.org>
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
#
 
20
#
 
21
# This class is an improvement from the original found at:
 
22
#
 
23
#   http://code.activestate.com/recipes/168639/
 
24
#
 
25
# Precision Translation Tools downloaded from: http://www.5dollarwhitebox.org/drupal/node/65
 
26
#       (moved ProgressBar._init_() to ProgressBar.reset() for reuse.)
 
27
 
 
28
''' Modifications for Corpus Filtergraph v4.0
 
29
Copyright © 2009-2012 Thomas A. Hoar
 
30
 
 
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.
 
35
 
 
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.
 
40
 
 
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/.'''
 
43
 
 
44
 
 
45
import sys, os
 
46
 
 
47
class ProgressBar:
 
48
 
 
49
        char = '#'
 
50
        mode = 'fixed'
 
51
        bar = '\r'
 
52
        min = 0
 
53
        max = 1
 
54
        span = 1
 
55
        width = 1
 
56
        amount = 0
 
57
 
 
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']:
 
62
                        self.mode = 'fixed'
 
63
 
 
64
                self.bar = '\r'
 
65
                self.min = min_value
 
66
                self.max = max_value
 
67
                self.span = max_value - min_value
 
68
                self.width = width
 
69
                self.amount = 0    # When amount == max, we are 100% done 
 
70
                self.update_amount(0) 
 
71
 
 
72
        def increment_amount( self, add_amount=1 ):
 
73
                '''
 
74
                Increment self.amount by 'add_ammount' or default to incrementing
 
75
                by 1, and then rebuild the bar string. 
 
76
                '''
 
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
 
81
                self.build_bar()
 
82
 
 
83
        def update_amount( self, new_amount=None ):
 
84
                '''
 
85
                Update self.amount with 'new_amount', and then rebuild the bar 
 
86
                string.
 
87
                '''
 
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
 
92
                self.build_bar()
 
93
 
 
94
        def build_bar( self ):
 
95
                '''
 
96
                Figure new percent complete, and rebuild the bar string base on 
 
97
                self.amount.
 
98
                '''
 
99
                diff = float(self.amount - self.min)
 
100
                self.percent_done = int(round((diff / float(self.span)) * 100.0))
 
101
 
 
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))
 
105
 
 
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
 
110
                else:
 
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)
 
114
 
 
115
                percent_str = str(self.percent_done) + '%'
 
116
                self.bar = '   [ %s ] %s\r'%(self.bar,percent_str)
 
117
 
 
118
        def __str__( self ):
 
119
                sys.stdout.flush()
 
120
                return str( self.bar )
 
121
 
 
122
def main():
 
123
        print
 
124
        limit = 1000000
 
125
 
 
126
        print 'Example 1: Fixed Bar'
 
127
        prog = ProgressBar(0, limit, 75, mode='fixed')
 
128
        oldprog = str(prog)
 
129
        for i in xrange(limit+1):
 
130
                prog.update_amount(i)
 
131
                if oldprog != str(prog):
 
132
                        print prog, '\r',
 
133
                        sys.stdout.flush()
 
134
                        oldprog=str(prog)
 
135
 
 
136
        print '\n\n'
 
137
 
 
138
        print 'Example 2: Dynamic Bar'
 
139
        prog = ProgressBar(0, limit, 75, mode='dynamic', char='-')
 
140
        oldprog = str(prog)
 
141
        for i in xrange(limit+1):
 
142
                prog.increment_amount()
 
143
                if oldprog != str(prog):
 
144
                        print prog, '\r',
 
145
                        sys.stdout.flush()
 
146
                        oldprog=str(prog)
 
147
 
 
148
        print '\n\n'
 
149
 
 
150
if __name__ == '__main__':
 
151
        main()
 
152
 
 
153
'''
 
154
Now add it to your app:
 
155
 
 
156
<code>
 
157
from progress_bar import ProgressBar
 
158
import sys
 
159
 
 
160
count = 0
 
161
total = 100000
 
162
 
 
163
prog = ProgressBar(count, total, 75, mode='fixed', char='#')
 
164
while count <= total:
 
165
        count += 1
 
166
        prog.increment_amount()
 
167
        print prog, '\r',
 
168
        sys.stdout.flush()
 
169
print
 
170
'''