All the work is done by the withprogressbar function. It acts as a decorator, so we can apply it to any function with the @withprogressbar syntax.
That is very convenient because the code that reports progress is isolated from the code actually doing the work, which allows us to reuse it in many different cases.
To make a decorator that interacts with the decorated function while the function itself is running, we relied on Python generators:
gen = func(*args, **kwargs) while True: try: progress = next(gen) except StopIteration as exc: sys.stdout.write('\n') return exc.value else: # display the progressbar
When we call the decorated function (in our example, the wait function), we will be in fact calling _func_with_progress ...