协程

def init(func):
def wrapper(*args,**kwargs):
obj = func(*args,**kwargs)
next(obj)
return obj
return wrapper @init
def food(name):
print("%s start to eat" % name)
food_list = []
while True:
food = yield food_list
food_list.append(food)
print("%s eat %s" % (name,food_list)) e = food("xyp") # xyp start to eat
e.send("apple") # xyp eat ['apple']
e.send("apple") # xyp eat ['apple', 'apple']
e.send("apple") # xyp eat ['apple', 'apple', 'apple']
......

协程 吃包子

 #grep -rl 'python' C:\egon
import os,time
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 #dir_name='C:\\egon'
print('车间search开始生产产品:文件的绝对路径')
time.sleep(2)
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) @init
def opener(target):
'打开文件,获取文件句柄'
while True:
file_path=yield
print('车间opener开始生产产品:文件句柄')
time.sleep(2)
with open(file_path) as f:
target.send((file_path,f)) @init
def cat(target):
'读取文件内容'
while True:
file_path,f=yield
print('车间cat开始生产产品:文件的一行内容')
time.sleep(2)
for line in f:
target.send((file_path,line)) @init
def grep(pattern,target):
'过滤一行内容中有无python'
while True:
file_path,line=yield
print('车间grep开始生产产品:包含python这一行内容的文件路径')
time.sleep(0.2)
if pattern in line:
target.send(file_path) @init
def printer():
'打印文件路径'
while True:
file_path=yield
print('车间printer开始生产产品:得到最终的产品')
time.sleep(2)
print(file_path) g=search(opener(cat(grep('python',printer()))))
g.send('C:\\egon')
g.send('D:\\dir1')
g.send('E:\\dir2')

协程windows 查找文件

 import os

 def init(func):
def wrapper(*args,**kwargs):
obj = func(*args,**kwargs)
next(obj)
return obj
return wrapper @init
def search(target):
while True:
dir_name = yield
obj = os.walk(dir_name)
for file in obj:
for path in file[-1]:
file_path = "%s/%s" % (file[0],path)
target.send(file_path) @init
def opener(target):
while True:
file_path = yield
with open(file_path) as f:
target.send((file_path,f))
@init
def cat(target):
while True:
file_path,file = yield
for line in file:
target.send((file_path,line))
@init
def grep(pattern,target):
while True:
file_path,line = yield
if pattern in line:
target.send(file_path)
@init
def print_file():
while True:
file_path = yield
print(file_path) g = search(opener(cat(grep("a",print_file()))))
g.send("/Users/centos/Desktop/example")

协程Linux查找文件


模拟grep -rl “python” F:\xuyaping这个命令
#查看xuyaping文件夹所有的绝对路径
import os
g=os.walk("F:\\xuyaping") #g为迭代器
for i in g:
# print(i) #i为文件路径
for j in i[-1]:
file_path="%s\\%s" %(i[0],j)
print(file_path)

程序输出结果:

F:\xuyaping\xuyaping.txt.txt
F:\xuyaping\xuyaping1.txt.txt
F:\xuyaping\a\a.txt.txt
F:\xuyaping\a\a1\a1.txt.txt
F:\xuyaping\a\a1\a2\a2.txt.txt
F:\xuyaping\b\b.txt.txt

代码如下:

#模拟grep -rl “python” F:\xuyaping这个命令

