• python函数之协程与面向过程编程


    第一:协程

    初步了解协程

    def eater():
        print('start to eat')
        while True:
            food=yield
            print('is eating food:%s'%food)
    
    def producer():
        dog=eater()
        next(dog) # 先next一次唤醒协程函数
        #然后下面的操作都是dog.send(值)
        food1='北京烤鸭'
        food2='鱼香肉丝'
        dog.send(food1)
        dog.send(food2)
    producer()
    
    输出结果:
    start to eat
    is eating food:北京烤鸭
    is eating food:鱼香肉丝

    如果有多个多个协程函数都需要初始化的话,我们用装饰器

    def init(func):
        def warpper(*args,**kwargs):
            g=func(*args,**kwargs)
            next(g)
            return g
        return warpper
    
    @init #eater=init(eater) #eater=warpper
    def eater():
        print('start to eat')
        while True:
            food=yield
            print('is eating food:%s' % food)
    
    e=eater()#e=warpper()
    e.send('北京烤鸭')
    
    输出结果:
    start to eat
    is eating food:北京烤鸭

    如果我们在写项目的时候,并且有多个协程函数,我们需要写注释,该怎么写:

    错误示例:

    def init(func):
        def warpper(*args,**kwargs):
            '我是warpper函数'
            g=func(*args,**kwargs)
            next(g)
            return g
        return warpper
    
    @init #eater=init(eater) #eater=warpper
    def eater():
        '我是eater函数'
        print('start to eat')
        while True:
            food=yield
            print('is eating food:%s' % food)
    
    print(help(eater))
    
    输出结果:
    Help on function warpper in module __main__:
    
    warpper(*args, **kwargs)
        我是warpper函数
    
    None
    
    我们看的是eater函数的帮助信息,返回的却是warpper函数信息的帮助
    View Code

    正确示例:

    import functools
    def init(func):
        @functools.wraps(func) #让warpper的帮助信息同eater相同
        def warpper(*args,**kwargs):
            '我是warpper函数'
            g=func(*args,**kwargs)
            next(g)
            return g
        return warpper
    
    @init #eater=init(eater) #eater=warpper
    def eater():
        '我是eater函数'
        print('start to eat')
        while True:
            food=yield
            print('is eating food:%s' % food)
    
    print(help(eater))
    
    输出结果:
    Help on function eater in module __main__:
    
    eater()
        我是eater函数
    
    None
    View Code

    第二:面向过程编程

    #实现grep -rl 'python' /root/

    import os
    
    import functools
    def init(func):
        @functools.wraps(func) #让warpper的帮助信息同eater相同
        def warpper(*args,**kwargs):
            '我是warpper函数'
            g=func(*args,**kwargs)
            next(g)
            return g
        return warpper
    
    #面向过程的编程
    #扩展性差
    def get_file(abs_path,target):
        '''
        生产一个个文件
        :param abs_path:传进来的决对路径
        :param target: 下一个目标
        :return: 返回绝对文件的路径
        '''
        g=os.walk(abs_path)
        for top_dir,current_dir,files in g:
            for file in files:
                abs_file_path=r'%s\%s' %(top_dir,file)
                target.send(abs_file_path)
    
    @init
    def opener(target):
        '''
        打开文件
        :return: 返回句柄
        :param target 下一个目标
        '''
        while True:
            abs_file_path=yield
            with open(abs_file_path) as f:
                target.send((f,abs_file_path))
    
    @init
    def get_lines(target):
        '''
        读取每一行的内容
        :param target: 下一个目标
        :return:返回每一行的内容
        '''
        while True:
            f,abs_file_path=yield
            for line in f:
                target.send((line,abs_file_path))
    
    @init
    def grep(pattern,target):
        '''
        过滤关键字
        :param pattern:匹配关键字 
        :param target: 下一个目标
        :return: 返回文件
        '''
        while True:
            line,abs_file_path=yield
            if pattern in line:
                target.send(abs_file_path)
    
    @init
    def printer():
        '''
         打印功能
        :return:输出信息 
        '''
        while True:
            abs_file_path=yield
            print(abs_file_path)
    
    
    get_file(r'D:	est',opener(get_lines(grep('zzl',printer()))))
  • 相关阅读:
    [POJ][3072][Robot]
    [HDU][4021][24 Puzzle]
    [POJ][1228][Grandpa's Estate]
    [POJ][3662][Telephone Lines]
    [HDU][4007][Dave]
    [POJ] [1264] [SCUD Busters]
    ASP.NET中的COOKIE
    分享至...(源码)
    php获取图片并输出
    VNC 服务器/本地复制粘贴实现命令
  • 原文地址:https://www.cnblogs.com/ylqh/p/6523659.html
Copyright © 2020-2023  润新知