Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1102474
  • 博文数量: 170
  • 博客积分: 1603
  • 博客等级: 上尉
  • 技术积分: 1897
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 15:54
文章分类

全部博文(170)

文章存档

2016年(27)

2015年(21)

2014年(27)

2013年(21)

2012年(7)

2011年(67)

我的朋友

分类: Python/Ruby

2011-04-26 14:22:05

 my_code.zip 
不要理会上面那个文件,没解压密码滴(*^__^*) 嘻嘻……
  
python有别人写好的很不错的进度条,我这里也自己写了个简单的,效率不知道好不好反正能用

imglong是进度条长度,maxid是最大循环次数,curid是当前循环次数,进度条为了方便处理设置成了20或者50的倍数(都是100的整倍),免得去处理浮点数-v-

class ProcessSchedule(object):
    """
    This class use to print porecess schedule
    ImgLong usr to define max long of img and it will be set as 20 or  n*50
    """
    def  __init__(self,maxid =1,ImgLong=50,startPercent = 0):
        """self.percenct is the cur percence of porcess"""
        self.percnet = startPercent
        self.ImgLong = ImgLong
        self.maxid = maxid
    
    def PrintSchedule(self,curid):
        if curid > self.maxid:return
        if self.ImgLong < 50:
            self.ImgLong = 20
        if self.ImgLong > 50:
            self.ImgLong = (int(self.ImgLong/50))*50
        self.percent = int((curid*100)/self.maxid)
        rangeStep = int(100/self.ImgLong)
        if rangeStep == 0:rangeStep =1
        for i in range(self.percent,101,rangeStep):
            if self.percent == i:
                count = int((i*self.ImgLong)/100)
                img = '#' * count
                nul = ' ' * (self.ImgLong - count)
                if self.percent < 10:
                    sys.stdout.write(img + nul + "  " + str(self.percent) + '%')
                if self.percent >= 10 and self.percent < 100:
                    sys.stdout.write(img + nul + " " +str(self.percent) + '%')
                if self.percent == 100:
                    sys.stdout.write(img + nul + '100%')
                sys.stdout.write('\r')
                sys.stdout.flush()
                self.percent = i
阅读(2221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~