1. 程式人生 > >協程函數

協程函數

AS return pytho 讀取 就是 star 狀態 org art

# yield把函數變成叠代器
# return的返回值只返回一次,yield返回多次
# 函數在暫停以及繼續下一次運行時的狀態是有yield保存
# 協程函數

# 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(‘鋼蛋‘)
# # print(e)
#
# next(e)
# e.send("包子")
# e.send("飯")

# print(next(e))

# 用列表
# 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(‘鋼蛋‘)
# print(next(e))
# print(e.send("包子"))
# print(e.send("飯"))

# 課叠代的:對象下有__iter__方法的都是可叠代的對象
# 叠代器:對象.__iter__()得到的結果就是叠代器
# 叠代器的特性:
# 叠代器.__next__()取下一個值
# 優點:
# 1、提供了一種統一的叠代對象的方式,不依賴於索引
# 2、惰性計算

# 缺點:
# 1、無法獲取叠代器的長度
# 2、一次性的,只能往後取值,不能往前退,不能像索引那樣取某個位置的值

# 生成器:函數內帶有yield,那麽這個函數的執行結果就是生成器
# 生成器本質就是叠代器
# 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)

# 總結yield的功能:
# 1、相當於把__iter__和__next__方法封裝到函數內部
# 2、與return比,return只能返回一次,而yield能返回多次
# 3、函數暫停已經繼續運行的狀態是通過yield保存的

# 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("大便")

# e.send與next(e)的區別
# 1、如果函數內yield是表達式形式,那麽必須先next(e)
# 2、二者的共同之處可以讓函數在上次暫停的位置,繼續運行,不一樣的地方在於send在觸發
# 下一次代碼的執行時,會順便給yield傳一個值

# 我想不用加next,就能傳參數
# def init(func):
# def wrapper(*args,**kwargs):
# res=func(*args,**kwargs)
# next(res)
# return res # 返回 next(res),就是執行(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 # 從return res取值 food=yield food_list意思是return food_list(只執行一次)+food=yield
# print(‘%s eat %s‘ %(name,food))
# food_list.append(food)
#
#
# e=eater(‘zhejiangF4‘) #wrapper(‘zhejiangF4‘)
# # print(next(e))
# e.send("大便")
# e.send("小便")

# 爬網頁,不斷地打開不同的URL
# from urllib.request import urlopen
# def get():
# while True:
# url=yield
# res=urlopen(url).read()
# print(res)
#
# g=get()
# next(g) # 讓光標停到url=yield
# g.send(‘http://www/python.org‘) # 為了傳值到URL

# 協程函數
#

# 把etc目錄下,到root的文件的路徑都打印出來,
# 在虛擬機輸入
# grep -rl ‘root‘ /etc
# 就可以得到結果,但是用PYTHON,需要下面的做法

# 先寫流程:
# def search():
# ‘找到文件的絕對路徑‘
# pass
#
# def opener():
# ‘打開文件,獲取文件句柄‘
# pass
#
# def cat():
# ‘讀取文件內容‘
# pass
#
# def grep():
# ‘過濾一行中有無python‘
# pass
#
# def printer():
# ‘打印包含python的路徑‘
# pass
#
# 在電腦運行中,輸入import os
# os.walk(‘c:\\egon‘‘)
# 或
# os.walk(r‘c:\egon‘‘) # r代表右邊的都是普通字符,沒有轉譯符
# g=os.walk(r‘c:\egon‘‘)
# next(g)
# next(g)
# 我們發現第一個值與第3個值拼接就得到文件的絕對路徑

# 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):
‘打開文件,獲取文件內容‘
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:

協程函數