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

实现进度条功能

方法一:简单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%")
时间: 2024-08-25 00:01:55

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

python基础学习日志day5-各模块文章导航

python基础学习日志day5---模块使用 http://www.cnblogs.com/lixiang1013/p/6832475.html python基础学习日志day5---time和datetime模块 http://www.cnblogs.com/lixiang1013/p/6848245.html python基础学习日志day5---random模块http://www.cnblogs.com/lixiang1013/p/6849162.html python基础学习日志da

python基础学习日志day5---logging模块

很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug(), info(), warning(), error() and critical() 5个级别,下面我们看一下怎么用. 最简单用法 1 2 3 4 5 6 7 8 import logging logging.warning("user [alex] attempt

python基础学习日志day8-异常处理

一.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面. 1)常用方式: try: pass except Exception as e: pass 一直尝试try中的代码,如果遇到错误和except中异常相同,就执行except中代码,如果和except没有相同,还是会抛出异常 # -*- coding:utf-8 -*- __author__ = 'shisanjun' names=['1','2','3'] data={} try:

python基础学习日志day7-类的反射

1)python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr, 改四个函数分别用于对对象内部执行:检查是否含有某成员.获取成员.设置成员.删除成员. # -*- coding:utf-8 -*- __author__ = 'shisanjun' class Foo(object): def __init__(self): self.name="s" def func(self): return 'func' obj=Foo() #

python基础学习日志day7-类的起源

Python中一切事物都是对象. class Foo(object): def __init__(self,name): self.name = name f = Foo("alex") f对象是FOO类的一个实例,Foo类对象是type类的一个实例. print(type(f)) print(type(foo)) # -*- coding:utf-8 -*- __author__ = 'shisanjun' class Foo(object): def func(self): pri

python基础学习日志day5--re模块

常用正则表达式符号 '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE) '$' 匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以 '*' 匹

python基础学习日志day5--subprocess模块

可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.*      --废弃,3.x中被移除 以上执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能 call 父进程等待子进程完成返回退出信息(returncode,相当于Linux exit code) 执行命令,返回状态码,shell=True是表示

python基础学习日志day6-面向对象

什么是面向对象编程 OOP编程是利用"类"和对象来创建各种模型来实现对真实世界的描述. OOP具有可维护性和可扩展性 二:面向对象有那些特性 1)CLASS类:一个类是对拥有相同属性的对象的抽象.类拥有类的属性和类的方法. 2)OBJECT对象:一个对象即是一个类的实例化的实例.这个过程就实例化. 3)Encapsulation封装:在类中对数据的赋值,内部调用对外部是透明. 4)Inheritance继承:一个类可以派生成子类,在这个父类的定义的属性和方法自动被子类继承 5)Poly

python基础学习日志day5--hashlib模块

hashlib模块用于加密操作,代替了md5和sha模块, 主要提供SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法. # -*- coding:utf-8 -*- __author__ = 'shisanjun' import hashlib m=hashlib.md5() #使用MD5算法 m.update(b"hello") #必须加b,申明为byte m.update(b"It is me") print(m.dige