function - Where do I insert progress bar code in python? -
i'm having difficulty in getting progress bar working in python 2.7. have code like:
import time import sys in range(101): time.sleep(0.1) sys.stdout.write("\r%d%%" % i) sys.stdout.flush()
from source posted here earlier. however, difficulty in getting code calculate progress of function want run. example:
def example(n): print n ** 10
my question is, how can progress bar to, well, progress, function?
sometimes know how work needs done, not how take. example, have list of images need process, know total amount of work. don't know how time take process each image, because each image different in size example. so, show progress bar in terms of total amount of work , not percent of time done.
in particular, progress bar, can this.
import time import sys def progress(percent): floor = int(percent) sys.stdout.write('\r' * (floor + 9)) sys.stdout.write('[') sys.stdout.write('=' * floor) sys.stdout.write('] {:02.2f}%'.format(percent)) sys.stdout.flush()
then can use this:
>>> in range(101): ... time.sleep(0.1) ... progress(float(i)) ... else: ... print('') ... [==========================] 26.00%
[edit]
following example of image processing, can write like:
>>> images = range(300) >>> >>> def process_image(img): ... # heavy processing here ... # whatever need here each input ... time.sleep(0.1) ... >>> i, img in enumerate(images, 1): ... process_image(img) ... percent = (i / float(len(images))) * 100 ... progress(percent) ... [====================] 20.00%
Comments
Post a Comment