22 """Text progressbar library for python. 24 This library provides a text mode progressbar. This is tipically used 25 to display the progress of a long running operation, providing a 26 visual clue that processing is underway. 28 The ProgressBar class manages the progress, and the format of the line 29 is given by a number of widgets. A widget is an object that may 30 display diferently depending on the state of the progress. There are 31 three types of widget: 32 - a string, which always shows itself; 33 - a ProgressBarWidget, which may return a diferent value every time 34 it's update method is called; and 35 - a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it 36 expands to fill the remaining width of the line. 38 The progressbar module is very easy to use, yet very powerful. And 39 automatically supports features like auto-resizing when available. 41 from __future__
import print_function
43 from builtins
import range
44 from builtins
import object
45 __author__ =
"Nilton Volpato" 46 __author_email__ =
"first-name dot last-name @ gmail.com" 47 __date__ =
"2006-05-07" 61 from array
import array
63 from fcntl
import ioctl
70 """This is an element of ProgressBar formatting. 72 The ProgressBar object will call it's update value when an update 73 is needed. It's size may change between call, but the results will 74 not be good if the size changes drastically and repeatedly. 77 """Returns the string representing the widget. 79 The parameter pbar is a reference to the calling ProgressBar, 80 where one can access attributes of the class for knowing how 81 the update must be made. 83 At least this function must be overriden.""" 86 class ProgressBarWidgetHFill(object):
87 """This is a variable width element of ProgressBar formatting. 89 The ProgressBar object will call it's update value, informing the 90 width this object must the made. This is like TeX \\hfill, it will 91 expand to fill the line. You can use more than one in the same 92 line, and they will all have the same width, and together will 96 """Returns the string representing the widget. 98 The parameter pbar is a reference to the calling ProgressBar, 99 where one can access attributes of the class for knowing how 100 the update must be made. The parameter width is the total 101 horizontal width the widget must have. 103 At least this function must be overriden.""" 107 class ETA(ProgressBarWidget):
108 "Widget for the Estimated Time of Arrival" 110 return time.strftime(
'%H:%M:%S', time.gmtime(seconds))
112 if pbar.currval == 0:
113 return 'ETA: --:--:--' 115 return 'Time: %s' % self.
format_time(pbar.seconds_elapsed)
117 elapsed = pbar.seconds_elapsed
118 eta = elapsed * pbar.maxval / pbar.currval - elapsed
122 "Widget for showing the transfer speed (useful for file transfers)." 125 self.
units = [
'B',
'K',
'M',
'G',
'T',
'P']
127 if pbar.seconds_elapsed < 2e-6:
130 bps =
float(pbar.currval) / pbar.seconds_elapsed
136 return self.
fmt % (spd, u+
'/s')
139 "A rotating marker for filling the bar of progress." 150 "Just the percentage done." 152 return '%3d%%' % pbar.percentage()
155 "Just the percentage done." 157 return '%3d' % pbar.currval
160 "Just the percentage done." 162 return '%3d' % pbar.maxval
165 "The bar of progress. It will strech to fill the line." 166 def __init__(self, marker='#', left='|', right='|'):
171 if isinstance(self.
marker, str):
174 return self.marker.update(pbar)
176 percent = pbar.percentage()
177 cwidth = width - len(self.
left) - len(self.
right)
178 marked_width =
int(percent * cwidth // 100)
180 bar = (self.
left + (m*marked_width).ljust(cwidth) + self.
right)
184 "The reverse bar of progress, or bar of regress. :)" 186 percent = pbar.percentage()
187 cwidth = width - len(self.
left) - len(self.
right)
188 marked_width =
int(percent * cwidth // 100)
190 bar = (self.
left + (m*marked_width).rjust(cwidth) + self.
right)
195 """This is the ProgressBar class, it updates and prints the bar. 197 The term_width parameter may be an integer. Or None, in which case 198 it will try to guess it, if it fails it will default to 80 columns. 200 The simple use is like this: 201 >>> pbar = ProgressBar().start() 202 >>> for i in xrange(100): 208 But anything you want to do is possible (well, almost anything). 209 You can supply different widgets of any type in any order. And you 210 can even write your own widgets! There are many widgets already 211 shipped and you should experiment with them. 213 When implementing a widget update method you may access any 214 attribute or function of the ProgressBar object calling the 215 widget's update method. The most important attributes you would 217 - currval: current value of the progress, 0 <= currval <= maxval 218 - maxval: maximum (and final) value of the progress 219 - finished: True if the bar is have finished (reached 100%), False o/w 220 - start_time: first time update() method of ProgressBar was called 221 - seconds_elapsed: seconds elapsed since start_time 222 - percentage(): percentage of the progress (this is a method) 224 def __init__(self, maxval=100, widgets=default_widgets, term_width=None,
231 if term_width
is None:
248 h,w=
array(
'h', ioctl(self.
fd,termios.TIOCGWINSZ,
'\0'*8))[:2]
252 "Returns the percentage of the progress." 261 if isinstance(w, ProgressBarWidgetHFill):
265 elif isinstance(w, str):
269 weval = w.update(self)
270 currwidth += len(weval)
272 for iw
in hfill_inds:
283 "Updates the progress bar to a new value." 284 assert 0 <= value <= self.
maxval 299 """Start measuring time, and prints the bar at 0%. 301 It returns self so you can use it like this: 302 >>> pbar = ProgressBar().start() 303 >>> for i in xrange(100): 313 """Used to tell the progress is finished.""" 316 signal.signal(signal.SIGWINCH, signal.SIG_DFL)
323 if __name__==
'__main__':
329 pbar =
ProgressBar(widgets=widgets, maxval=10000000).start()
330 for i
in range(1000000):
338 "It's bigger between 45 and 80 percent" 339 def update(self, pbar):
340 if 45 < pbar.percentage() < 80:
341 return 'Bigger Now ' + FileTransferSpeed.update(self,pbar)
343 return FileTransferSpeed.update(self,pbar)
345 widgets = [CrazyFileTransferSpeed(),
' <<<',
Bar(),
'>>> ',
Percentage(),
' ',
ETA()]
346 pbar =
ProgressBar(widgets=widgets, maxval=10000000)
349 for i
in range(2000000):
357 pbar =
ProgressBar(widgets=widgets, maxval=10000000).start()
358 for i
in range(1000000):
366 Bar(marker=
'0',left=
'[',right=
']'),
370 for i
in range(100,500+1,50):
def __init__(self, maxval=100, widgets=default_widgets, term_width=None, fd=sys.stderr)
def update(self, pbar, width)
def handle_resize(self, signum, frame)
def __init__(self, marker='#', left='|', right='|')
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
def _format_marker(self, pbar)
auto array(Array const &a)
Returns a manipulator which will print the specified array.
def update(self, pbar, width)
def format_time(self, seconds)
def __init__(self, markers='|/-\\')
def _format_widgets(self)