Cat
progressbar.py
Go to the documentation of this file.
1 import sys
2 import time
3 
5  """ProgressBar class holds the options of the progress bar.
6  The options are:
7  start State from which start the progress. For example, if start is
8  5 and the end is 10, the progress of this state is 50%
9  end State in which the progress has terminated.
10  width --
11  fill String to use for "filled" used to represent the progress
12  blank String to use for "filled" used to represent remaining space.
13  format Format
14  incremental
15  """
16  def __init__(self, process=" ", start=0, end=10, width=12, fill='=', blank='.', format='running %(process)s [%(fill)s%(blank)s] %(progress)s%%', incremental=True):
17  super(ProgressBar, self).__init__()
18 
19  self.start = start
20  self.end = end
21  self.width = width
22  self.fill = fill
23  self.blank = blank
24  self.format = format
25  self.incremental = incremental
26  self.step = 100 / float(width) #fix
27  self.process = process
28  self.reset()
29 
30  def __add__(self, increment):
31  increment = self._get_progress(increment)
32  if 100 > self.progress + increment:
33  self.progress += increment
34  else:
35  self.progress = 100
36  return self
37 
38  def __str__(self):
39  progressed = int(self.progress / self.step) #fix
40  fill = progressed * self.fill
41  proc = self.process
42  blank = (self.width - progressed) * self.blank
43  return self.format % {'process': proc, 'fill': fill, 'blank': blank, 'progress': int(self.progress)}
44 
45  __repr__ = __str__
46 
47  def _get_progress(self, increment):
48  return float(increment * 100) / self.end
49 
50  def set(self, value):
51  value = self._get_progress(value)
52  if 100 > value:
53  self.progress = value
54  else:
55  self.progress = 100
56  return self
57 
58  def reset(self):
59  """Resets the current progress to the start point"""
60  self.progress = self._get_progress(self.start)
61  return self
62 
63 
65  """Extends ProgressBar to allow you to use it straighforward on a script.
66  Accepts an extra keyword argument named `stdout` (by default use sys.stdout)
67  and may be any file-object to which send the progress status.
68  """
69  def __init__(self, *args, **kwargs):
70  super(AnimatedProgressBar, self).__init__(*args, **kwargs)
71  self.stdout = kwargs.get('stdout', sys.stdout)
72  self.last_indicator = ''
73 
74  def show_progress(self):
75  indicator = str(self)
76  if indicator == self.last_indicator:
77  return
78  if hasattr(self.stdout, 'isatty') and self.stdout.isatty():
79  self.stdout.write('\r')
80  else:
81  self.stdout.write('\n')
82  self.stdout.write(str(self))
83  self.stdout.flush()
84  self.last_indicator = indicator
85 
86 if __name__ == '__main__':
87  p = AnimatedProgressBar(end=100, width=80)
88 
89  while True:
90  p + 5
91  p.show_progress()
92  time.sleep(0.1)
93  if p.progress == 100:
94  break
95  print #new line
def _get_progress(self, increment)
Definition: progressbar.py:47
def __init__(self, args, kwargs)
Definition: progressbar.py:69
def set(self, value)
Definition: progressbar.py:50
Definition: object.py:1
def __add__(self, increment)
Definition: progressbar.py:30
def __init__(self, process=" ", start=0, end=10, width=12, fill='=', blank='.', format='running %(process) s [%(fill) s%(blank) s] %(progress) s%%', incremental=True)
Definition: progressbar.py:16