Python基础day-10[生成器表达式,面向过程编程]

生成器的表达式形式:

  使用.send向yield传送一个值,.send无法在函数第一次运行时传递传入,函数必须先运行一次.send才能正常工作。

  .send有next的功能,并且还有给yield传值的效果。并且运行时,是先传值在next。

  示例:

def foo():
    print('starting')
    while True:
        x = yield
        print('value: ',x)

g = foo()
next(g)    #先执行一遍函数
g.send(1)   #传送数值
g.send(2)
执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-10/day10_练习.py
starting
value:  1
value:  2

Process finished with exit code 0

生成器的表达式形式,每次传值之前必须先运行一遍,所以可以写个装饰器来出来这件事。

装饰器示例:

def func(func):         #<--------装饰器
    def start(*args,**kwargs):
        g = func(*args,**kwargs)
        next(g)
        return g
    return start
@func      #调用装饰器
def eat(name):
    print('%s is ready' %name)
    list1 = []   #定义一个空列表
    while True:
        food = yield list1     #yield返回值为列表
        list1.append(food) 
        print('%s eat %s' %(name,food))

e = eat('abc')
print(e.send(123))
print(e.send(456))
print(e.send(789))

执行结果:
D:\Python\Python36-32\python.exe E:/Python/DAY-10/day10_练习.py
abc is ready
abc eat 123
[123]
abc eat 456
[123, 456]
abc eat 789
[123, 456, 789]

Process finished with exit code 0

面向过程编程:grep -rl  ’root‘ 、etc

  面向过程编程的核心思想是过程。

使用os.walk查找文件的绝对路径

def search():   
    g = os.walk(start_path)
    for path_dir,_,file in g:
        for filename in file:
            file_path = r'%s\%s'%(path_dir,filename)
        print(file_path)
start_path = r'E:\Python\Exercise'
search()
执行结果:

D:\Python\Python36-32\python.exe E:/Python/DAY-10/day10_练习.py
E:\Python\Exercise\1.py
E:\Python\Exercise\170609_练习题.py
E:\Python\Exercise\170612_练习题.py
E:\Python\Exercise\170614_练习题.py
E:\Python\Exercise\170615_练习题.py
E:\Python\Exercise\170617_练习题.py
E:\Python\Exercise\a.txt
E:\Python\Exercise\mysql_select.py
E:\Python\Exercise\shopping.py
E:\Python\Exercise\temp.py
E:\Python\Exercise\user.txt
E:\Python\Exercise\userinfo.txt

Process finished with exit code 0

模拟: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))   #接收阶段四的tag
            if res:    #判断
                break       #跳出循环,终止本次文件读取循环
 
#阶段四:收到一行内容,判断root是否在这一行中,如果在,则把文件名发给阶段五
@init
def grep(target,pattern):
    'grep function'
    tag=False
    while True:
        filepath,line=yield tag   #yield返回值为 tag
        tag=False       #初始tag为 False
        if pattern in line:
            target.send(filepath)
            tag=True        #如果判断成功就 把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)

 

posted @ 2017-06-19 16:40  neuropathy_ldsly  阅读(192)  评论(0)    收藏  举报