码迷,mamicode.com
首页 > 编程语言 > 详细

python基础学习日志day8-实现进度条功能,for和yield实现

时间:2017-06-11 18:17:54      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:数据   python   基础学习   进度   中断   print   iter   except   current   

实现进度条功能

 

方法一:简单FOR实现打印进度条功能
for i in range(10):
    print("#",end="",flush=True)
    time.sleep(0.4)

 

#方法二,yeild实现复杂进度条功能

def show_process(total):
    recive_size=0
    current_size=0

    while recive_size<total:

        if int(recive_size/total*100) >current_size: #进度比现在的大
            print("#",end="",flush=True)

            current_size=int(recive_size/total*100)
        new_size=yield  #中断
        recive_size+=new_size

total=10000000
recive_size=0

precess=show_process(total) #调用getnerator
precess.__next__()
while recive_size <total:
    recive_size+=10
    try:
        precess.send(10) #给generotor发送数据,并继续yield后面的执行
    except StopIteration as e:
        print("100%")

 

python基础学习日志day8-实现进度条功能,for和yield实现

标签:数据   python   基础学习   进度   中断   print   iter   except   current   

原文地址:http://www.cnblogs.com/lixiang1013/p/6985712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!