马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!1剖析日记的python框架awk.py
[html]viewplaincopyprint?
- #
- #Customawk.pymodule
- #
- classcontroller:
- def__init__(self,f):
- self.m_file=f
- self.m_handlers=[]
- defsubscribe(self,o):
- self.m_handlers.append(o)
- defrun(self):
- foroinself.m_handlers:
- o.begin()
- s=self.m_file.readline()
- whiles!="":
- foroinself.m_handlers:
- o.process_line(s)
- s=self.m_file.readline()
- foroinself.m_handlers:
- o.end()
- defprint_results(self):
- print
- print"Results:"
- print
- foroinself.m_handlers:
- print"------------------------------------------------------"
- printo.description()
- print"------------------------------------------------------"
- printo.result()
统计日记的点击量count_line.py
[html]viewplaincopyprint?
- #Standardsysmodule
- importsys
- #Customawk.pymodule
- importawk
- classcount_lines:
- defbegin(self):
- self.m_count=0
- defprocess_line(self,s):
- self.m_count+=1
- defend(self):
- pass
- defdescription(self):
- return"#oflinesinthefile"
- defresult(self):
- returnself.m_count
- #
- #Step1:CreatetheAwkcontroller
- #
- ac=awk.controller(sys.stdin)
- #
- #Step2:Subscribethehandler
- #
- ac.subscribe(count_lines())
- #
- #Step3:Run
- #
- ac.run()
- #
- #Step4:Printtheresults
- #
- ac.print_results()
利用***是shell中实行
#catapachelog.log|pythoncount_lines.py
统计扫瞄次数凌驾n次的会见者visitors.py
HowmanypeoplehavereturnedtothesitemorethanNtimes?
<divclass="dp-highlighterbg_html"style="font-family:Consolas,CourierNew,Courier,mono,serif;width:700.90625px;overflow:auto;padding-top:1px;color:rgb(51,51,51);line-height:26px;margin:18px0px!important;background-color:rgb(231,229,220);">[html]viewplaincopyprint?
<olclass="dp-xml"start="1"style="padding-right:0px;padding-left:0px;border:none;list-style-position:initial;list-style-image:initial;color:rgb(92,92,92);margin:0px0px1px45px!important;background-color:rgb(255,255,255);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">importre;<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">importsys<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">imortawk<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">classreturn_visitors:<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">def__init__(self,n):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">self.m_n=n;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">self.m_ip_days={};<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">defbegin(self):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">pass;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">defprocess_line(self,s):<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">try:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">array=s.split();<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">ip=array[0];<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">day=array[3][1:7];<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">ifself.m_ip_days.has_key(ip):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">ifdaynotinself.m_ip_days[ip]:<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">self.m_ip_days[ip].append(day);<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">else:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">self.m_ip_days[ip]=[];<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">self.m_ip_days[ip].append(day);<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">exceptIndexError:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">pass;<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">defend(self):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">ips=self.m_ip_days.keys();<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">count=0;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">foripinips:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"> |