Task:
A "Progress indicator" is displayed to the user during the operation at the specified time ".
Solution:
# Coding = utf-8import sysclass progressbar (object): def _ init _ (self, finalcount, block_char = '. '): Self. finalcount = finalcount self. blockcount = 0 self. block = block_char self. F = sys. stdout if not self. finalcount: return self. f. write ('\ n -------- % process ------- 1 \ n') self. f. write ('1 2 3 4 5 6 7 8 9 0 \ n') self. f. write ('0 0 0 0 0 0 0 0 0 \ n') def progress (self, count): Count = min (count, self. finalcount) If self. finalcount: percentcomplent = int (round (100.0 * count/self. finalcount) If percentcomplent <1: percentcomplent = 1 else: percentcomplent = 100 blockcount = int (percentcomplent // 2) # // It is division, but not rounding if blockcount <= self. blockcount: return for I in range (self. blockcount, blockcount): Self. f. write (self. block) self. f. flush () self. blockcount = blockcount if percentcomplent = 100: Self. f. write ("\ n") # testif _ name _ = '_ main _': From time import sleep Pb = progressbar (8 ,"*") for count in range (1, 9): Pb. progress (count) sleep (0.2) Pb = progressbar (100) Pb. progress (20) sleep (0.3) Pb. progress (47) sleep (0.3) Pb. progress (90) sleep (0.3) Pb. progress (100) print "Testing 1:" PB = progressbar (1) Pb. SS (1)
Cookbook 11.1 displays the progress bar on the text Console