随笔分类 - Python
摘要:不断跟新中,欢迎补充!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
阅读全文
摘要:1 使用%来格式字符串Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->print("hello%s:%s"%("AAA","youaresonice"))2 使用zip来将两个list构造为一个dictCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.c
阅读全文
摘要:一 基本的异常处理Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->defTestTryException():try:f=open('myfile.txt')s=f.readline()f.close()i=int(s.strip())exceptIOErrorasioerror:print(ioerror)exceptValueErrorasvalueerror:print(valueerror)except:print(&
阅读全文
摘要:经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台(操作系统类型)。代码如下:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importplatformdefTestPlatform():print("----------OperationSystem--------------------------")#Windowswillbe:(32bit,WindowsPE)#Linux
阅读全文
摘要:使用:foldercleanup.py -d 10 -k c:\test\keepfile.txt c:\test表示对c:\test目录只保留最近10天的子文件夹和keepfile.txt中指定的子文件夹。代码:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importosimportos.pathimportdatetimedefgetOption():fromoptparseimportOptionParserdes="cl
阅读全文
摘要:If an object’s value can be modified, the object is said to be mutable. If the value cannot be modified,the object is said to be immutable. mutable 可变类型,例如 list,set,自定义类型(等价于C#中的引用类型); immutable 不可变类型,例如string,numbers等(等价于C#中的值类型);一 引用和拷贝(references and copies)当程序中使用=赋值操作符时,例如a=b,对于不可变的对象,a作为b的一个拷贝.
阅读全文
摘要:一 http的get和post get和post的区别:get是从服务器上获取数据,post是向服务器传送数据。(1)参数传输方式, GET提交,请求的数据会附在URL之后,以?分割URL和传输数据,多个参数用&连接;例 如:login.action?name=hyddd&password=idontknow&verify=%E4%BD%A0 %E5%A5%BD。如果数据是英文字母/数字,原样发送,如果是空格,转换为+,如果是中文/其他字符,则直接把字符串用BASE64加密,得出如: %E4%BD%A0%E5%A5%BD,其中%XX中的XX为该符号以16进制表示的ASC
阅读全文
摘要:一 python文件的encoding默认地,python的.py文件以标准的7位ASCII码存储,然而如果有的时候用户需要在.py文件中包含很多的unicode字符,例如.py文件中需要包含中文的字符串,这时可以在.py文件的第一行或第二行增加encoding注释来将.py文件指定为unicode格式。#!/usr/bin/env python# -*- coding: UTF-8 -*-s = "中国" # String in quotes is directly encoded in UTF-8.但是如果你的py文件是文本文件,且是unicode格式的,不指定# -*
阅读全文
摘要:一 MySQL模块安装下载:http://sourceforge.net/projects/mysql-python安装: python setup.py build (源码安装) python setup.py install支持:目前支持MySQL versions 3.23-5.1和Python versions 2.3-2.6二 MySQL操作过程1)import MySQLdb导入MySQLdb模块。2)conn = MySQLdb.connect()使用connect()来连接MySQL数据库,connect()用来和数据库建立连接,接收数个参数,返回连接对象.比较常用的参数包括h
阅读全文
摘要:很多常用的python函数或模块,经常需要查看帮助,很不方便。在python的交互命令行下使用help()或在python文件中调用help()函数可以很方便的查看帮助。一 查看所有的关键字:help("keywords")Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->HereisalistofthePythonkeywords.Enteranykeywordtogetmorehelp.andelifimportretu
阅读全文
摘要:一 python提供的xml支持2种工业标准的xml解析方法-SAX和DOM。SAX(simple API for XML),是基于事件处理的,当XML文档顺序地读入时,每次遇到一个元素会触发相应的事件处理函数来处理。DOM(Document Object Model),通过构建一个树结构来表现整个xml文档,一旦树被构建,可以通过DOM提供了接口来遍历树和提取相应的数据。python还提供了python独特的xml解析方法,相比于SAX和DOM更容易使用和更加快速,此方法为ElementTree。python的xml模块为:1)xml.dom.minidom2)xml.elementtree
阅读全文
摘要:打印出builtin的函数:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->forbuiltinindir(__builtins__):if(not(builtin.find("Error")>=0orbuiltin.find("Warning")>=0orbuiltin.find("Exception")>=0)):print(builtin)结果:Code hi
阅读全文
摘要:列出常见类型的方法:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->defListFunctions(lists):print("------------------------------------------")print(type(lists))foritemindir(lists):if(notitem.startswith("__")):print(item)#listl=[1,2,3]#o
阅读全文
摘要:webservice提供方:http://www.webxml.com.cn/zh_cn/web_services.aspx天气预报webservice:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx一 使用urllib + xml.dom.minidom通过http get的方式来使用webservice:例如上海的天气:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=58367http
阅读全文