Allow usage as a context manager.

This commit is contained in:
Tobias Gruetzmacher
2016-07-31 21:58:51 +02:00
parent 6553b7b207
commit 086cfd5599
3 changed files with 24 additions and 9 deletions

View File

@@ -73,12 +73,17 @@ class Infinite(object):
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):
@@ -119,9 +124,7 @@ class Progress(Infinite):
except TypeError:
pass
try:
with self:
for x in it:
yield x
self.next()
finally:
self.finish()