Python的生成器进阶玩法

时间:2023-03-09 07:35:16
Python的生成器进阶玩法

              Python的生成器进阶玩法

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.yield的表达式形式

 #!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com def foo():
print("starting")
while True:
print("=======")
x = yield #将yield的返回值进行赋值操作,我们可以称之为生成器表达式
print("value:%s"% x ) g = foo()
# print(g)
next(g) #Next方法默认是不传值的,生成器第一次传值传值必须为空(g.send(None) ),
# 否则就会报错(TypeError: can't send non-None value to a just-started generator),一次传None值的操作我们称之为初始化。 g.send(100) #相比next方法多了一个传值操作,即把100传给生成器g中的yield。 g.send(None) #如果传值(send)为空(None)则等同于next(g)方法。 #以上代码执行结果如下:
starting
=======
value:100
=======
value:None
=======

二.生成器初始化传值操作

 #!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import time def InitializationValue(func): #定义一个装饰器对生成器进行初始化操作。
def wrapper(*args,**kwargs):
g = func(*args,**kwargs)
try:
next(g) #进行生成器传值的初始化操作,这样用户就可以第一次进行传值(因为Next方法默认是不传值的,生成器第一次传值传值必须为空(g.send(None) ))
except TypeError:
pass
return g
return wrapper @InitializationValue
def consumer(name):
# print("%s 准备吃饺子啦!"% name)
while True:
dumplings = yield
print("饺子[%s]来了,被[%s]吃了!"% (dumplings,name)) @InitializationValue
def producer(name):
c = consumer("yinzhengjie")
# print("俺要开始准备吃饺子了!")
for i in range(1,10):
time.sleep(1)
print("%s做的第%s个饺子"%(name,i))
c.send(i)
producer("尹正杰") #以上代码执行结果如下:
尹正杰做的第1个饺子
饺子[1]来了,被[yinzhengjie]吃了!
尹正杰做的第2个饺子
饺子[2]来了,被[yinzhengjie]吃了!
尹正杰做的第3个饺子
饺子[3]来了,被[yinzhengjie]吃了!
尹正杰做的第4个饺子
饺子[4]来了,被[yinzhengjie]吃了!
尹正杰做的第5个饺子
饺子[5]来了,被[yinzhengjie]吃了!
尹正杰做的第6个饺子
饺子[6]来了,被[yinzhengjie]吃了!
尹正杰做的第7个饺子
饺子[7]来了,被[yinzhengjie]吃了!
尹正杰做的第8个饺子
饺子[8]来了,被[yinzhengjie]吃了!
尹正杰做的第9个饺子
饺子[9]来了,被[yinzhengjie]吃了!

三.面向过程编程案例

 #!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com
import os def init(func):
def wrapper(*args,**kwargs):
g = func(*args,**kwargs)
next(g)
return g
return wrapper #阶段一:递归找文件的绝对路径,把路径发给阶段二
@init
def Search(Target):
"sercg files abapath"
while True:
StartPath = yield
g = os.walk(StartPath)
for ParDir, _, files in g: # 注意,ParDir是父目录,其中“_"是父目录下的子目录,“files”则是父目录下的所有子文件!
for file in files:
FilePath = r"%s\%s" % (ParDir, file)
Target.send(FilePath) #阶段二:收到文件路径,打开文件获取文件对象吗,把文件对象发给截断三;
@init
def Opener(Target):
"get file obj :f = open(filepath)"
while True:
FilePath = yield
with open(FilePath,encoding="utf-8")as f:
Target.send((FilePath,f)) #阶段三:收到文件对象,for循环读取文件的每一行内容,把每一行内容发给阶段四;
@init
def Cat(Target):
"read file"
while True:
FilePath,f = yield
for line in f:
res = Target.send((FilePath,line))
if res:
break
#阶段四:收到一行内容,判断root是否在这一行中,如果在,则把文件名发给阶段五;
@init
def Grep(Target,Pattern):
"grep function"
tag = False
while True:
FilePath,line = yield tag #我们用tag来标记是否找到文件名,如果找到了就为True
tag = False
if Pattern in line:
Target.send(FilePath)
tag = True
#阶段五:收到文件名,并打印结果;
@init
def Printer():
"parint function"
while True:
Filename = yield
print(Filename) Startpath = r"E:\Code\pycharm\文件存放处\python学习笔记\DAY7"
g = Search(Opener(Cat(Grep(Printer(),"yinzhengjie")))) print(g) g.send(Startpath) """
经过上面的程序课件yield表达式形式有以下特征:
面向过程的程序设计思想:
核心是:过程,过程就是流程
优点:
1>.思路清晰;
2>.复杂的问题简单化;
3>.流程化;
缺点:
1>.扩展性差
应用:
1>.Linux内核;
2>.httpd;
3>.git;
""” #以上代码执行结果如下:
<generator object Search at 0x02A02450>
E:\Code\pycharm\文件存放处\python学习笔记\DAY7\1.yield的表达式形式.py
E:\Code\pycharm\文件存放处\python学习笔记\DAY7\2.生成器初始化传值操作.py
E:\Code\pycharm\文件存放处\python学习笔记\DAY7\3.面向过程编程-用生成器模拟grep功能.py
E:\Code\pycharm\文件存放处\python学习笔记\DAY7\access.log