![]() |
![]() |
Public Member Functions | |
def | __call__ |
def | __init__ |
def | __iter__ |
def | __next__ |
def | finish |
def | percentage |
def | start |
def | update |
Public Attributes | |
currval | |
fd | |
finished | |
last_update_time | |
left_justify | |
maxval | |
next_update | |
num_intervals | |
poll | |
seconds_elapsed | |
signal_set | |
start_time | |
term_width | |
update_interval | |
widgets | |
Static Public Attributes | |
next = __next__ | |
Properties | |
percent = property(percentage) | |
Private Member Functions | |
def | _env_size |
def | _format_line |
def | _format_widgets |
def | _handle_resize |
def | _need_update |
def | _update_widgets |
Private Attributes | |
__iterable | |
_time_sensitive | |
Static Private Attributes | |
tuple | __slots__ |
int | _DEFAULT_MAXVAL = 100 |
int | _DEFAULT_TERMSIZE = 80 |
The ProgressBar class which updates and prints the bar. A common way of using it is like: >>> pbar = ProgressBar().start() >>> for i in range(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish() You can also use a ProgressBar as an iterator: >>> progress = ProgressBar() >>> for i in progress(some_iterable): ... # do something ... Since the progress bar is incredibly customizable you can specify different widgets of any type in any order. You can even write your own widgets! However, since there are already a good number of widgets you should probably play around with them before moving on to create your own widgets. The term_width parameter represents the current terminal width. If the parameter is set to an integer then the progress bar will use that, otherwise it will attempt to determine the terminal width falling back to 80 columns if the width cannot be determined. When implementing a widget's update method you are passed a reference to the current progress bar. As a result, you have access to the ProgressBar's methods and attributes. Although there is nothing preventing you from changing the ProgressBar you should treat it as read only. Useful methods and attributes include (Public API): - currval: current progress (0 <= currval <= maxval) - maxval: maximum (and final) value - finished: True if the bar has finished (reached 100%) - start_time: the time when start() method of ProgressBar was called - seconds_elapsed: seconds elapsed since start_time and last call to update - percentage(): progress in percent [0..100]
Definition at line 163 of file progressbar.py.
def progressbar::ProgressBar::__init__ | ( | self, | |
maxval = None , |
|||
widgets = None , |
|||
term_width = None , |
|||
poll = 1 , |
|||
left_justify = True , |
|||
fd = sys.stderr |
|||
) |
Initializes a progress bar with sane defaults
Definition at line 215 of file progressbar.py.
00217 : 00218 '''Initializes a progress bar with sane defaults''' 00219 00220 self.maxval = maxval 00221 self.widgets = widgets 00222 self.fd = fd 00223 self.left_justify = left_justify 00224 00225 self.signal_set = False 00226 if term_width is not None: 00227 self.term_width = term_width 00228 else: 00229 try: 00230 self._handle_resize() 00231 signal.signal(signal.SIGWINCH, self._handle_resize) 00232 self.signal_set = True 00233 except (SystemExit, KeyboardInterrupt): raise 00234 except: 00235 self.term_width = self._env_size() 00236 00237 self.__iterable = None 00238 self._update_widgets() 00239 self.currval = 0 00240 self.finished = False 00241 self.last_update_time = None 00242 self.poll = poll 00243 self.seconds_elapsed = 0 00244 self.start_time = None 00245 self.update_interval = 1 00246
def progressbar::ProgressBar::__call__ | ( | self, | |
iterable | |||
) |
Definition at line 247 of file progressbar.py.
def progressbar::ProgressBar::__iter__ | ( | self | ) |
Definition at line 260 of file progressbar.py.
def progressbar::ProgressBar::__next__ | ( | self | ) |
Definition at line 264 of file progressbar.py.
def progressbar::ProgressBar::_env_size | ( | self | ) | [private] |
Definition at line 280 of file progressbar.py.
def progressbar::ProgressBar::_format_line | ( | self | ) | [private] |
Definition at line 327 of file progressbar.py.
def progressbar::ProgressBar::_format_widgets | ( | self | ) | [private] |
Definition at line 300 of file progressbar.py.
00301 : 00302 result = [] 00303 expanding = [] 00304 width = self.term_width 00305 00306 for index, widget in enumerate(self.widgets): 00307 if isinstance(widget, WidgetHFill): 00308 result.append(widget) 00309 expanding.insert(0, index) 00310 else: 00311 widget = format_updatable(widget, self) 00312 result.append(widget) 00313 width -= len(widget) 00314 00315 count = len(expanding) 00316 while count: 00317 portion = max(int(math.ceil(width * 1. / count)), 0) 00318 index = expanding.pop() 00319 count -= 1 00320 00321 widget = result[index].update(self, portion) 00322 width -= len(widget) 00323 result[index] = widget 00324 00325 return result 00326
def progressbar::ProgressBar::_handle_resize | ( | self, | |
signum = None , |
|||
frame = None |
|||
) | [private] |
Definition at line 286 of file progressbar.py.
def progressbar::ProgressBar::_need_update | ( | self | ) | [private] |
Definition at line 336 of file progressbar.py.
def progressbar::ProgressBar::_update_widgets | ( | self | ) | [private] |
Definition at line 344 of file progressbar.py.
def progressbar::ProgressBar::finish | ( | self | ) |
Definition at line 403 of file progressbar.py.
def progressbar::ProgressBar::percentage | ( | self | ) |
Definition at line 293 of file progressbar.py.
def progressbar::ProgressBar::start | ( | self | ) |
Starts measuring time, and prints the bar at 0%. It returns self so you can use it like this: >>> pbar = ProgressBar().start() >>> for i in range(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish()
Definition at line 374 of file progressbar.py.
00375 : 00376 '''Starts measuring time, and prints the bar at 0%. 00377 00378 It returns self so you can use it like this: 00379 >>> pbar = ProgressBar().start() 00380 >>> for i in range(100): 00381 ... # do something 00382 ... pbar.update(i+1) 00383 ... 00384 >>> pbar.finish() 00385 ''' 00386 00387 if self.maxval is None: 00388 self.maxval = self._DEFAULT_MAXVAL 00389 00390 self.num_intervals = max(100, self.term_width) 00391 self.next_update = 0 00392 00393 if self.maxval is not UnknownLength: 00394 if self.maxval < 0: raise ValueError('Value out of range') 00395 self.update_interval = self.maxval / self.num_intervals 00396 00397 00398 self.start_time = self.last_update_time = time.time() 00399 self.update(0) 00400 00401 return self 00402
def progressbar::ProgressBar::update | ( | self, | |
value = None |
|||
) |
Definition at line 351 of file progressbar.py.
00352 : 00353 'Updates the ProgressBar to a new value.' 00354 00355 if value is not None and value is not UnknownLength: 00356 if (self.maxval is not UnknownLength 00357 and not 0 <= value <= self.maxval): 00358 00359 raise ValueError('Value out of range') 00360 00361 self.currval = value 00362 00363 00364 if not self._need_update(): return 00365 if self.start_time is None: 00366 raise RuntimeError('You must call "start" before calling "update"') 00367 00368 now = time.time() 00369 self.seconds_elapsed = now - self.start_time 00370 self.next_update = self.currval + self.update_interval 00371 self.fd.write(self._format_line() + '\r') 00372 self.last_update_time = now 00373
progressbar::ProgressBar::__iterable [private] |
Definition at line 215 of file progressbar.py.
tuple progressbar::ProgressBar::__slots__ [static, private] |
('currval', 'fd', 'finished', 'last_update_time', 'left_justify', 'maxval', 'next_update', 'num_intervals', 'poll', 'seconds_elapsed', 'signal_set', 'start_time', 'term_width', 'update_interval', 'widgets', '_time_sensitive', '__iterable')
Definition at line 206 of file progressbar.py.
int progressbar::ProgressBar::_DEFAULT_MAXVAL = 100 [static, private] |
Definition at line 212 of file progressbar.py.
int progressbar::ProgressBar::_DEFAULT_TERMSIZE = 80 [static, private] |
Definition at line 213 of file progressbar.py.
Definition at line 344 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
progressbar::ProgressBar::next = __next__ [static] |
Definition at line 277 of file progressbar.py.
Definition at line 351 of file progressbar.py.
Definition at line 383 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
Definition at line 215 of file progressbar.py.
progressbar::ProgressBar::percent = property(percentage) [static] |
Definition at line 297 of file progressbar.py.