Day 22 (06/19)yield的表达式 生成器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day 22 (06/19)yield的表达式 生成器相关的知识,希望对你有一定的参考价值。

yield的表达式:

# def init(func):
# def wrapper(*args,**kwargs):
# g=func(*args,**kwargs)
# next(g)
# return g
# return wrapper
#
# @init #foo=init(foo)
# def foo():
# print(‘starting‘)
# while True:
# x=yield None#return 1
# print(‘value : ‘,x)
#
#
#
#
# g=foo() #wrapper()
#
# g.send(2)


#send的效果:
# 1:先从为暂停位置的那个yield传一个值,然后yield会把值赋值x
# 2:与next的功能一样

# print(g)
#
# next(g)
# print(‘=‘*30)
# print(g.send(1))
# print(‘=‘*30)
# print(g.send(2))
# print(‘=‘*30)
# print(next(g))
# print(‘=‘*30)
# print(g.send(None)) #g.send(None)等同于next(g)

 

 

# g.send(None) # next(g)

# g.send(2)

 

 

 

def init(func):
def wrapper(*args,**kwargs):
g=func(*args,**kwargs)
next(g)
return g
return wrapper
@init
def eater(name):
print(‘%s ready to eat‘ %name)
food_list=[]
while True:
food=yield food_list#return None
food_list.append(food)
print(‘%s start to eat %s‘ %(name,food))


# e=eater(‘alex‘)
# print(e.send(‘狗屎‘))
# print(e.send(‘猫屎‘))
# print(e.send(‘alex屎‘))


# def make_shit(people,n):
# for i in range(n):
# people.send(‘shit%s‘ %i)
#
# e=eater(‘alex‘)
# make_shit(e,5)

 

 

#应用:grep -rl ‘root‘ /etc
import os
def init(func):
def wrapper(*args,**kwargs):
g=func(*args,**kwargs)
next(g)
return g
return wrapper
#阶段一:递归地找文件的绝对路径,把路径发给阶段二
@init
def search(target):
‘search file abspath‘
while True:
start_path=yield
g = os.walk(start_path)
for par_dir, _, files in g:
# print(par_dir,files)
for file in files:
file_path = r‘%s\\%s‘ % (par_dir, file)
target.send(file_path)
#阶段二:收到文件路径,打开文件获取获取对象,把文件对象发给阶段三
@init
def opener(target):
‘get file obj: f=open(filepath)‘
while True:
file_path=yield
with open(file_path,encoding=‘utf-8‘) as f:
target.send((file_path,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 #target.send((filepath,line))
tag=False
if pattern in line:
target.send(filepath)
tag=True
#阶段五:收到文件名,打印结果
@init
def printer():
‘print function‘
while True:
filename=yield
print(filename)

start_path1=r‘C:\\Users\\Administrator\\PycharmProjects\\python5期\\a‘
start_path2=r‘C:\\Users\\Administrator\\PycharmProjects\\python5期\\a\\b‘
g=search(opener(cat(grep(printer(),‘root‘))))

print(g)
# g.send(start_path1)
g.send(start_path2)

 


#
# def init(func):
# def wrapper(*args,**kwargs):
# g=func(*args,**kwargs)
# next(g)
# return g
# return wrapper
# @init
# def func():
# tag=False
# while True:
# x=yield tag #g.send(‘b‘) #x=‘b‘
# tag=False
# if ‘root‘ in x:
# print(x)
# tag=True
# g=func()
#
# print(g.send(‘a‘))
# print(g.send(‘root123‘))
# print(g.send(‘b‘))
# print(g.send(‘c‘))
# print(g.send(‘d‘))
# print(g.send(‘e‘))

 

 

内置函数:

技术分享 

 

 









































































































































以上是关于Day 22 (06/19)yield的表达式 生成器的主要内容,如果未能解决你的问题,请参考以下文章

Day 22 生成器yield表达式及内置函数(一丢丢)

day22 面向对象

day 5 正则 模块与包

Day9:yield的表达式形式面向过程编程(grep -rl 'root' /etc)

day 18 lambda表达式

随堂笔记 19 day