import os,time
#初始化被装饰的函数,使其运行到yield所在的行
def init(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
next(res)
return res
return wrapper #search函数用循环找到一个绝对路径,往下一个阶段opener函数传递一次,直至传递完。
# 所以opener函数循环的收,所以opener函数用while循环。
@init
def search(target): #定义生成器target,用于传递路径file_path
"找到文件的绝对路径"
while True:
dir_name=yield
print("车间search开始生产产品:文件的绝对路径")
time.sleep(1)
g = os.walk(dir_name)
for i in g:
for j in i[-1]:
file_path="%s\\%s" %(i[0],j)
target.send(file_path) #传递路径file_path,给opener函数 @init
def opener(target):
"打开文件,获取文件句柄"
while True:
file_path=yield #opener函数接收search函数传递过来的路径file_path。opener函数将执行结果传递给search函数中的target
print("车间opener开始生产产品:文件句柄")
time.sleep(1)
with open(file_path) as f:
target.send((file_path,f)) #同样传递句柄f 和file_path给cat函数,所以此时用生成器target来传递,同上
@init
def cat(target):
"读取文件内容"
while True:
file_path,f=yield #cat函数接收opener函数传递过来的句柄f 和file_path。cat函数将执行结果传递给opener函数中的target
print("车间cat开始生产产品:文件的一行内容")
time.sleep(1)
for line in f:
target.send((file_path,line)) @init
def grep(pattern,target):
"过滤一行内容中有无python"
while True:
file_path,line=yield
print("车间grep开始生产产品:包含python这一行内容的文件路径")
time.sleep(1)
if pattern in line:
target.send(file_path) @init
def printer():
"打印文件路径"
while True:
file_path=yield
print("车间printer开始生产产品:得到最终的产品")
time.sleep(1)
print(file_path) #g=search("target")--->g=search(opener(target))--->g=search(opener(cat(target)))
# --->g=search(opener(cat(grep(patteren,target))))--->g=search(opener(cat(grep("python",printer()))))
g=search(opener(cat(grep("python",printer()))))
g.send("F:\\xuyaping")

运行结果:

车间search开始生产产品:文件的绝对路径
车间opener开始生产产品:文件句柄
车间cat开始生产产品:文件的一行内容
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间printer开始生产产品:得到最终的产品
F:\xuyaping\xuyaping.txt.txt
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间opener开始生产产品:文件句柄
车间cat开始生产产品:文件的一行内容
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间printer开始生产产品:得到最终的产品
F:\xuyaping\xuyaping1.txt.txt
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间opener开始生产产品:文件句柄
车间cat开始生产产品:文件的一行内容
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间opener开始生产产品:文件句柄
车间cat开始生产产品:文件的一行内容
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间printer开始生产产品:得到最终的产品
F:\xuyaping\a\a1\a1.txt.txt
车间opener开始生产产品:文件句柄
车间cat开始生产产品:文件的一行内容
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间opener开始生产产品:文件句柄
车间cat开始生产产品:文件的一行内容
车间grep开始生产产品:包含python这一行内容的文件路径
车间printer开始生产产品:得到最终的产品
F:\xuyaping\b\b.txt.txt
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
车间grep开始生产产品:包含python这一行内容的文件路径
 

python(18)- 协程函数及应用的更多相关文章

  1. python 3 协程函数

    python 3 协程函数 1:把函数的执行结果封装好__iter__和__next__,即得到一个迭代器 2:与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yiel ...

  2. Python之协程函数

    Python之协程函数 什么是协程函数:如果一个函数内部yield的使用方法是表达式形式的话,如x=yield,那么该函数成为协程函数. def eater(name): print('%s star ...

  3. Python基础(协程函数、内置函数、递归、模块和包)-day05

    写在前面 上课第五天,打卡: 凭着爱,再回首: 一.协程函数(生成器:yield的表达式形式) 1.yield 的语句形式: yield 1 - 这种方式在 Python基础(函数部分)-day04  ...

  4. python之协程函数、递归、二分法

    一.协程函数: 协程函数的语法: def eater(name): print('%s说:我开动啦' %name) food_list=[] while True: food=yield food_l ...

  5. python基础-协程函数、递归、模块、包等内容

    1. 协程函数 1.1 yield基本用法 yield作用 1.把函数的执行结果封装好,即封装__iter__和__next__,即得到一个迭代器 2.与return功能类似,都可以返回值,但不同的是 ...

  6. python自动化 协程函数、二分查找、模块搜索

    协程函数 yiled: 把函数的执行结果封装好__iter__和__next__得到一个迭代器 与return功能类似,都可以返回值,但是return只能返回一次只 def fun(count): p ...

  7. python协程函数应用 列表生成式 生成器表达式

    协程函数应用 列表生成式 生成器表达式   一.知识点整理: 1.可迭代的:对象下有_iter_方法的都是可迭代的对象 迭代器:对象._iter_()得到的结果就是迭代器 迭代器的特性: 迭代器._n ...

  8. python协程函数、递归、匿名函数与内置函数使用、模块与包

    目录: 协程函数(yield生成器用法二) 面向过程编程 递归 匿名函数与内置函数的使用 模块 包 常用标准模块之re(正则表达式) 一.协程函数(yield生成器用法二) 1.生成器的语句形式 a. ...

  9. python基础之协程函数、列表表达式、生成器表达式

    一.协程函数 协程函数的定义?如果在一个函数内部yield的使用方式是表达式形式的话,如x=yield,那么该函数称为协程函数 协程函数补充: def init(func): def wrapper( ...

  10. python基础----迭代器、生成器、协程函数及应用(面向过程实例)

    一.什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代 ...

随机推荐

  1. 国际化多语言(本地化)缩写 NLS API

    NLS Information for Windows 7 LCID Culture Identifier Culture Name Locale Language Country/Region La ...

  2. C++之Effective STL学习笔记Item21

    好了,先贴一段英文,真心不想翻译过来,而且感觉也翻译不到那么到位: The STL is awash in comparisons of objects to see if they have the ...

  3. 【bzoj】P4407于神之怒加强版(莫比乌斯反演)

    题目链接 套路一般的枚举$gcd(i,j)=w$.设$min(n,m)=top$,则有 $\sum\limits_{i=1}^{n}\sum\limits_{j=1}{m}gcd(i,j)$ $=\s ...

  4. NOJ——1508火烧赤壁2(并查集+启发式合并+逆序加边)

    [1508] 火烧赤壁2 时间限制: 1000 ms 内存限制: 65535 K 问题描述 上次出了一道火烧赤壁的题目给当时的新生,也就是你们的上一届学长们做,那么这次,我又想到了另一个想法. 上次的 ...

  5. 移动web端使用rem实现自适应原理

    1.先获取到物理像素和实际像素的像素比 var pixclPatio = 1 / window.devicePixelRatio; 2.viewport视口设置为像素比大小 document.writ ...

  6. IOs动画的那些事儿

    CoreAnimation介绍 1:Core Animation是直接作用在CALayer上的(并非UIView上)非常强大的跨Mac OS X和iOS平台的动画处理API,Core Animatio ...

  7. 【Visual Studio】Visual Studio 2015快捷键设置问题 alt+ F8 (转)

    具体修改方法如下: 工具-选项-环境-键盘-应用以下其他键盘映射方案,选择visual C++6, 然后编代码试试,嘿,我的alt+F8回来了(否则是ctrl + k, ctrl + f); 原文转自 ...

  8. html css的简单学习(三)

    html css的简单学习(三) 前端开发工具:Dreamweaver.Hbuilder.WebStorm.Sublime.PhpStorm...=========================== ...

  9. Struts学习总结(一)

    1,需要包下载地址: http://archive.apache.org/dist/struts/binaries/ 2.报错:org/apache/commons/lang3/StringUtils ...

  10. poj 1950(搜索)

    Dessert Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5430   Accepted: 2029 Descripti ...