易截截图软件、单文件、免安装、纯绿色、仅160KB

强大的Python生成器

逐步演进
f=open('/etc/motd','r')
longest=0
while True:
    lineLen=len(f.readline().strip())
    if not lineLen: break
    if lineLen > longest:
        longest=lineLen
f.close()
return longest
问题:一直占有文件句柄未释放
f=open('/etc/motd','r')
longest=0
allLines=f.readLines()
f.close()
for line in allLines:
    lineLen=len(line.strip())
    if lineLen > longest:
        longest=lineLen
return longest
问题:读完文件所有行再开始计算,耗费大量内存
f=open('/etc/motd','r')
allLineLens=[len(x.strip()) for x in f]
f.close()
return max(allLineLens)
问题:文件迭代器一行一行迭代f,列表解析需要文件所有行都会读到内存中
f=open('/etc/motd','r')
longest=max(len(x.strip()) for x in f)
f.close()
return longest
问题:可去掉文件打开模式,让python去处理打开的文件
return max(len(x.strip()) for x in open('etc/motd'))
这个完美了。


相关文档:

python 集合运算

集合类型操作符(所有的集合类型)
联合( | )
联合(union)操作和集合的OR(又称可兼析取(inclusive
disjunction))其实是等价的,两个集
合的联合是一个新集合,该集合中的每个元素都至少是其中一个集合的成员,即,属于两个集合其

之一的成员。联合符号有一个等价的方法,union().
Edit By Vheavens
Edit By Vhe ......

使用pdb进行python的调试

1 在想要插入断点的地方插入代码
                import pdb
                pdb.set_trace()
2然后使用指令进行debug
查看代码上下文,l(小写L)
监视变量 ......

Python MySqlDB 增删改数据库


下载安装MySQLdb
http://sourceforge.net/projects/mysql-python/ 好像没看到windows版本for python2.6的下载,网上搜索到一个
http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
安装后import MySQLdb会出现 DeprecationWarning: the sets module is deprecated 这样一个警告,google之
......

python 的time模板翻译及说明

python 的内嵌time模板翻译及说明
一、简介
time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
year ......

Python获取操作系统版本信息

    最近,想在我的YouMoney(http://code.google.com/p/youmoney/)里面增加提取用户操作系统版本信息。比如windows用户,可能要返回Windows XP ,或者Windows 2003, 苹果用户应该返回Mac OS X 10.5.8。用了很多办法,包括在mac系统里调用系统命令,取环境变量,等等。最后无意发现,原来python里里面有个pl ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号