python学习系列之生成器与协程

原创
2016/05/30 19:15
阅读数 60

import os

import fnmatch

 

def find_files(topdir, pattern):

    print "start find_files"

    for path,dirname,filelist in os.walk(topdir):

     #   print path

     #   print dirname

     #   print filelist

        for name in filelist:

            if fnmatch.fnmatch(name,pattern):

                print "find_file"

                yield os.path.join(path,name)

 

import gzip,bz2

def opener(filenames):

    print "start opener"

    for name in filenames:

        if name.endswith(".gz"): f = gzip.open(name)

        elif name.endswith(".bz2"): f = bz2.BZ2File(name)

        else: f = open(name)

        print "opener"

        yield f

 

def cat(filelist):

    print "start cat"

    for f in filelist:

        for line in f:

            print "cat"

            yield line

 

def grep(pattern,lines):

    print "start grep"

    for line in lines:

        if pattern in line:

            print "grep"

            yield line

 

wwwlogs = find_files("python27","ast*")

print wwwlogs

for log in wwwlogs:

    print log

raw_input()

files = opener(wwwlogs)

lines = cat(files)

pylines = grep("python",lines)

for line in pylines:

    print "lines "

    print line

 

print "test"

说明:整个程序是有最后的for语句驱动的。这种实现占用内存极少,无需创建任何临时列表或其他大型的数据结构。
总结:协程可以用于数据流处理程序。 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部