易截截图软件、单文件、免安装、纯绿色、仅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的C、c++扩展

python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 ......

第一个python小程序

闲的无聊就看了一点关于python的基础知识,当时也不知道python和perl之间争论的这么的激烈(主要是当时不知道perl这个语言的性质),所以直接就看了python,下面是我的第一个用python写的小程序源码,希望朋友们多多指教,有什么问题大家尽管指正,在此先谢谢大家了。
[code]
#!/usr/bin/python
import sys, os, re
impor ......

Python int类型插入数据库 MySQLdb

 def test2():
 32     db = util.DBUnit('mysql_ab')                                    &nb ......

python 的time模板翻译及说明

python 的内嵌time模板翻译及说明
一、简介
time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
year ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号