处理特定字段的内容,并指指定条件输出。
注意代码中用一个方法列表,并且将方法参数延后传递。
GOOGLE作过PYTHON代码的水平,就是不一样呀。
希望能学到这种通用的技巧。
只是,英文PDF看起来有难度,并且印刷代码还有错误。
有识之士能出个中文版么?现在只好硬头皮看下去。
#!/usr/bin/python import sys from optparse import OptionParser class LogProcessor(object): ''' Process a combined log format. This processor handles log files in a combined format, objects that act on the results are passed in to the init method as a series of methods. ''' def __init__(self, call_chain=None): """ Setup parser Save the call chain. Each time we process a log , we'll run the list of callbacks with the processed log results. """ if call_chain is None: call_chain = [] self._call_chain = call_chain def split(self, line): """ Split a log file. Initially,we just want size and requested file name . so we'll split on spaces and pull the data out. """ parts = line.split() return { 'size': 0 if parts[9] == '-' else int(parts[9]), 'file_requested': parts[6] } def parse(self, handle): """ Parses the log file. Returns a dictionary composed of log entry values for easy data summation """ for line in handle: fields = self.split(line) for func in self._call_chain: func(fields) class MaxSizeHandler(object): """ Check a file's size. """ def __init__(self, size): self.size = size def process(self, fields): """ Looks at each line individually. Looks at each parsed log line individually and performs a size calculation. If it's bigger than our self.size, we just print a warning. """ if fields['size'] > self.size: #print ('Warning: %s exceeds $d bytes (%s) !' % (fields['file_requested'], str(self.size), fields['size'])) print ('Warning: {0} exceeds {1} bytes {2} !'.format (fields['file_requested'], str(self.size), fields['size'])) if __name__ == '__main__': parser = OptionParser() parser.add_option('-s', '--size', dest = "size", help = "Maximum File Size Allowed", default = 0, type = "int") opts,args = parser.parse_args() call_chain = [] size_check = MaxSizeHandler(opts.size) call_chain.append(size_check.process) processor = LogProcessor(call_chain) processor.parse(sys.stdin)