随笔分类 -  Python

摘要:logging.basicConfig配置日志记录到文件A后,再使用logging.FileHandler生成记录到文件B的logger在使用此logger记录日志时,会同时记录的文件A和文件B,感觉这个机制还是比较出乎意料的# -*- coding: utf8 -*-import loggingl... 阅读全文
posted @ 2014-09-15 20:17 鸪斑兔 阅读(3526) 评论(0) 推荐(0) 编辑
摘要:将函数作为参数传递,同时将该函数需要的参数一起传递。可参考threading.Timer的处理方式:class threading.Timer(interval, function, args=[], kwargs={})Create a timer that will run function w... 阅读全文
posted @ 2014-08-27 15:40 鸪斑兔 阅读(318) 评论(0) 推荐(0) 编辑
摘要:关键点是输出'\r'这个字符可以使光标回到一行的开头,这时输出其它内容就会将原内容覆盖。import timeimport sysdef progress_test(): bar_length=20 for percent in xrange(0, 100): hashes... 阅读全文
posted @ 2014-07-24 16:11 鸪斑兔 阅读(7132) 评论(0) 推荐(3) 编辑
摘要:参考http://www.cnblogs.com/tuzkee/p/3243110.htmlimport sysimport osdef detailtrace(info): retStr = "" curindex=0 f = sys._getframe() f = f.f... 阅读全文
posted @ 2014-07-23 16:51 鸪斑兔 阅读(2352) 评论(0) 推荐(0) 编辑
摘要:策略模式定义了算法族,分别封装起来,让它们之间可以互相替换。此模式让算法的变化独立于使用算法的客户。作为动态语言,Python实现策略模式非常容易,只要所有算法提供相同的函数即可。import osclass Script: def __init__(self, cmd): se... 阅读全文
posted @ 2014-05-21 14:52 鸪斑兔 阅读(349) 评论(0) 推荐(0) 编辑
摘要:# -*- coding: utf-8 -*-import xml.dom.minidomELEMENT_NODE = xml.dom.Node.ELEMENT_NODEclass SimpleXmlGetter(object): def __init__(self, data): ... 阅读全文
posted @ 2014-04-25 09:12 鸪斑兔 阅读(1520) 评论(0) 推荐(0) 编辑
摘要:class StepedProgress: '''方便显示进度的级联进度信息。 ''' def __init__(self, stockPercent=[1], parentProgress=None): self.percent = 0 self.in... 阅读全文
posted @ 2014-04-16 10:45 鸪斑兔 阅读(271) 评论(0) 推荐(0) 编辑
摘要:#!/bin/bashecho "Content-Type:text/html"echo ""echo "hello world!"在当前目录建立cgi-bin目录,然后将上述脚本命名为hello.sh,放在cgi-bin/hello.sh执行python -m CGIHTTPServer然后在浏览器中输入http://:8000/cgi-bin/hello.sh即可调用我们的cgi脚本说明:1、脚本前三行是必须的,第一行用于指定脚本执行使用的解释器,第二行和第三行是HTTP协议规范,在发送HTML正文之前要发送MIME头和空行2、c 阅读全文
posted @ 2014-03-05 11:58 鸪斑兔 阅读(4692) 评论(0) 推荐(1) 编辑
摘要:1 XXXServer1.1 BaseSever提供基础的循环等待请求的处理框架。使用serve_forever启动服务,使用shutdown停止。同时提供了一些可自行扩展的方法,用于对不同类型的请求做自己想要的处理。1.2 TCPServer在BaseServer基础上增加了一个TCP的socket连接,使用server_bind、server_activate、server_close处理TCP启停等操作同时增加了get_request、shutdown_request、close_request处理客户端请求。1.3 UDPServer继承自TCPServer,将socket改为了SOC 阅读全文
posted @ 2014-02-28 11:28 鸪斑兔 阅读(2931) 评论(0) 推荐(0) 编辑
摘要:1 class Test: 2 def __getattr__(self, name): 3 print name, 4 return self 5 6 def __call__(self, *args, **kwargs): 7 print "call", args, kwargs, 8 return self 9 10 t = Test()11 #输出xxx call ('x',) {} yyy call ('y',) {'m': 1, 'n': 'nnnn'} zzz call () {} 阅读全文
posted @ 2013-09-13 17:46 鸪斑兔 阅读(198) 评论(0) 推荐(0) 编辑
摘要:基本不太好搞。可以参考如下讨论:http://stackoverflow.com/questions/728164/securely-erasing-password-in-memory-pythonhttp://stackoverflow.com/questions/982682/mark-data-as-sensitive-in-python/983525#983525http://www.ibm.com/developerworks/library/s-data.html 阅读全文
posted @ 2013-08-07 15:55 鸪斑兔 阅读(416) 评论(0) 推荐(0) 编辑
摘要:网上找到如下几个思路:1、用inspect模块2、用sys._getframe模块3、用sys.exc_traceback,先抛一个异常,然后抓出traceback#!/usr/bin/env python# -*- coding: utf-8 -*-import sysdef test(depth = 0): frame = sys._getframe(depth) code = frame.f_code print "frame depth = ", depth print "func name = ", code.co_name print &qu 阅读全文
posted @ 2013-08-07 14:55 鸪斑兔 阅读(16184) 评论(1) 推荐(0) 编辑
摘要:inspect — Inspect live objectsNew in version 2.1.The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, re 阅读全文
posted @ 2013-08-07 12:18 鸪斑兔 阅读(411) 评论(0) 推荐(0) 编辑
摘要:#!/usr/bin/env python#coding=utf-8def generate_counter(): CNT = [0] def add_one(): CNT[0] = CNT[0] + 1 return CNT[0] return add_onecounter = generate_counter()print counter() # 1print counter() # 2print counter() # 3 阅读全文
posted @ 2013-08-01 09:54 鸪斑兔 阅读(895) 评论(2) 推荐(1) 编辑
摘要:当前有shell个脚本/tmp/test.sh,内容如下:#!/bin/bashexit 11使用Python的os.system调用,获取返回值是:>>> ret=os.system("/tmp/test.sh")>>> ret2816查看Manual没有说明。网上找到解释如下:os.system(cmd):该方法在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码。如果我们需要获得os.system的正确返回值,那使用位移运算(或者除以256)可以还原返回值:>>> 阅读全文
posted @ 2013-06-24 09:57 鸪斑兔 阅读(5688) 评论(1) 推荐(0) 编辑
摘要:import timedef time_me(fn): def _wrapper(*args, **kwargs): start = time.clock() fn(*args, **kwargs) print "%s cost %s second"%(fn.__name__, time.clock() - start) return _wrapper#这个装饰器可以在方便地统计函数运行的耗时。用来分析脚本的性能是最好不过了。#这样用:@time_medef test(x, y): time.sleep(0.1)@time_medef... 阅读全文
posted @ 2013-02-21 16:50 鸪斑兔 阅读(6912) 评论(0) 推荐(0) 编辑
摘要:Frame objectsFrame objects represent execution frames. They may occur in traceback objects (see below).Special read-only attributes: f_back is to the previous stack frame (towards the caller), or None if this is the bottom stack frame; f_code is the code object being executed in this frame; f_locals 阅读全文
posted @ 2013-02-19 11:19 鸪斑兔 阅读(1647) 评论(0) 推荐(0) 编辑
摘要:map(function, iterable, ...)Apply function to every item of iterable and return a list of the results. 阅读全文
posted @ 2013-02-18 14:28 鸪斑兔 阅读(240) 评论(0) 推荐(0) 编辑
摘要:可以使用id>>> print id.__doc__id(object) -> integerReturn the identity of an object. This is guaranteed to be unique amongsimultaneously existing objects. (Hint: it's the object's memory address.)>>> 阅读全文
posted @ 2013-02-06 14:50 鸪斑兔 阅读(11907) 评论(0) 推荐(0) 编辑
摘要:1、闭包(closure)2、装饰器(decorator)3、元类(metaclass)4、对象模型5、其它:new和init、作用域、传值和传引用、super 阅读全文
posted @ 2013-02-06 09:21 鸪斑兔 阅读(209) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示