I’m making a progress indicator for some long-running console process with intent to use it like this:
pi = ProgressIndicator()
for x in somelongstuff:
do stuff
pi.update()
pi.print_totals()
Basically, it should output some kind of a progress bar with dots and dashes, and something like “234234 bytes processed” at the end.
I thought it would be nice to use it as a context manager:
with ProgressIndicator() as pi:
for x in somelongstuff:
do stuff
pi.update()
However there are a few things that concern me about this solution:
- extra indentation makes the indicator feature appear more important than it actually is
- I don’t want
ProgressIndicatorto handle any exceptions that might occur in the loop
Is this a valid use case for a context manager? What other solutions can you suggest?
It definitely seems a valid use case. The context manager doesn’t have to handle exceptions if you don’t want it to, although you would want to end the line that the progress bar is output on to prevent it being confused with the traceback, and have it not print the totals if it is exited through an exception.
With regard to indentation, I’d argue that letting the user see progress is actually a very important feature so it’s fine for it to take up an indentation level.