CMS 3D CMS Logo

Public Member Functions | Public Attributes | Static Public Attributes | Properties | Private Member Functions | Private Attributes | Static Private Attributes

progressbar::ProgressBar Class Reference

List of all members.

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

Detailed Description

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.


Constructor & Destructor Documentation

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 


Member Function Documentation

def progressbar::ProgressBar::__call__ (   self,
  iterable 
)

Definition at line 247 of file progressbar.py.

00248                                 :
00249         'Use a ProgressBar to iterate through an iterable'
00250 
00251         try:
00252             self.maxval = len(iterable)
00253         except:
00254             if self.maxval is None:
00255                 self.maxval = UnknownLength
00256 
00257         self.__iterable = iter(iterable)
00258         return self
00259 

def progressbar::ProgressBar::__iter__ (   self)

Definition at line 260 of file progressbar.py.

00261                       :
00262         return self
00263 

def progressbar::ProgressBar::__next__ (   self)

Definition at line 264 of file progressbar.py.

00265                       :
00266         try:
00267             value = next(self.__iterable)
00268             if self.start_time is None: self.start()
00269             else: self.update(self.currval + 1)
00270             return value
00271         except StopIteration:
00272             self.finish()
00273             raise
00274 

def progressbar::ProgressBar::_env_size (   self) [private]

Definition at line 280 of file progressbar.py.

00281                        :
00282         'Tries to find the term_width from the environment.'
00283 
00284         return int(os.environ.get('COLUMNS', self._DEFAULT_TERMSIZE)) - 1
00285 

def progressbar::ProgressBar::_format_line (   self) [private]

Definition at line 327 of file progressbar.py.

00328                           :
00329         'Joins the widgets and justifies the line'
00330 
00331         widgets = ''.join(self._format_widgets())
00332 
00333         if self.left_justify: return widgets.ljust(self.term_width)
00334         else: return widgets.rjust(self.term_width)
00335 

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.

00287                                                      :
00288         'Tries to catch resize signals sent from the terminal.'
00289 
00290         h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2]
00291         self.term_width = w
00292 

def progressbar::ProgressBar::_need_update (   self) [private]

Definition at line 336 of file progressbar.py.

00337                           :
00338         'Returns whether the ProgressBar should redraw the line.'
00339         if self.currval >= self.next_update or self.finished: return True
00340 
00341         delta = time.time() - self.last_update_time
00342         return self._time_sensitive and delta > self.poll
00343 

def progressbar::ProgressBar::_update_widgets (   self) [private]

Definition at line 344 of file progressbar.py.

00345                              :
00346         'Checks all widgets for the time sensitive bit'
00347 
00348         self._time_sensitive = any(getattr(w, 'TIME_SENSITIVE', False)
00349             for w in self.widgets)
00350 

def progressbar::ProgressBar::finish (   self)

Definition at line 403 of file progressbar.py.

00404                     :
00405         'Puts the ProgressBar bar in the finished state.'
00406 
00407         self.finished = True
00408         self.update(self.maxval)
00409         self.fd.write('\n')
00410         if self.signal_set:
            signal.signal(signal.SIGWINCH, signal.SIG_DFL)
def progressbar::ProgressBar::percentage (   self)

Definition at line 293 of file progressbar.py.

00294                         :
00295         'Returns the progress as a percentage.'
00296         return self.currval * 100.0 / self.maxval

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 


Member Data Documentation

Definition at line 215 of file progressbar.py.

tuple progressbar::ProgressBar::__slots__ [static, private]
Initial value:
('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.


Property Documentation

progressbar::ProgressBar::percent = property(percentage) [static]

Definition at line 297 of file progressbar.py.