PYTHON文本处理指南之日志LOG解析
处理特定字段的内容,并指指定条件输出。
注意代码中用一个方法列表,并且将方法参数延后传递。
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)
PYTHON文本处理指南之日志LOG解析的更多相关文章
- Python 文本解析器
Python 文本解析器 一.课程介绍 本课程讲解一个使用 Python 来解析纯文本生成一个 HTML 页面的小程序. 二.相关技术 Python:一种面向对象.解释型计算机程序设计语言,用它可以做 ...
- 5. python 文本解析
5. python 文本解析 这一章节我们简单的聊聊文本解析的两种方法: 1.分片,通过分片,记录偏移处,然后提取想要的字符串 例子: >>> line='aaa bbb ccc' ...
- Python 编码风格指南
原文:http://python.jobbole.com/84618/ 本文超出 PEP8 的范畴以涵盖我认为优秀的 Python 风格.本文虽然坚持己见,却不偏执.不仅仅涉及语法.模块布局等问题,同 ...
- 【转】python模块分析之logging日志(四)
[转]python模块分析之logging日志(四) python的logging模块是用来写日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分 ...
- python模块分析之logging日志(四)
前言 python的logging模块是用来设置日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python模块 ...
- Python 全栈开发九 日志模块
日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件也有重要性的概念 ...
- Python开发人员指南
本指南是一个全面的资源贡献 给Python的 -为新的和经验丰富的贡献者.这是 保持由维护的Python同一社区.我们欢迎您对Python的贡献! 快速参考 这是设置和添加补丁所需的基本步骤.了解基础 ...
- .NetCore中的日志(1)日志组件解析
.NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...
- 如何正确使用日志Log
title: 如何正确使用日志Log date: 2015-01-08 12:54:46 categories: [Python] tags: [Python,log] --- 文章首发地址:http ...
随机推荐
- myecipse的debug调试操作方法
在myecipse如果想要查询某个变量的值,或者跟踪程序的执行流程,可以如下操作: 首先在程序中设置好断点(断点的设置方法,就是在想要设置的地方的行首双击,当一个蓝色的圆形实心图标显示出来,就证明你设 ...
- 浮动闭合方案:clearfix
1 ;clear:both;visibility:hidden} .clearfix{*+height:1%;} 2 .clearfix{overflow:auto;_height:1%} 3 ;}
- Android(java)学习笔记191:Android数据存储5种方式总结
1.使用文件(File)存储 存储一般的数据 2.使用sharedperference(xml) 存储设置信息.配置信息.密码 3.数据库Sqlite 开源的,嵌入式的数据库,轻量级 4.使用Cont ...
- SetUID、SetGID中的大小写Ss和Sticky bit中的大小写Tt
大写:原文件/目录没有执行(x)权限 小写:原文件/目录有执行(x)权限 例如: 原文件:-rwxr-xr-x 增加SetUID后 4755 变为:-rwsr-xr-x 再如: 原文件:-rwxr-- ...
- 使用DataList 分页方法
什么是DataList我想应该不需要解释了,接下来分享本人在项目里使用到的通过DataList进行分页展示方法. 首先在ASPX页面添加一个DataList(后面都简称DL)控件,示例代码如下: &l ...
- rest例子
http://www.xdemo.org/spring-restful/(可用) http://www.open-open.com/lib/view/open1389075258125.html(有例 ...
- HttpServletRequest 获取URL的方法及区别
HttpServletRequest 获取请求的URL的方法有: 1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它 ...
- 《转》15种CSS混合模式让图片产生令人惊艳的效果
浏览器支持 按照现在情况来讲, 浏览器支持 CSSbackground-blend-mode属性还在不断的完善中.早期版本的浏览器目前还不支持,但caniuse.com报告说在Chrome,Firef ...
- LVS单机测试不负载
LVS单机测试不负载 1.困惑 当我们在个人PC上搭建虚拟机(Vmware)做LVS负载实验的时候,我们不论是在个人浏览器或者其他虚拟机上访问LVS的VIP都会出现上时间刷新都出现同一个页面的情况. ...
- Java学习----finally块
public class Test { String x; public static void main(String[] args) { Test test = new Test(); try { ...