|
5 | 5 | ---------- |
6 | 6 | 问题 |
7 | 7 | ---------- |
8 | | -todo... |
| 8 | +你想以数据管道(类似Unix管道)的方式迭代处理数据。 |
| 9 | +比如,你有个大量的数据需要处理,但是不能将它们一次性放入内存中。 |
| 10 | + |
| 11 | +| |
9 | 12 |
|
10 | 13 | ---------- |
11 | 14 | 解决方案 |
12 | 15 | ---------- |
13 | | -todo... |
| 16 | +生成器函数是一个实现管道机制的好办法。 |
| 17 | +为了演示,假定你要处理一个非常大的日志文件目录: |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + foo/ |
| 22 | + access-log-012007.gz |
| 23 | + access-log-022007.gz |
| 24 | + access-log-032007.gz |
| 25 | + ... |
| 26 | + access-log-012008 |
| 27 | + bar/ |
| 28 | + access-log-092007.bz2 |
| 29 | + ... |
| 30 | + access-log-022008 |
| 31 | +
|
| 32 | +假设每个日志文件包含这样的数据: |
| 33 | + |
| 34 | +.. code-block:: python |
| 35 | +
|
| 36 | + 124.115.6.12 - - [10/Jul/2012:00:18:50 -0500] "GET /robots.txt ..." 200 71 |
| 37 | + 210.212.209.67 - - [10/Jul/2012:00:18:51 -0500] "GET /ply/ ..." 200 11875 |
| 38 | + 210.212.209.67 - - [10/Jul/2012:00:18:51 -0500] "GET /favicon.ico ..." 404 369 |
| 39 | + 61.135.216.105 - - [10/Jul/2012:00:20:04 -0500] "GET /blog/atom.xml ..." 304 - |
| 40 | + ... |
| 41 | +
|
| 42 | +为了处理这些文件,你可以定义一个由多个执行特定任务独立任务的简单生成器函数组成的容器。就像这样: |
| 43 | + |
| 44 | +.. code-block:: python |
| 45 | +
|
| 46 | + import os |
| 47 | + import fnmatch |
| 48 | + import gzip |
| 49 | + import bz2 |
| 50 | + import re |
| 51 | +
|
| 52 | + def gen_find(filepat, top): |
| 53 | + ''' |
| 54 | + Find all filenames in a directory tree that match a shell wildcard pattern |
| 55 | + ''' |
| 56 | + for path, dirlist, filelist in os.walk(top): |
| 57 | + for name in fnmatch.filter(filelist, filepat): |
| 58 | + yield os.path.join(path,name) |
| 59 | +
|
| 60 | + def gen_opener(filenames): |
| 61 | + ''' |
| 62 | + Open a sequence of filenames one at a time producing a file object. |
| 63 | + The file is closed immediately when proceeding to the next iteration. |
| 64 | + ''' |
| 65 | + for filename in filenames: |
| 66 | + if filename.endswith('.gz'): |
| 67 | + f = gzip.open(filename, 'rt') |
| 68 | + elif filename.endswith('.bz2'): |
| 69 | + f = bz2.open(filename, 'rt') |
| 70 | + else: |
| 71 | + f = open(filename, 'rt') |
| 72 | + yield f |
| 73 | + f.close() |
| 74 | +
|
| 75 | + def gen_concatenate(iterators): |
| 76 | + ''' |
| 77 | + Chain a sequence of iterators together into a single sequence. |
| 78 | + ''' |
| 79 | + for it in iterators: |
| 80 | + yield from it |
| 81 | +
|
| 82 | + def gen_grep(pattern, lines): |
| 83 | + ''' |
| 84 | + Look for a regex pattern in a sequence of lines |
| 85 | + ''' |
| 86 | + pat = re.compile(pattern) |
| 87 | + for line in lines: |
| 88 | + if pat.search(line): |
| 89 | + yield line |
| 90 | +
|
| 91 | +现在你可以很容易的将这些函数连起来创建一个处理管道。 |
| 92 | +比如,为了查找包含单词python的所有日子行,你可以这样做: |
| 93 | + |
| 94 | +.. code-block:: python |
| 95 | +
|
| 96 | + lognames = gen_find('access-log*', 'www') |
| 97 | + files = gen_opener(lognames) |
| 98 | + lines = gen_concatenate(files) |
| 99 | + pylines = gen_grep('(?i)python', lines) |
| 100 | + for line in pylines: |
| 101 | + print(line) |
| 102 | +
|
| 103 | +如果将来的时候你想扩展管道,你甚至可以在生成器表达式中包装数据。 |
| 104 | +比如,下面这个版本计算出传输的字节数并计算其总和。 |
| 105 | + |
| 106 | +.. code-block:: python |
| 107 | +
|
| 108 | + lognames = gen_find('access-log*', 'www') |
| 109 | + files = gen_opener(lognames) |
| 110 | + lines = gen_concatenate(files) |
| 111 | + pylines = gen_grep('(?i)python', lines) |
| 112 | + bytecolumn = (line.rsplit(None,1)[1] for line in pylines) |
| 113 | + bytes = (int(x) for x in bytecolumn if x != '-') |
| 114 | + print('Total', sum(bytes)) |
| 115 | +
|
| 116 | +| |
14 | 117 |
|
15 | 118 | ---------- |
16 | 119 | 讨论 |
17 | 120 | ---------- |
18 | | -todo... |
| 121 | +以管道方式处理数据可以用来解决各类其他问题,包括解析,读取实时数据,定时轮询等。 |
| 122 | + |
| 123 | +In understanding the code, it is important to grasp that the yield statement acts as a |
| 124 | +kind of data producer whereas a for loop acts as a data consumer. When the generators |
| 125 | +are stacked together, each yield feeds a single item of data to the next stage of the |
| 126 | +pipeline that is consuming it with iteration. In the last example, the sum() function is |
| 127 | +actually driving the entire program, pulling one item at a time out of the pipeline of |
| 128 | +generators. |
| 129 | +为了理解上述代码,重点是要明白yield语句作为数据的生产者而for循环语句作为数据的消费者。 |
| 130 | +当这些生成器被连在一起后,每个yield会将一个单独的数据元素传递给迭代处理管道的下一阶段。 |
| 131 | +在例子最后部分 |
| 132 | + |
0 commit comments