Public Member Functions | Public Attributes | Private Member Functions | List of all members
progressbar.ProgressBar Class Reference
Inheritance diagram for progressbar.ProgressBar:

Public Member Functions

def __init__ (self, maxval=100, widgets=default_widgets, term_width=None, fd=sys.stderr)
 
def handle_resize (self, signum, frame)
 
def percentage (self)
 
def update (self, value)
 
def start (self)
 
def finish (self)
 

Public Attributes

 maxval
 
 widgets
 
 fd
 
 signal_set
 
 term_width
 
 currval
 
 finished
 
 prev_percentage
 
 start_time
 
 seconds_elapsed
 

Private Member Functions

def _format_widgets (self)
 
def _format_line (self)
 
def _need_update (self)
 

Detailed Description

This is the ProgressBar class, it updates and prints the bar.

The term_width parameter may be an integer. Or None, in which case
it will try to guess it, if it fails it will default to 80 columns.

The simple use is like this:
>>> pbar = ProgressBar().start()
>>> for i in xrange(100):
...    # do something
...    pbar.update(i+1)
...
>>> pbar.finish()

But anything you want to do is possible (well, almost anything).
You can supply different widgets of any type in any order. And you
can even write your own widgets! There are many widgets already
shipped and you should experiment with them.

When implementing a widget update method you may access any
attribute or function of the ProgressBar object calling the
widget's update method. The most important attributes you would
like to access are:
- currval: current value of the progress, 0 <= currval <= maxval
- maxval: maximum (and final) value of the progress
- finished: True if the bar is have finished (reached 100%), False o/w
- start_time: first time update() method of ProgressBar was called
- seconds_elapsed: seconds elapsed since start_time
- percentage(): percentage of the progress (this is a method)

Definition at line 194 of file progressbar.py.

Constructor & Destructor Documentation

def progressbar.ProgressBar.__init__ (   self,
  maxval = 100,
  widgets = default_widgets,
  term_width = None,
  fd = sys.stderr 
)

Definition at line 225 of file progressbar.py.

225  fd=sys.stderr):
226  assert maxval > 0
227  self.maxval = maxval
228  self.widgets = widgets
229  self.fd = fd
230  self.signal_set = False
231  if term_width is None:
232  try:
233  self.handle_resize(None,None)
234  signal.signal(signal.SIGWINCH, self.handle_resize)
235  self.signal_set = True
236  except:
237  self.term_width = 79
238  else:
239  self.term_width = term_width
240 
241  self.currval = 0
242  self.finished = False
243  self.prev_percentage = -1
244  self.start_time = None
246 
def handle_resize(self, signum, frame)
Definition: progressbar.py:247

Member Function Documentation

def progressbar.ProgressBar._format_line (   self)
private

Definition at line 276 of file progressbar.py.

276  def _format_line(self):
277  return ''.join(self._format_widgets()).ljust(self.term_width)
278 
def progressbar.ProgressBar._format_widgets (   self)
private

Definition at line 255 of file progressbar.py.

255  def _format_widgets(self):
256  r = []
257  hfill_inds = []
258  num_hfill = 0
259  currwidth = 0
260  for i, w in enumerate(self.widgets):
261  if isinstance(w, ProgressBarWidgetHFill):
262  r.append(w)
263  hfill_inds.append(i)
264  num_hfill += 1
265  elif isinstance(w, str):
266  r.append(w)
267  currwidth += len(w)
268  else:
269  weval = w.update(self)
270  currwidth += len(weval)
271  r.append(weval)
272  for iw in hfill_inds:
273  r[iw] = r[iw].update(self, (self.term_width-currwidth)//num_hfill)
274  return r
275 
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def update(self, value)
Definition: progressbar.py:282
def progressbar.ProgressBar._need_update (   self)
private

Definition at line 279 of file progressbar.py.

def progressbar.ProgressBar.finish (   self)
Used to tell the progress is finished.

Definition at line 312 of file progressbar.py.

312  def finish(self):
313  """Used to tell the progress is finished."""
314  self.update(self.maxval)
315  if self.signal_set:
316  signal.signal(signal.SIGWINCH, signal.SIG_DFL)
317 
318 
319 
320 
321 
322 
def update(self, value)
Definition: progressbar.py:282
def progressbar.ProgressBar.handle_resize (   self,
  signum,
  frame 
)

Definition at line 247 of file progressbar.py.

247  def handle_resize(self, signum, frame):
248  h,w=array('h', ioctl(self.fd,termios.TIOCGWINSZ,'\0'*8))[:2]
249  self.term_width = w
250 
def handle_resize(self, signum, frame)
Definition: progressbar.py:247
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
def progressbar.ProgressBar.percentage (   self)

Definition at line 251 of file progressbar.py.

251  def percentage(self):
252  "Returns the percentage of the progress."
253  return self.currval*100.0 / self.maxval
254 
def progressbar.ProgressBar.start (   self)
Start measuring time, and prints the bar at 0%.

It returns self so you can use it like this:
>>> pbar = ProgressBar().start()
>>> for i in xrange(100):
...    # do something
...    pbar.update(i+1)
...
>>> pbar.finish()

Definition at line 298 of file progressbar.py.

298  def start(self):
299  """Start measuring time, and prints the bar at 0%.
300 
301  It returns self so you can use it like this:
302  >>> pbar = ProgressBar().start()
303  >>> for i in xrange(100):
304  ... # do something
305  ... pbar.update(i+1)
306  ...
307  >>> pbar.finish()
308  """
309  self.update(0)
310  return self
311 
def update(self, value)
Definition: progressbar.py:282
def progressbar.ProgressBar.update (   self,
  value 
)

Definition at line 282 of file progressbar.py.

282  def update(self, value):
283  "Updates the progress bar to a new value."
284  assert 0 <= value <= self.maxval
285  self.currval = value
286  if not self._need_update() or self.finished:
287  return
288  if not self.start_time:
289  self.start_time = time.time()
290  self.seconds_elapsed = time.time() - self.start_time
291  self.prev_percentage = self.percentage()
292  if value != self.maxval:
293  self.fd.write(self._format_line() + '\r')
294  else:
295  self.finished = True
296  self.fd.write(self._format_line() + '\n')
297 
def update(self, value)
Definition: progressbar.py:282

Member Data Documentation

progressbar.ProgressBar.currval

Definition at line 241 of file progressbar.py.

progressbar.ProgressBar.fd

Definition at line 229 of file progressbar.py.

progressbar.ProgressBar.finished

Definition at line 242 of file progressbar.py.

progressbar.ProgressBar.maxval

Definition at line 227 of file progressbar.py.

progressbar.ProgressBar.prev_percentage

Definition at line 243 of file progressbar.py.

progressbar.ProgressBar.seconds_elapsed

Definition at line 245 of file progressbar.py.

progressbar.ProgressBar.signal_set

Definition at line 230 of file progressbar.py.

progressbar.ProgressBar.start_time

Definition at line 244 of file progressbar.py.

progressbar.ProgressBar.term_width

Definition at line 237 of file progressbar.py.

progressbar.ProgressBar.widgets

Definition at line 228 of file progressbar.py.


The documentation for this class was generated from the following file: