随笔分类 - Python
摘要:修改自:原文 : Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects and More地址 : http://jimmyg.org/blog/2009/working-with-python-subprocess.html一 程序的stdin,stdout,stderr+redirect+pipe程序的stdin,stdout,stderr:通常地一个应用程序默认地连接有3个io流,分别为stdin标准输入流,stdout标准输出流,stderr标准错误输出流。在程序中我们可以使用它们的句柄
阅读全文
摘要:一 基本知识 1、插入节点 Element.insert(index, element) 、Element(tag[, attrib][, **extra]) 、SubElement(parent, tag[, attrib[, **extra]]) 、Element.append(subelement) 2、删除节点 Element.remove(subelement) 删除一个节点、Element.clear()删除该节点下所有子节点 3、在节点中插入属性 Element.set(key, value)4、查找节点 a) Element.getiteratorb) Element.getc
阅读全文
摘要:一 发送简单的纯文本邮件Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importsysimportos.pathimportsmtplibimportemaildefsendTextMail(mailhost,sender,recipients,ccto='',bccto='',subject='',message='',messagefile=''):try
阅读全文
摘要:一 setuptools 和easy_installsetuptools:setuptools 是一组由PEAK(Python Enterprise Application Kit)开发的 Python 的 distutils 工具的增强工具,可以让程序员更方便的创建和发布 Python的egg包,特别是那些对其它包具有依赖性的状况。 由 setuptools 创建和发布的包看起来和基于 distutils 发布的包没什么不同。最终用户不需要事先安装 setuptools 甚至根本不需要知道 setuptools 的存在,而程序员也不需要附上完整的 setuptools,只需要包含一个大小约
阅读全文
摘要:需要安装python2.x 和python-LDAP模块。python-ldap:http://www.python-ldap.org/python-ldap的windows版本下载:http://pypi.python.org/pypi/python-ldap/python26实例代码:(用来验证某用户是否存在于LDAP Server)Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importtimeimportldap''
阅读全文
摘要:python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的。常用的libraries(modules)如下:1)python运行时服务* copy: copy模块提供了对复合(compound)对象(list,tuple,dict,custom class)进行浅拷贝和深拷贝的功能。* pickle: pickle模块被用来序列化python的对象到bytes流,从而适合存储到文件,网络传输,或数据库存储。(pickle的过程也被称serializing,marshalling或者flattening,pi.
阅读全文
摘要:一 使用如下代码将keywords+builtins+modules输出到文件Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importsysdefstdoutToFile(filename,function,args):oldStdout=sys.stdoutf=open(filename,"w")sys.stdout=ffunction(args)#sys.stdout.flush()#f.close()sys.st
阅读全文
摘要:一变量与作用域 变量的定义使用var=varvalue1)如果函数中定义了同名的变量,则同名的全局变量被屏蔽,否则查找使用全局变量Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->deff1():v1='local'f2()print(v1)deff2():print(v1)v1="Value1"f1()#Value1#local2)内嵌定义的函数中的变量的使用,如果内嵌函数中没有定义,则先查找包含此内嵌函数
阅读全文
摘要:一 re.search 和 re.matchpython提供了2中主要的正则表达式操作:re.match 和 re.search。match :只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回None;search :将字符串的所有字串尝试与正则表达式匹配,如果所有的字串都没有匹配成功,返回None,否则返回matchobject;(re.search相当于perl中的默认行为)实例代码:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighte
阅读全文
摘要:简单地解析VisualStudio的buildlog:(有的时候log文件太长,但是我们只是关心warning和error, 通过该程序可以直接得到所有的warning和error的行)Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importrewarninglist=[]warninglist.append("warningC\d{4}")errorlist=[]errorlist.append("erro
阅读全文
摘要:转自:http://code.activestate.com/recipes/577518-rsync-algorithm/?in=lang-python代码:Tested in Python 2.5, 2.6, and 3.1. In 2.7, io.BufferedReader should yield the best throughput. On all other versions use __builtin__.open.Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHi
阅读全文
摘要:代码:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importrandomimportstringimporttime#strong.high=3#randomforthewholepasswd#storng.middle=2#includeonespecialsign#strong.ow=1#justincludecharactersordigitsdefmkpassByRandom(size=8,strong=2):chars=[]c
阅读全文
摘要:不断跟新中,欢迎补充!python中所有的__XXX__方法都有一定的含义,代表一定的协议,相当于CSharp和Java中的接口。特殊方法 描述基本定制型C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数)C.__new__(self[, arg1, ...]) 构造器(带一些可选的参数);通常用在设置不变数据类型的子类。C.__del__(self) 解构器C.__str__(self) 可打印的字符输出;内建str()及print 语句C.__repr__(self) 运行时的字符串输出;内建repr() 和‘‘ 操作符C.__unicode__(self
阅读全文
摘要:一 iterator迭代器1) 迭代器是实现了迭代器协议的某种类型,一般需要实现如下两个方法(1)在python2.x中,next方法,在python3.x中,为__next__(),返回容器的下一个元素(2)__iter__方法,返回迭代器自身通常的iterator与for关键字配合使用,for语句在容器对象中调用__iter__()函数返回一个定义了next()或__next__()方法的iterator。通过iterator的next()或__next__()方法来在容器中逐一访问元素,没有后续元素,next()或__next__()就会抛出一个异常,告知for循环结束。2)iterat
阅读全文
摘要:在windows上使用subst和netuseCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importosimportsubprocessdefRunCommand(cmd):returnsubprocess.call(cmd)defRunCommandWithOutput(cmd):p=subprocess.Popen(cmd,shell=True,universal_newlines=True,stdout=subprocess.PI
阅读全文
摘要:一 简单使用 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->defTestLogBasic():importlogginglogging.basicConfig(filename='log.txt',filemode='a',level=logging.NOTSET,format='%(asctime)s-%(levelname)s:%(message)s')logging.debug(
阅读全文
摘要:一 时间日期差Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->print("-----------------------------------")#classdatetime.timedelta(days=0,seconds=0,microseconds=0,milliseconds=0,minutes=0,hours=0,weeks=0)oneyear=datetime.timedelta(days=365)five
阅读全文
摘要:1 函数的默认值为mutable类型时的问题和解决办法Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->deff2(a,L=[]):L.append(a)returnLprint(f2(1))print(f2(2))print(f2(3))deff3(a,L=None):ifLisNone:L=[]L.append(a)returnLprint(f3(1))print(f3(2))print(f3(3))#theresultwillbe#[1]
阅读全文
摘要:一 withpython中的with的作用是自动释放对象,即使对象在使用的过程中有异常抛出。可以使用with的类型必须实现__enter__ __exit__。我的理解是=try...finally{},在finally中调用了释放函数。[类似与CSharp中的using(){}关键字,用来自动确保调用对象的dispose()方法,即使对象有异常抛出。C#中可以使用using{}的对象必须已经实现了IDispose接口。]Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighligh
阅读全文

浙公网安备 33010602011771号