Tuesday, May 3, 2011

关于 print

很奇怪的用法:
print c
大概就是把 c 先转换成为 str,然后 sys.stdout.write,最后加上换行。有时候不想换行,就用
print c,
但是很多时候这样会出现两次调用之间的空格,这可以用下面的方法消除:
print c,
sys.stdout.softspace=0
但是这个 softwspace 只在那一次有效,后面必须 reset 到 0,也许是 print 搞的鬼?

比较有意思的时 print 和 sys.stdout.write 之间的差异,似乎 print 多了一个转换和换行,但我想这中间肯定还有别的什么。看了一下 help,果然有一脚
|  softspace
 |      flag indicating that a space needs to be printed; used by print
这个实现真是 ugly 啊 ugly....

Thursday, March 3, 2011

aspell-python

为 aspell 的动态链接库提供了支持,可以做单词拼写检查:
import aspell
speller.aspell.Speller()
speller.check( 'hello' ) 

安装简单,编译产生一个 .so 然后放在需要的地方就行了。

Sunday, February 20, 2011

py_compile

将 py 的源文件编译成为 pyc 的 byte code。


import py_compile
py_compile.compile( 'some.py' )

subprocess

subprocess 提供了好几个老 module 的功能,其核心成员就是 Popen 类,其构造函数包括一个命令,一般可以用 raw_input 让用户输入,然后 split 成为一个 arg list。可以指定创建进程的 stdin、stdout 等信息,如果使用 PIPE 可表示创建需要的管道。获得的 Popen 对象含有 stdin、stdout、pid 等信息,可以 wait、toll。

下面是一个简单的 snippet 我们可以看看管道的用法:
import subprocess
import sys
ls = subprocess.Popen( ['ls', '-l'], stdout=sys.stdout)
ls.wait()

popen2

这是用来做管道通讯的 module,现在已经 deprecated 了,建议用 subprocess。

提供的函数主要是 popen2/3/4 以及对应的类,返回 stdin、stdout、stderr 这些。下面是一个简单的例子:
#!/usr/bin/python

import popen2

if __name__ == '__main__':
    cout, cin = popen2.popen2( 'grep abc' )
    print >> cin, 'abc\ndef\nghi'
    cin.close()
    for line in cout:
        print line,

Tuesday, February 15, 2011

re

python 的 RE 模块非常好用,记得 pattern 描述用 raw string,这主要是避免 \ 被多次转义非常麻烦。

re 有两种用法,一个直接调用 re.func,常用的有 match(从头开始匹配)、search(任意位置开始匹配) 、sub 替换、findall 和 finditer(用于迭代),也可以用 re.compile( r're') 预先编译产生一个 RE 对象,这样做类似的操作会快很多。像 search 的结果一般放在 group 里面,调用 match object 的 group(0) 返回所有。

下面是一个简单的 snippet 用于 incremental 搜索某个 re:
import re
xxx_pat = re.compile( r'"(.*?)"' )
res = xxx_pat.search( my_text )
for s in res:
    print s.group(1)

Sunday, February 13, 2011

httplib

这是一个用于获得 http 协议内容获得的库,最常见的用处无外于写 browser 或者 crawler。基本用法就是创建 HTTPConnection 对象,然后 request(使用 GET 或者 POST)获得一个 HTTPResponse 对象,如果其 status == 200 且 reason == 'OK',则可以调用 read 方法获得对应的数据。

下面是一个简单的 snippet:
import httplib
conn = httplib.HTTPConnection( 'www.yahoo.com' )
res = conn.request( 'GET', '/', headers = {'User-Agent': ua, 'Cookie': cookie })
if res.status == 200:
    html = res.read()
    # parse the html
else:
    print res.status, res.reason