zoukankan      html  css  js  c++  java
  • python实现一个控制台下的进度条

      今天写练习爬虫感觉很需要个进度条,就随手用函数实现了一个,到了晚上突然感觉到这个东西应该单独写出来以后肯定用用得着。

      代码也很简单,我就不细讲了,直接上代码了。

      测试代码:

      instance.py  

    import bar
    import time
    
    bar = bar.ProgressBar(50,0,2)
    
    for i in range(50):
        bar.move()
    #    if i == 15:                
    #         bar.reset_pram(30,count=100,width=3)
        bar.bar_run() 
        time.sleep(0.2)

      

    mahua

        bar.py 

    # -*- coding:utf-8 -*-
    
    '''
    @author Zhang Te 
    Created on Jan 27 2016
    '''
    
    import sys
    
    class ProgressBar:
        '''
        doc: This class has been created for something such as waiting for spyder or downloading music in python.
        '''
    
        def __init__(self,total = 0,count = 0,width = 1):
    
            self.total = total
            self.width = width
            self.count = count
    
    
        def reset_pram(self,total = 0,count = 0,width = 1):
            
            sys.stdout.write(' ' * ( self.total * self.width + 8) + '
    ')
            sys.stdout.flush()
            self.total = total
            self.count = count
            self.total = total
    
        def move(self):
            
            if self.count != self.total:
                self.count += 1
    
        def bar_run(self):
            
            if self.count <= self.total:
                sys.stdout.write(' ' * self.count * self.width + '
    ')
                sys.stdout.flush()
    
                sys.stdout.write('{0:3}/{1:3} '.format(self.count,self.total) )
                sys.stdout.write('#' * self.width * self.count + '-' * (self.total - self.count) * self.width + '
    ')        
            
                sys.stdout.flush()
            else:
                self.count = self.total
        
  • 相关阅读:
    JavaWeb
    申请百度开发者账号
    秋招C++面试相关总结索引
    游戏开发客户端
    Python源码剖析——02虚拟机
    Python源码剖析——01内建对象
    Pymongo 笔记
    调用其他文件__name__=='__main__'下代码
    Python 相关
    Python import本地模块
  • 原文地址:https://www.cnblogs.com/zhangte/p/5164562.html
Copyright © 2011-2022 走看看