Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ To use them, just call ``next`` to advance and ``finish`` to finish:
bar.next()
bar.finish()

or use any bar of this class as a context manager:

.. code-block:: python

from progress.bar import Bar

with Bar('Processing', max=20) as bar:
for i in range(20):
# Do some work
bar.next()

The result will be a bar like the following: ::

Processing |############# | 42/100
Expand Down
15 changes: 9 additions & 6 deletions progress/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ def next(self, n=1):
self.update()

def iter(self, it):
try:
with self:
for x in it:
yield x
self.next()
finally:
self.finish()

def __enter__(self):
self.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.finish()


class Progress(Infinite):
Expand Down Expand Up @@ -119,9 +124,7 @@ def iter(self, it):
except TypeError:
pass

try:
with self:
for x in it:
yield x
self.next()
finally:
self.finish()
7 changes: 4 additions & 3 deletions test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def sleep():

for bar_cls in (IncrementalBar, PixelBar, ShadyBar):
suffix = '%(percent)d%% [%(elapsed_td)s / %(eta)d / %(eta_td)s]'
bar = bar_cls(bar_cls.__name__, suffix=suffix)
for i in bar.iter(range(200)):
sleep()
with bar_cls(bar_cls.__name__, suffix=suffix, max=200) as bar:
for i in range(200):
bar.next()
sleep()

for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
for i in spin(spin.__name__ + ' ').iter(range(100)):
Expand Down