面向过程:核心是面向过程,过程即解决问题的步骤,基于面向过程去设计程序,就像是在设计流水线
#优点:程序结构清晰,可以把复杂的问题简单化,流程化#缺点:可扩展性差,一条流水线只是用来解决一个问题os模块功能展示:
#Author wangmengzhu #装饰器 def init(func): def wrapper(*args,**kwargs): g = func(*args,**kwargs) next(g) return g return wrapper #第一阶段,找到所有文件的绝对路径 import os @init def search(target): while True: filepath = yield g = os.walk(filepath) for dirpath,_,files in g: for file in files: abspath = r‘%s\%s‘%(dirpath,file) target.send(abspath) #第二阶段,打开文件 @init def opener(target): while True: abspath = yield with open(abspath) as f: target.send((abspath,f)) #第三阶段,循环读出每一行的内容 @init def cat(target): while True: abspath,f = yield for line in f: res = target.send((line,abspath)) if res: break #第四阶段,过滤 @init def grep(pattern,target): tag = False while True: line,abspath = yield tag tag = False if pattern in line: target.send(abspath) tag = True #第五阶段 @init def printer(): while True: abspath = yield print(abspath) g = search(opener(cat(grep(‘error‘,printer())))) g.send(r‘C:\Users\wangmengzhu\PycharmProjects\MZ\a‘)
时间: 2024-10-11 11:58:49