Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, April 11, 2007

Import file -- Python

一直没有在意import的机制,也没有遇到什么大的问题。今天import文件夹里的file时出现问题。

如果import的文件在文件夹里,比如 test/main.py ,test所在的目录已经在python_path中,from test.main import * 会提示找不到路径。解决方法,在test目录里添加文件__init__.py 就可以了。

Set content type and charset --Quixote

Set python procedure character as Unicode as below:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Set editor (Vim) as Unicode, add "set encoding=utf-8" in _vimrc.
The last thing is let quixote tell browser that the coming content should be encoded as UTF-8. Before return the content in function, add code "get_response().set_content_type("text/html", charset="utf-8")". In order to use get_resonse(), "from quixote import get_response" first.

Friday, October 13, 2006

Switch statments in Python

Python没有switch语句。本人比较笨,每次都用一对if...elif...。今天才发现在Simon的blog上,他介绍了通过dictionary和lambda实现switch。

result  = {  
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}[value](x)

在ASPN上,还有"Readable switch construction without lambdas or dictionaries ",建了个switch的类。

Tuesday, September 19, 2006

看了一下午wxPython in action

终于大概搞明白了wxPython是咋回事了。晚上再看sizer吧。不用sizer还真麻烦ni。

Friday, September 08, 2006

Thursday, August 31, 2006

Built-in function: map, zip

map(func,Seq1, Seq2,..,Seqx) 生成一个分别以seq1, seq2..,seqx为参数的func返回结果的list。
如果func为None, list的值是Seq1, Seq2,...,Seqx 配对在一起的tuplle.

def func(arg1,arg2,...,argx):
...
return result
a=[1,2,3,4,5]
b=[6,7,8,9,10]

def test_plus(i,j):
return i+j

c=map(test_plus,a,b)

print c

output:
[7, 9, 11, 13, 15]



zip() 相当于第一个参数为None的map. 返回结果就是配对的seq形成的list