Python uses a progress bar

Source: Internet
Author: User

Added some modifications to the progress bar found in the pyskydrive Project (http://code.google.com/p/pyskydrive!

Import OS <br/> Import sys <br/> Import CMD <br/> Import unicodedata <br/> from threading import thread <br/> from time import sleep </P> <p> class progressbar (thread): <br/> "a simple text progress bar control. "</P> <p> def _ init _ (Self): <br/> self. running = true <br/> self. percent =-1 <br/> self. MSG = "" <br/> thread. _ init _ (Self) </P> <p> def run (Self): <br/> I = 1 <br/> while self. running and self. Percent <100: <br/> display = '/R' </P> <p> If self. MSG! = "": <Br/> display + = self. MSG + ''</P> <p> If self. percent> = 0: <br/> display + = STR (self. percent) + '%' <br/> display + = '-' * (40 * self. (percent/100) </P> <p> If I + 1 + Len (Display)> 80: <br/> I = 1 </P> <p> display + = '-' * I <br/> display + = '>' <br/> display + =' '* (80-len (Display )) <br/> sys. stderr. write (Display) </P> <p> I + = 1 <br/> sleep (0.5) </P> <p> sys. stderr. write ('/R' + ''* 79 + '/R') <br/> sys. stderr. flush () </P> <p> def stop (Self): <br/> "" Stop displaying progress bar. </P> <p> Note: There may be latency to stop. you 'd better wait for the thread <br/> stops. see _ stop_progress (t_BAR ). </P> <p> "" <br/> self. running = false </P> <p> def set_percent (self, percent, MSG = ""): <br/> "" Call Back Method for owner of progress bar. "<br/> self. percent = int (percent) <br/> self. ms G = MSG </P> <p> def is_alive (Self): <br/> return self. isalive () </P> <p> def _ start_progress (): <br/> "" display a progress bar "" <br/> t_BAR = progressbar () <br/> t_bar.start () <br/> return t_BAR </P> <p> def _ stop_progress (t_BAR ): <br/> "hide the progress bar" <br/> If hasattr (t_BAR, "is_alive"): <br/> alive = t_bar.is_alive () <br/> else: <br/> alive = t_bar.isalive () </P> <p> If t_BAR and alive: <B R/> t_bar.stop () <br/> t_bar.join () </P> <p> def _ set_progress (t_BAR, percent, MSG = ""): <br/> t_bar.set_percent (percent) </P> <p> def _ print_msg (t_BAR, * ARGs ): <br/> "hide the progress bar and Print message" "</P> <p> assert t_BAR is none or isinstance (t_BAR, progressbar) </P> <p> If t_BAR and t_bar.is_alive (): <br/> _ stop_progress (t_BAR) </P> <p> Print ''. join (ARGs) </P> <p> def _ unicode_str_width (text): <br /> "Calculate the exact width of Unicode string. "</P> <p> width = 0 <br/> for char in text: <br/> If isinstance (char, Unicode ): <br/> If unicodedata. east_asian_width (char) in ('F', 'w', 'A'): <br/> width + = 2 <br/> else: <br/> width + = 1 <br/> else: <br/> width + = 1 </P> <p> return width </P> <p> # test the progress bar <br/> If _ name _ = = "_ main __": <br/> t_BAR = _ start_progress () <br/> COUNT = 0; <B R/> while count <100: <br/> _ set_progress (t_BAR, Count, '% d' % (count) <br/> sleep (0.1) <br/> COUNT = count + 1 </P> <p> _ stop_progress (t_BAR) <br/> Print "done!"

 

* In fact, several control functions can be encapsulated into a class for use, which is more convenient!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.