in sing/fondation/utils.py [0:0]
def progress_iterator(iterator, divisions=100):
"""
Wraps an iterator of known length and yield a tuple `(progress, item)`
for each `item` in `iterator`. `progress` will be None except `divisions`
times that are evenly spaced. When `progress` is not None
it will contain the current percentage of items that have been seen.
Arguments:
iterator (iterator): source iterator, should support :func:`len`.
divisions (int): progress will be every 1/divisions of the
iterator length.
Examples::
>>> for (progress, element) in progress_iterator(range(500)):
... if progress:
... print("{:.0f}% done".format(progress))
"""
length = len(iterator)
division_width = length / divisions
next_division = division_width
for idx, element in enumerate(iterator):
progress = None
if (idx + 1) >= next_division or idx + 1 == length:
next_division += division_width
progress = (idx + 1) / length * 100
yield progress, element