coroutine function

# yield turns the function into an iterator 
# The return value of return is only returned once, yield returns multiple times
# The state of the function when it pauses and continues to run the next time is saved by yield
# Coroutine function

# def eater(name):
# print ('%s start to eat food' %name)
# while True:
# food=yield
# print('%s get %s, to start eat' %(name,food))
#
# print("done")
#
# e=eater('Steel Egg')
# # print(e)
#
# next(e)
# e.send("bun")
# e.send("rice")

# print(next(e))

# use List
# def eater(name):
# print('%s start to eat food' %name)
# food_list=[]
# while True:
# food=yield food_list
# print('%s get %s,to start eat' %(name,food))
# food_list.append(food)
#
# print("done")
#
# e=eater('Steel Egg')
# print(next(e))
# print(e. send("bun"))
# print(e .send("rice"))

# Lesson iterative: Objects with __iter__ methods under the object are all iterable objects
# Iterator: The result obtained by object .__iter__() is iterator
# Features of iterator:
# Iteration __next__() takes the next value
# Advantages:
# 1. Provides a unified way to iterate objects without relying on index
# 2. Lazy calculation

# Disadvantages:
# 1. Unable to get the length of the iterator
# 2, One-time, you can only take the value backward, not forward, you can't take the value of a certain position like an index

# Generator: If the function contains yield, then the execution result of this function is the generator
# The essence of the generator is Iterator
# def func():
# n=0
# while n<5:
# yield n
# n+=1
# g=func()
# print(next(g))
# print(next(g))
#
# for i in g:
# print(i)

# Summarize the function of yield:
# 1. It is equivalent to encapsulating the __iter__ and __next__ methods inside the function
# 2. Compared with return, return can only be returned once, while yield Can return multiple times # 3. The state where the function has been suspended and continued to run is the expression form
of # yield saved by yield: # food=yield # def eater(name): # print('%s start to eat' %name) # food_list=[] # while True: # food=yield food_list # print('%s eat %s' %(name,food)) # food_list.append(food) # # def aa # e=eater('zhejiangF4' ) # print(next(e)) # e.send("stool") # The difference between e.send and next(e) # 1. If the yield in the function is in the form of an expression, then it must first be next(e) # 2 , The common feature of the two allows the function to continue running at the position where it was last paused. The difference is that send will pass a value to yield when triggering the next code execution.























# I want to pass parameters without adding next
# def init(func):
# def wrapper(*args,**kwargs):
# res=func(*args,**kwargs)
# next(res)
# return res # Return to next(res), which is the next step of execution (return food_list)
# return wrapper
#
# @init #eater=init(eater)
# def eater(name):
# print('%s start to eat' %name)
# food_list=[]
# while True:
# food=yield food_list # From return res food=yield food_list means return food_list (execute only once) +food=yield
# print('%s eat %s' %(name ,food))
# food_list.append(food)
#
#
# e=eater('zhejiangF4') #wrapper('zhejiangF4')
# # print(next(e))
# e.send("stool")
# e.send("pee")

# Crawl web pages and open different URLs constantly
# from urllib.request import urlopen
# def get():
# while True:
# url=yield
# res=urlopen(url).read( )
# print(res)
#
# g=get()
# next(g) # Make cursor stop at url=yield
# g.send('http://www/python.org') # To pass value to URL

# Coroutine function
#

# Print out the path to the root file in the etc directory, # Enter # grep -rl 'root' /etc #
in the virtual machine to get the result, but with PYTHON, you need the following method # first Writing process: # def search(): # 'Find the absolute path of the file' # pass # # def opener(): # 'Open the file and get the file handle' # pass # # def cat(): # 'Read the file content '# pass















#
# def grep():
# 'Filter a line for python'
# pass
#
# def printer():
# 'Print the path containing python'
# pass
#
# When the computer is running, enter import os
# os.walk( 'c:\\egon'')
# or
# os.walk(r'c:\egon'') # r means that all characters on the right are normal characters, no translation characters
# g=os.walk(r'c:\ egon'')
# next(g)
# next(g)
# We find the absolute path of the file by concatenating the first value with the third value

# import os
#
# def search():
# while True:
# dir_name= yield
# g=os.walk(dir_name)
# for i in g:
# # print(i)
# for j in i[-1]:
# file_path='%s\\%s' %(i[0], j)
# print(file_path)
#
# e=search()
# next(e)
# e.send('c:\\egon')

# 我想把print(file_path)的结果存起来
import os

def init(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
next(res)
return res
return wrapper

@init
def search(target):
while True:
dir_name=yield
g=os.walk(dir_name)
for i in g:
# print(i)
for j in i[-1]:
file_path='%s\\%s' %(i[0],j)
target.send(file_path) # 把file_path值传到target中

@init
def opener(target):
'Open the file, get the file content'
while True:
file_path=yield
with open(file_path) as f:
target.send(f)

@init
def cat():
while True:
f=yield
for line in f:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325155191&siteId=291194637