10-day10-yield的表达式形式
#! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "w.z" # Date: 2017/6/19 # send 先传值给yield,再从终端处继续执行...可以通过yield传值,不能第一次直接g.send(2).可以用装饰器,伪装成next一次。 # def foo(): print('starting') while True: x = yield print('xx', x) g = foo() next(g) g.send(1) print(g.send(1)) print会打印返回值 print(g.send(None)) # 加装饰器 def init(func): def wrapper(): g = func() next(g) return g return wrapper @init def foo(): print('starting') while True: x1 = yield print('xx', x1) x = foo() x.send(1) x.send(2) x.send(3) # 加装饰器 传入参数 def init(func): def wrapper(*args, **kwargs): g = func() next(g) return g return wrapper @init def foo(*args, **kwargs): print('starting') while True: x1 = yield print('xx', x1) x = foo('alex', 'food1') next(x) x.send(1) x.send(2) x.send(3) # 面向过程的编程:grep -rl 'root' /etc # grep -rl 找出所有文件的绝对路径 os.walk # start_path = r'C:\Users\cx2c\Desktop\pytest' # search(operer(cat(grep(printer(),'root')))start_path) import os def init(func): def wrapper(*args, **kwargs): g = func(*args, **kwargs) next(g) return g return wrapper # 第一阶段:递归地找文件绝对路径,发给阶段二 def search(target, start_path): g = os.walk(start_path) for par_dir, _, files in g: for file in files: file_path = r'%s\%s' % (par_dir, file) target.send(file_path) # 阶段二收到文件路径,打开文件获取对象,把文件对象发给阶段三 def opener(target): while True: file_path = yield with open(file_path,encoding='utf-8') as f: target.send(file_path,f) @init def cat(target): while True: filepath, f =yield for line in f: target.send(filepath, line) @init def grep(target, pattern): while True: filepath, line = yield if pattern in line: target.send(filepath) @init def printer(): while True: filename = yield print(filename) start_path = r'C:\Users\cx2c\Desktop\pytest' search(opener(cat(grep(printer(), 'root'))), start_path)
要默写的作业
# 作业2 grep -rl 'root' /etc import os # 装饰器配合生成器用,对被装饰对象执行一次next(func()) 操作 def init(func): def wrapper(*args, **kwargs): g = func(*args, **kwargs) next(g) return g return wrapper # 阶段一:递归地找文件的绝对路径,把路径发给阶段二 @init def search(target): 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): while True: file_path = yield with open(file_path, encoding='utf-8') as f: target.send((file_path, f)) # 阶段三:收到文件对象,for循环读取文件的每一行内容,把每一行内容发给阶段四 @init def cat(target): while True: filepath, f = yield for line in f: res = target.send((filepath, line)) if res: break # 阶段四:收到一行内容,判断root是否在这一行中,如果在,则把文件名发给阶段五 @init def grep(target, pattern): 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(): 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' start_path1 = r'C:\Users\cx2c\Desktop\pytest\10-day-10' g = search(opener(cat(grep(printer(), 'root')))) # print(g) # g.send(start_path1) # g.send(start_path2) g.send(start_path1) # import os # print(os.walk(r'C:\Users\cx2c\Desktop\pytest')) # gg = os.walk(r'C:\Users\cx2c\Desktop\pytest') # generator # for path, _, files in gg: # for file in files: # print('%s\\%s' % (path, file))