Differences between revisions 3 and 19 (spanning 16 versions)
Revision 3 as of 2006-09-15 05:01:31
Size: 28590
Editor: ZoomQuiet
Comment:
Revision 19 as of 2006-11-16 02:26:52
Size: 25323
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
'''
Python 绝对简明手册
'''-- [email protected]
::-- ZoomQuiet [[[DateTime(2006-09-15T04:35:33Z)]]]
[[TableOfContents]]
''' Python 绝对简明手册 '''-- [email protected] ::-- ZoomQuiet [[[DateTime(2006-09-15T04:35:33Z)]]] [[TableOfContents]]
Line 9: Line 6:
[[Include(CPUGnav)]]
##[[Include(CPUGnav)]]
Line 15: Line 11:
[http://groups.google.com/group/python-cn/browse_thread/thread/8322e786aa364789/2116205595a4a493#2116205595a4a493 zuroc主动在列表中分享]

PYthon绝对简明手册,初学必备!

  -- For Python2.5

{{{
版本:0.1 beta

作者:张沈鹏

参考:python2.5中文教程

}}}
Http://blog.csdn.net/zuroc

 -- 欢迎指出错误和遗漏,提出建议和意见,请发信到[email protected]



现在学的东西很容易忘记,写这篇文章的目的是能让我在需要时快速找回当时的感觉. 需要深入的请下载 attachment:diveintopython%2Dpdf%2D%35.%34.zip

 . -- For Python2.5
{{{
版本:0.2 beta
作者:张沈鹏 电子科大大三 生物医学工程
参考:python2.5中文教程
感谢:Zoom.Quiet limodou
}}}
[http://blog.csdn.net/zuroc 我的技术Blog] [http://www.cppblog.com/zuroc 我的C++Blog] [http://www.cnweblog.com/zuroc/ 我的文学Blog]

 . -- 欢迎指出错误和遗漏,提出建议和意见, 请发信到[email protected]
Line 37: Line 24:
Line 39: Line 25:
{{{#!python
x=int(raw_input("Please enter an integer:")) 
if x<0: 
    print 'Negative Number' 
elif x==0: 
    print 'Zero' 
else: 
    print 'Positive Number'  }}}

=== for ===
{{{
#!python
a=['cat','door,'example'] 
for x in a: 
    print x 
}}}
#如果要修改a的内容,则用a的副本循环,如: 
{{{
for x in a[:] :      .....................
}}}

{{{
#!python 
>>>range(10,0,-3) 
[10,7,4,1] 
a=['cat','door,'example'] 
for i in range(len(a)): 
    print i,a[i]  }}}
 

break,continue 用法和C++中类似          === pass ===
{{{
while True: 
    pass #忽略,什么也不做  }}}

{{{
#!python
def fib(n=1000):#参数可以有默认值,多个可选参数赋值可以直接写"参数变量名=值"来快速赋值 
    """这里给函数写文档注释""" 
    a,b=0,1 
    while b<n:         print b
       a,b=b,a+b  }}}
 

#函数可以重命名,如   {{{#!python
f=fib 
f(223)  }}}



 

=== in ===
 {{{
#!python
if 'yes' in ('y','ye','yes'):print 'ok'  }}}

 
{{{
#!python
x=int(raw_input("Please enter an integer:"))
if x<0:
    print 'Negative Number'
elif x==0:
    print 'Zero'
else:
    print 'Positive Number'
}}}
=== for ===
{{{
#!python
a=['cat','door','example']
for x in a:
    print x
}}}
#如果要修改a的内容,则用a的副本循环,如:

{{{
for x in a[:] :
    .....................
}}}


{{{
#!python
>>>range(10,0,-3)
[10,7,4,1]
a=['cat','door','example']
for i in range(len(a)):
    print i,a[i]
}}}

break,continue 用法和C++中类似

=== pass
===
{{{
while True:
    pass #忽略,什么也不做
}}}
{{{
#!python
def fib(n=1000):#参数可以有默认值,多个可选参数赋值可以直接写"参数变量名=值"来快速赋值
    """这里给函数写文档注释"""
    a,b=0,1
    while b<n:
       print b

       a,b=b,a+b
}}}

#函数可以重命名,如

{{{
#!python
f=fib
f(223)
}}}
=== in ===
 . {{{
#!python
if 'yes' in ('y','ye','yes'):print 'ok'
}}}
Line 112: Line 87:
#参数格式为 **para 表示接受一个字典,为 *para 表示接受一个元组 

{{{#!python 
def test(para1,*args,**dic): 
    print para1 
    for arg in args : print arg 
    keys=dic.keys() 
    keys.sort() 
    for key in keys:print key ,':',dic[key] 
}}}
  
#参数格式为 **para 表示接受一个字典,为 *para 表示接受一个元组

{{{
#!python
def test(para1,*args,**dic):
    print para1
    for arg in args : print arg
    keys=dic.keys()
    keys.sort()
    for key in keys:print key ,':',dic[key]
}}}
Line 125: Line 99:
 {{{#!python 
def make_incrementor(n): 
    return lambda x: x+n 
f=make_incrementor(n) 
>>>f(0) 
42 
>>>f(1) 
43  }}}

 

=== List的函数  ===
{{{
append(x) 追加到链尾 
extend(L) 追加一个 
insert(i,x) 在位置i插入x 
remove(x) 删除第一个值为x的元素,如果不存在会抛出异常 
pop([i]) 返回并删除位置为i的元素,i未给定时默认作用在最后一个元素.[i]表示i为可选的 
index(x) 返回第一个值为x的元素,不存在则抛出异常 
count(x) 返回x出现的次数 
sort() 排序 
reverse() 翻转,反转  }}}

`
filter(function函数 , sequence序列) `
返回sequence中使filer为true的   `map(function,sequence,[sequence...]) `
返回新的sequence,序列中值为对每个元素分别调用function.      `reduce(function,sequence,[init]) `
返回一个单值为,计算步骤为 :
 * 第1个结果=function(sequence[0],sequence[1]) 
 * 第2个结果=function(第1个结果,sequence[2]) 
 * 返回最后一个计算得值 
 * 如果有init,则先调用   `function(init,sequence[0]) `
sequence只有一个元素时,返回该元素,为空时抛出异常.   === 链表推导式 ===
{{{
freshfruit=[' banana ',' loganberry '] 
>>>[weapon.strip() for weapon in freshfruit] 
['banana','loganberry'] 
vec=[2,4,6] 
>>>[3*x for x in vec if x>3] 
[12,18] 
 . {{{
#!python
def make_incrementor(n):
    return lambda x: x+n
f=make_incrementor(n)
>>>f(0)
42
>>>f(1)
43
}}}

=== List的函数 ===
{{{
append(x) 追加到链尾
extend(L) 追加一个
insert(i,x) 在位置i插入x
remove(x) 删除第一个值为x的元素,如果不存在会抛出异常
pop([i]) 返回并删除位置为i的元素,i未给定时默认作用在最后一个元素.[i]表示i为可选的
index(x) 返回第一个值为x的元素,不存在则抛出异常
count(x) 返回x出现的次数
sort() 排序
reverse() 翻转,反转
}}}
{{{
filter(function函数 , sequence序列) }}} 返回sequence中使filer为true的

{{{
map(function,sequence,[sequence...]) }}} 返回新的sequence,序列中值为对每个元素分别调用function.

{{{
reduce(function,sequence,[init]) }}} 返回一个单值为,计算步骤为 :

* 第1个结果=function(sequence[0],sequence[1])
 * 第2个结果=function(第1个结果,sequence[2])
 * 返回最后一个计算得值
 * 如果有init,则先调用
{{{
function(init,sequence[0]) }}} sequence只有一个元素时,返回该元素,为空时抛出异常.

=== 列
表推导式 ===
{{{
freshfruit=[' banana ',' loganberry ']
>>>[weapon.strip() for weapon in freshfruit]
['banana','loganberry']
vec=[2,4,6]
>>>[3*x for x in vec if x>3]
[12,18]
Line 177: Line 142:
[x,x**2 for x in vec]是错误的 
[(2,4),(4,16),(6,36)] 
vec2=[4,3,-9] 
[x*y for x in vec for y in vec2] 
[vec[i]+vec2[i] for i in range(len(vec))] 
[x,x**2 for x in vec]是错误的
[(2,4),(4,16),(6,36)]
vec2=[4,3,-9]
[x*y for x in vec for y in vec2]
[vec[i]+vec2[i] for i in range(len(vec))]
Line 184: Line 149:

   === del ===

{{{
#!python  a=[1,2,3,4,5,6]
del a[0]  >>>a
[2,3,4,5,6]

del a[2:4]  >>>a
[2,3,6]

del a[:]  >>>a
[]
del a
>>>a

}}}
抛出异常      

=== 元组  ===
{{{
t=1234,5567,'hello' 
x,y,z=t #拆分操作可以应用于所有sequence 
>>>x  1234
u=t,(1,2,3) 
>>>u 
((1234,5567,'hello'),(1,2,3)) 
empty=() #空元组 
singleton='hi', #单个元素的元组  }}}

=== del ===
{{{
#!python
a=[1,2,3,4,5,6]

del a[0]
>>>a
[2,3,4,5,6]

del a[2:4]
>>>a
[2,3,6]

del a[:]
>>>a
[]
del a
>>>a

}}}
抛出异常

=== 元组 ===
{{{
t=1234,5567,'hello'
x,y,z=t #拆分操作可以应用于所有sequence
>>>x
1234

u=t,(1,2,3)
>>>u
((1234,5567,'hello'),(1,2,3))
empty=() #空元组
singleton='hi', #单个元素的元组
}}}
Line 224: Line 180:
set(集合):无序不重复的元素集   {{{#!python   basket = ['apple','orange','apple','pear','apple','banana']
fruit=set(basket) 
>>>fruit  set(['orange', 'pear', 'apple', 'banana'])
>>>'orange' in fruit 
True 
a=set('abracadabew')  >>>a
set(['a', 'c', 'b', 'e', 'd', 'r', 'w']) 
b=set('wajgwaoihwb')  >>> b
set(['a', 'b', 'g', 'i', 'h', 'j', 'o', 'w']) 
>>> a-b #差  set(['c', 'r', 'e', 'd'])
>>> a|b #并 
set(['a', 'c', 'b', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r', 'w']) 
>>> a&b #交 
set(['a', 'b', 'w']) 
>>>a^b #(并-交) 
set(['c', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r'])  }}}


=== dict ===


字典:关键字为不可变类型,如字符串,整数,只包含不可变对象的元组.表等不可以作为关键字.如果表中存在关键字对,可以用dict()直接构造字典.而这样的表对通常是由表推导式生成的.   {{{#!python 
tel={'jack':4098,'sape':4139} 
tel['guido']=4127  >>> tel
{'sape': 4139, 'jack': 4098, 'guido': 4127} 
>>>tel['jack'] 
4098  del tel['sape']
>>>tel.keys() 
['jack', 'guido'] 
>>>tel.has_key('jack') 
True  knight=['gallahad':'the pure','robin':'the brave']
for k,v in knight.iteritems(): 
    print k,v 
}}}
输出:   {{{#!python 
gallahad the pure 
robin the brave   }}}



enumerate()返回索引位置和对应的值   {{{#!python  
for i,v in enumerate(['tic','tac','toe']) 
    print i,v  }}}

输出:
0 tic  

1 tac  

2 toe    
set(集合):无序不重复的元素集

{{{
#!python
basket = ['apple','orange','apple','pear','apple','banana']

fruit=set(basket)
>>>fruit
set(['orange', 'pear', 'apple', 'banana'])

>>>'orange' in fruit
True
a=set('abracadabew')
>>>a

set(['a', 'c', 'b', 'e', 'd', 'r', 'w'])
b=set('wajgwaoihwb')
>>> b

set(['a', 'b', 'g', 'i', 'h', 'j', 'o', 'w'])
>>> a-b #差
set(['c', 'r', 'e', 'd'])

>>> a|b #并
set(['a', 'c', 'b', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r', 'w'])
>>> a&b #交
set(['a', 'b', 'w'])
>>>a^b #(并-交)
set(['c', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r'])
}}}
=== dict ===


字典:关键字为不可变类型,如字符串,整数,只包含不可变对象的元组.表等不可以作为关键字.如果表中存在关键字对,可以用dict()直接构造字典.而这样的表对通常是由表推导式生成的.

{{{
#!python
tel={'jack':4098,'sape':4139}
tel['guido']=4127
>>> tel

{'sape': 4139, 'jack': 4098, 'guido': 4127}
>>>tel['jack']
4098
del tel['sape']

>>>tel.keys()
['jack', 'guido']
>>>tel.has_key('jack')
True
knight={
'gallahad':'the pure','robin':'the brave'}
for k,v in knight.iteritems():
    print k,v
}}}
输出:

{{{
#!python
gallahad the pure
robin the brave
}}}

enumerate()返回索引位置和对应的值

{{{
#!python
for i,v in enumerate(['tic','tac','toe'])
    print i,v
}}}

输出:  0 tic

1 tac

2 toe
Line 299: Line 247:
zip用于多个sequence的循环   {{{#!python 
questions=['name','quest','favorite color'] 
answers=['lancelot','the holy grail','blue'] 
for q,a in zip(questions,answers): 
    print 'What is your %s ? It is %s.'%(q,a) 
}}}
输出:   {{{#!python
What is your name ? It is lancelot. 
What is your quest ? It is the holy grail. 
What is your favorite color ? It is blue.  }}}
 

=== reversed反向循环  ===
 

{{{
#!python 
for i in reversed(range(1,4)):      print i
}}}

输出:   
3   
2   
1     
=== sorted排序  ===
 

 

=== sequence比大小  ===

list<string<tuple(因为字母表中l在s前...) 

同类比大小按照字典序       
zip用于多个sequence的循环

{{{
#!python
questions=['name','quest','favorite color']
answers=['lancelot','the holy grail','blue']
for q,a in zip(questions,answers):
    print 'What is your %s ? It is %s.'%(q,a)
}}}
输出:

{{{

What is your name ? It is lancelot.
What is your quest ? It is the holy grail.
What is your favorite color ? It is blue.
}}}

=== reversed反向循环 ===
{{{
#!python
for i in reversed(range(1,4)):
    print i
}}}

输出:

{{{

3
2
1
}}}

=== sorted排序 ===
=== sequence比大小 ===
list<string<tuple(因为字母表中l在s前...)

同类比大小按照字典序
Line 353: Line 283:
   模块的查找路径  

1.当前的目录  

2.环境变量PYTHONPATH所指的目录列表  

3.环境变量PATH表示的目录


4
.python解释器的安装目录    

如将代码保存上述的一个目录中的的fibo.py文件中,便可以   {{{#!python  import fibo
fibo.function().............  }}}

如果想直接使用fibo.function可以重命名这个函数,如   {{{#!python 
f=fibo.function 
f() 
}}}
也可以{{{#!python  form fibo import function
function()  }}}

甚至可以`form fibo import * `


可以 `
form 包.子包.模块 imort 函数 `
模块的查找路径

1.当前的目录

2.环境变量PYTHONPATH所指的目录列表

3.python解释器的安装目录

如将代码保存上述的一个目录中的的fibo.py文件中,便可以

{{{
#!python
import fibo

fibo.function().............
}}}

如果想直接使用fibo.function可以重命名这个函数,如

{{{
#!python
f=fibo.function
f()
}}}
也可以

{{{
#!python
form fibo import function

function()
}}}

甚至可以{{{form fibo import * }}}

可以 {{{
form 包.子包.模块 imort 函数 }}}
Line 396: Line 318:
  
Line 399: Line 319:


引用推荐写法为 

`form 包 import 模块`
引用推荐写法为

{{{form 包 import 模块}}}
Line 410: Line 327:
Line 413: Line 329:
 __init__.py
 
Formats/
  __init__.py
  
wavread.py
  wavwrite.py
  mp3read.py
  mp3write.py
  wmaread.py
  wmawrite.py
 Effects/
  __init__.py
  
echo.py
  surround.py
  reverse.py
}}}

只有当__init__.py存在时python才将该文件夹视为一个包,该文件可以为空文件
一般在__init__.py文件中定义一个__all__表,包含要import *时要导入的模块.
如Sound/Effects/__init__.py可以有如下内容

`__all__=["echo","surround","reverse"]`
        __init__.py
        
Formats/
                __init__.py
                
wavread.py
                wavwrite.py
                mp3read.py
                mp3write.py
                wmaread.py
                wmawrite.py
        Effects/
                __init__.py
                
echo.py
                surround.py
                reverse.py
}}}
只有当__init__.py存在时python才将该文件夹视为一个包,该文件可以为空文件 一般在__init__.py文件中定义一个__all__表,包含要import *时要导入的模块. 如Sound/Effects/__init__.py可以有如下内容

{{{__all__=["echo","surround","reverse"]}}}
Line 440: Line 352:
`form 包.子包 imort 模块`

简写为
`
imort 模块`
{{{form 包.子包 imort 模块}}}

简写为 {{{imort 模块}}}
Line 448: Line 358:
for x in xrange(1,11): 
    print repr(x).rjust(2),repr(x*x).rjust(3) 
    #repr是将变量类型转换为可以被编译器处理的文字格式 
for x in xrange(1,11):
    print repr(x).rjust(2),repr(x*x).rjust(3)
    #repr是将变量类型转换为可以被编译器处理的文字格式
Line 452: Line 362:
以截断输出;center为居中 
    #zfill()可以向数值表达式的左侧填充0 
以截断输出;center为居中
    #zfill()可以向数值表达式的左侧填充0
Line 455: Line 365:
Line 457: Line 366:
  {{{
for x in xrange(1,11): 
    print '%2d %3d' % (x,x*x)  #%2d和%3d之间要有空格,d表示为整数
#%10s 表示用str转化为字符串 
#小数输出如 %5.3f  }}}

对于字典可以用变量名来直接格式化,如: 
{{{
>>>table={'Sjoerd':4127,'Jack':4098,'Dcab':8637678} 
>>>print 'Jack:%(Jack)d; Sjoerd:%(Sjoerd)d; Dcab:%(Dcab)d' % 
table 
Jack:4098; Sjoerd:4127; Dcab:8637678  }}}
 

同时,函数vars()返回包含所有变量的字典,配合使用,无坚不摧!         

=== 读写文件:  ===

 

f=open('/tmp/hello','w') 

#open(路径+文件名,读写模式) 
{{{
for x in xrange(1,11):
    print '%2d %3d' % (x,x*x)
#%10s 表示用str转化为字符串
#小数输出如 %5.3f
}}}

对于字典可以用变量名来直接格式化,如:

{{{
>>>table={'Sjoerd':4127,'Jack':4098,'Dcab':8637678}
>>>print 'Jack:%(Jack)d; Sjoerd:%(Sjoerd)d; Dcab:%(Dcab)d' %
table
Jack:4098; Sjoerd:4127; Dcab:8637678
}}}

同时,函数vars()返回包含所有变量的字典,配合使用,无坚不摧!

=== 读写文件: ===
f=open('/tmp/hello','w')

#open(路径+文件名,读写模式)
Line 493: Line 389:
如:'rb','wb','r+b'等等    

f.read([size]) size未指定则返回整个文件,如果文件大小>2倍内存则有问题.f.read()读到文件尾时返回""(空字串) 

file.readline() 返回一行 

file.readline([size]) 返回包含size行的列表,size 未指定则返回全部行    

for line in f: #交换通道       print line     
f.write("hello\n") #如果要写入字符串以外的数据,先将他转换为字符串.    

f.tell() 返回一个整数,表示当前文件指针的位置(就是到文件头的比特数).    

f.seek(偏移量,[起始位置]) 

用来移动文件指针 

偏移量:单位:比特,可正可负 

起始位置:0-文件头,默认值;1-当前位置;2-文件尾    

f.close() 关闭文件      ----------------------------------------------------------

 
如:'rb','wb','r+b'等等

f.read([size]) size未指定则返回整个文件,如果文件大小>2倍内存则有问题.f.read()读到文件尾时返回""(空字串)

file.readline() 返回一行

file.readline([size]) 返回包含size行的列表,size 未指定则返回全部行

for line in f: #交换通道

 .
print line
f.write("hello\n") #如果要写入字符串以外的数据,先将他转换为字符串.

f.tell() 返回一个整数,表示当前文件指针的位置(就是到文件头的比特数).

f.seek(偏移量,[起始位置])

用来移动文件指针

偏移量:单位:比特,可正可负

起始位置:0-文件头,默认值;1-当前位置;2-文件尾

f.close() 关闭文件

----------
Line 537: Line 417:
pickle 序列化/保存对象/封装    

pickle.dump(x,f) #把文件对象f保存到x变量中 

x=pickle.load(f) #还原这个对象         ----------------------------------------------------------

 

try:       ..............

except 异常类型: 

#如果有多个异常类型,可以将他们放在括号()中 

#如except(RuntimeError,TypeError,NameError):       ...............

 

最后一个异常名可以省略异常类型,作为通配项将所有异常pass.慎用!!!    

在except后可以包含一个else    

raise可以抛出异常,同时可以附带异常参数 

try:       rasie Exception('message1','message2')      #只有一个参数时也可以这样写rasie Exception,'message1'  
except Exception,inst:       #inst是该异常类的一个实例

      
print inst.args      #打印出参数名          print inst #直接打印__str__属性

      
x,y=inst

   
print 'x=',x      print 'y=',y     
如果抛出异常没有指定参数,则捕获异常时的参数包含的是该异常的默认信息 

>>>try:               1/0         except ZeroDivisionError,detail:               print 'Handling run-time error:',detail     
Handling run-time error:integer division or modulo by zero      ----------------------------------------------------------

 
pickle 序列化/保存对象/封装

pickle.dump(x,f) #把文件对象f保存到x变量中

x=pickle.load(f) #还原这个对象

----------

try:

 . .............

except 异常类型:

#如果有多个异常类型,可以将他们放在括号()中

#如except(RuntimeError,TypeError,NameError):

 . ..............

最后一个异常名可以省略异常类型,作为通配项将所有异常pass.慎用!!!

在except后可以包含一个else

raise可以抛出异常,同时可以附带异常参数

try:

 .
rasie Exception('message1','message2') #只有一个参数时也可以这样写rasie Exception,'message1'
except Exception,inst:

 .
#inst是该异常类的一个实例 print inst.args #打印出参数名
print inst #直接打印__str__属性 x,y=inst print 'x=',x print 'y=',y
如果抛出异常没有指定参数,则捕获异常时的参数包含的是该异常的默认信息

>>>try:

 .
1/0
 .
except ZeroDivisionError,detail:
  .
print 'Handling run-time error:',detail
Handling run-time error:integer division or modulo by zero

----------
Line 629: Line 459:
   class MyError(Exception):       def __init__(self,value):          self.value=value       def __str__(self):          return reper(self.value)      try:

   
raise MyError(2,2)  
except MyError,e:       print 'My exeception occurred,value',e.value    
>>> 

My exeception occurred,value 4      ----------------------------------------------------------

 

finally:和C++中类似,即使是break,continue或return后一样会执行。一般用于释放资源,如文件,网络连接。    

def divide(x,y):       try:

      
result=x/y      except ZeroDivisionError:          print "除以0"        else:

       print '结果是',result

    finally:

       print "计算完成!"

 

----------------------------------------------------------

 

with #with可以帮助你自动释放资源    

with open('myfile.txt') as f:       for line in f:

       print line

#该文件会自动被释放      ----------------------------------------------------------

class MyError(Exception):

 .
def __init__(self,value):
  .
self.value=value
def __str__(self):
  .
return reper(self.value)
try:

 .
raise MyError(2,2)
except MyError,e:

 .
print 'My exeception occurred,value',e.value
>>>

My exeception occurred,value 4

----------

finally:和C++中类似,即使是break,continue或return后一样会执行。一般用于释放资源,如文件,网络连接。

def divide(x,y):

 . try:
  . try:
   .
result=x/y
except ZeroDivisionError:
   .
print "zero"
  else:
   . print 'result=',result
 finally:
  . print 'finish'
----------

with #with可以帮助你自动释放资源,下一个版本可用

with open('myfile.txt') as f:

 . for line in f:
  . print line

#该文件会自动被释放

----------
Line 708: Line 501:

   
class ClassName:       "类文档,可以通过类名.__doc__访问"             #类的私有变量是至少以双下划线开头,最多以单下划线结尾的类变量,调用时会变量名会被混淆成 _ClassName__变量名

    __
i=12345

         
def f(self)          return "hello world"

       
def __init(self)__:

     
"构造函数,可以初始化变量,可以有参数"

     
#可以通过self调用当前类的函数和数据

     
self.data=[]


class ClassName:

 .
"类文档,可以通过类名.__doc__访问"
#类的私有变量是至少以双下划线开头,最多以单下划线结尾的类变量,调用时会变量名会被混淆成 _ClassName__变量名 __
 
i=12345 def f(self)
  .
return "hello world"
 . def __init(self)__:
  . "构造函数,可以初始化变量,可以有参数" #可以通过self调用当前类的函数和数据 self.data=[]
Line 743: Line 513:

Line 749: Line 517:


----------------------------------------------------------

----------
Line 756: Line 520:


Line 761: Line 522:
 ....................................

 
 . ...................................
Line 769: Line 527:

Line 777: Line 533:


----------------------------------------------------------
----------
Line 785: Line 538:


class DerivedClassName:(Base1,Base2,Base3):
class DerivedClassName(Base1,Base2,Base3):
Line 791: Line 542:

Line 795: Line 544:
   ----------------------------------------------------------

----------
Line 802: Line 547:


Line 807: Line 549:
 print element

98 print element
 . print element
Line 813: Line 552:
 print key
 . print key
Line 817: Line 555:
 print line


 . print line
Line 823: Line 558:


用Generators(生成器)可以快速定义for...in循环.
用Generators(生成器)可以快速生成一个序列,在调用next()时依次返回序列中的值.
Line 829: Line 562:
 for index in range(len(data)-1,-1,-1):

  
yield data[index]
 . for index in range(len(data)-1,-1,-1):
  . yield data[index]
Line 835: Line 566:


----------------------------------------------------------
----------
Line 841: Line 569:
生成器表达式:类似于没有中括号的表推导式,可用在参数中

生成器表达式:类似于没有中括号的表推导式,可用在参数中
Line 849: Line 575:

Line 857: Line 581:
 260


 . 260
Line 865: Line 586:

Line 869: Line 588:

Line 877: Line 594:


----------------------------------------------------------

----------
Line 884: Line 597:


dir(模块) #来获取模块的函数/变量列表
dir(模块) #来获取模块的函数/变量列表
Line 890: Line 601:


----------------------------------------------------------
----------
Line 896: Line 604:
 与操作系统相关的函数


 . 与操作系统相关的函数
Line 906: Line 611:
os.chidr() #改变当前工作目录



----------------------------------------------------------

 
os.chidr()      #改变当前工作目录

----------
Line 916: Line 616:
 目录和文件管理的接口


 . 目录和文件管理的接口
Line 924: Line 621:
shutil.copyfile('data.txt','archive.txt') 

shutil.move('/build/a.txt','b.txt') 

----------------------------------------------------------

 
shutil.copyfile('data.txt','archive.txt')

shutil.move('/build/a.txt','b.txt')

----------
Line 936: Line 630:

Line 946: Line 638:


----------------------------------------------------------


----------
Line 956: Line 643:

Line 972: Line 657:

Line 978: Line 661:

Line 986: Line 667:


----------------------------------------------------------

 
----------
Line 996: Line 672:

Line 1006: Line 680:

Line 1012: Line 684:


----------------------------------------------------------


----------
Line 1022: Line 689:

Line 1034: Line 699:
   ----------------------------------------------------------


----------
Line 1044: Line 704:

Line 1054: Line 712:


>>> random.sample(xrange(100),10) #随机值不会重复
>>> random.sample(xrange(100),10)       #随机值不会重复
Line 1060: Line 716:

Line 1066: Line 720:

Line 1072: Line 724:


----------------------------------------------------------

 
----------
Line 1082: Line 729:

Line 1088: Line 733:
 print line



----------------------------------------------------------


 . print line
----------
Line 1100: Line 739:

Line 1106: Line 743:
sever.sendmail('[email protected]','[email protected]') sever.sendmail(' [email protected] ',' [email protected] ')
Line 1116: Line 753:


----------------------------------------------------------

 
----------
Line 1126: Line 758:
  
Line 1138: Line 768:

Line 1144: Line 772:

Line 1154: Line 780:


----------------------------------------------------------


----------
Line 1164: Line 785:
  
Line 1172: Line 791:
 t=zlib.compress(s)
 . t=zlib.compress(s)
Line 1190: Line 808:
   ----------------------------------------------------------

 
----------
Line 1200: Line 813:

Line 1218: Line 829:

Line 1222: Line 831:


----------------------------------------------------------

 
----------
Line 1234: Line 838:

Line 1240: Line 842:
 """Computer average
 . """Computer average
Line 1244: Line 845:
>>> print average([10,90,53])  >>> print average([10,90,53])
Line 1248: Line 849:
 """

 
return sum(values,0.0)/len(values)


 . """ return sum(values,0.0)/len(values)
Line 1258: Line 854:
----------------------------------------------------------
----------
Line 1264: Line 859:

Line 1272: Line 865:


 
def test_average(self):

  
self.assertEqual(average([20,30,70]),40.0)

  
self.assertEqual(round([1,5,7]),1)

  
self.assertRaises(ZeroDivisionError,average,[])

  
self.assertRaises(TypeError,average,20,30,70)


 . def test_average(self):
  . self.assertEqual(average([20,30,70]),40.0) self.assertEqual(round([1,5,7]),1)
  self.assertRaises(ZeroDivisionError,average,[])
  self.assertRaises(TypeError,average,20,30,70)
Line 1288: Line 871:


----------------------------------------------------------


----------
Line 1296: Line 874:

Line 1300: Line 876:

Line 1304: Line 878:

Line 1308: Line 880:

Line 1312: Line 882:

Line 1316: Line 884:


----------------------------------------------------------


----------
Line 1326: Line 889:

Line 1338: Line 899:
  ['green', 'red'],

 
'yellow'],

'while']



----------------------------------------------------------


 . ['green', 'red'], 'yellow'],
 . 'while']
----------
Line 1354: Line 906:

Line 1360: Line 910:

Line 1364: Line 912:

Line 1386: Line 932:


----------------------------------------------------------

 
----------
Line 1396: Line 937:

Line 1410: Line 949:

Line 1418: Line 955:
----------------------------------------------------------
----------
Line 1424: Line 960:

Line 1436: Line 970:

Line 1442: Line 974:


----------------------------------------------------------


----------
Line 1452: Line 979:

Line 1458: Line 983:

Line 1466: Line 989:
 start+=14

 
fields=struct.unpack('LLLHH',data[start:start+16])

 
crc32,comp_size,uncompsize,filenamesize,extra_size=fields



 
start+=16

 
filename=data[start:start+filenamesize]



 start+=filenamesize

 
extra=data[start:start+extra_size]



 
print filename,hex(crc32),comp_size,uncomp_size

    start+=extra_size+comp_size
#下一个头文件



----------------------------------------------------------

 
 . start+=14 fields=struct.unpack('LLLHH',data[start:start+16]) crc32,comp_size,uncompsize,filenamesize,extra_size=fields start+=16 filename=data[start:start+filenamesize] start+=filenamesize extra=data[start:start+extra_size] print filename,hex(crc32),comp_size,uncomp_size start+=extra_size+comp_size #下一个头文件
----------
Line 1502: Line 995:

Line 1510: Line 1001:
 def __init__(self , infile , outfile):

  self.infile=infile

  self.outfile=outfile



 
def run(self):

  
f=zipfile.ZipFile(self.outfile , 'w' , zipfile.DEFLATED)

  
f.write(self.infile)

  
f.close()

  
print 'Finished background zip of: ', self.infile


 . def __init__(self , infile , outfile):
  . self.infile=infile self.outfile=outfile
 def run(self):
  . f=zipfile.ZipFile(self.outfile , 'w' , zipfile.DEFLATED) f.write(self.infile) f.close() print 'Finished background zip of: ', self.infile
Line 1532: Line 1007:

Line 1538: Line 1011:


background.join() #Wait for the background task finish
background.join()       #Wait for the background task finish
Line 1544: Line 1015:
----------------------------------------------------------



 模块:Queue

 协多线程的资源调用



----------------------------------------------------------

 
----------
 . 模块:Queue 协多线程的资源调用
----------
Line 1562: Line 1022:

Line 1570: Line 1028:

Line 1574: Line 1030:

Line 1578: Line 1032:

Line 1582: Line 1034:

Line 1586: Line 1036:

Line 1596: Line 1044:


----------------------------------------------------------

 
----------
Line 1606: Line 1049:

Line 1616: Line 1057:
... self.value=value ...     self.value=value
Line 1620: Line 1061:
... return str(self.value) ...     return str(self.value)
Line 1624: Line 1065:
>>>a=A(10) #create a reference >>>a=A(10)      #create a reference
Line 1628: Line 1069:
>>>d[’primary’]=a #does not create a reference

>>>d[’primary’] #fetch the object if it is still alive
>>>d[’primary’]=a       #does not create a reference

>>>d[’primary’] #fetch the object if it is still alive
Line 1634: Line 1075:
>>>del a #remove the one reference

>>>gc.collect()  #run garbage collection right away
>>>del a        #remove the one reference

>>>gc.collect()  #run garbage collection right away
Line 1640: Line 1081:
>>>d[’primary’] #entry was automatically removed >>>d[’primary’] #entry was automatically removed
Line 1646: Line 1087:
d[’primary’] #entry was automatically removed d[’primary’]    #entry was automatically removed
Line 1654: Line 1095:


----------------------------------------------------------



模块:array

类似表的对象,比默认的表更紧凑,仅用来存储数据



例:

from array import array
----------
. 模块:array 类似表的对象,比默认的表更紧凑,仅用来存储数据 例: from array import array
Line 1680: Line 1107:
----------------------------------------------------------

 
----------
Line 1686: Line 1110:
提供了类似表的deque对象,它从左边添加(append)和弹出(pop)更快,但在内部查询更慢.适用于队列的实现和广度优先树的搜索

提供了类似表的deque对象,它从左边添加(append)和弹出(pop)更快,但在内部查询更慢.适用于队列的实现和广度优先树的搜索
Line 1702: Line 1124:

Line 1708: Line 1128:
 node=unsearch.popleaf()

 
for m in gen_moves(node):

  
if is_goal(m):

   
return m

  
unsearched.append(m)

----------------------------------------------------------

 
 . node=unsearch.popleaf() for m in gen_moves(node):
  . if is_goal(m):
   . return m
  unsearched.append(m)
----------
Line 1724: Line 1135:
操作存储

操作存储
Line 1740: Line 1149:


----------------------------------------------------------

 
----------
Line 1748: Line 1152:
提供基于正常表的堆实现,最小总是第一个元素(0点),对希望循环访问最小元素但不想执行完整堆排列非常有用

提供基于正常表的堆实现,最小总是第一个元素(0点),对希望循环访问最小元素但不想执行完整堆排列非常有用
Line 1764: Line 1166:

Line 1770: Line 1170:


----------------------------------------------------------



 模块:decimal
----------
 . 模块:decimal
Line 1780: Line 1174:

Line 1786: Line 1178:

Line 1792: Line 1182:

Line 1798: Line 1186:

Line 1804: Line 1190:

Line 1810: Line 1194:

Line 1816: Line 1198:

Line 1822: Line 1202:

Line 1830: Line 1208:
----------------------------------------------------------

 
----------

Python 绝对简明手册 -- [email protected] ::-- ZoomQuiet [DateTime(2006-09-15T04:35:33Z)] TableOfContents

1. Py2.5 绝对简明手册

简述

现在学的东西很容易忘记,写这篇文章的目的是能让我在需要时快速找回当时的感觉. 需要深入的请下载 attachment:diveintopython%2Dpdf%2D%35.%34.zip

  • -- For Python2.5

版本:0.2 beta
作者:张沈鹏  电子科大大三  生物医学工程
参考:python2.5中文教程
感谢:Zoom.Quiet limodou

[http://blog.csdn.net/zuroc 我的技术Blog] [http://www.cppblog.com/zuroc 我的C++Blog] [http://www.cnweblog.com/zuroc/ 我的文学Blog]

1.1. 语法

1.1.1. if

   1 x=int(raw_input("Please enter an integer:"))
   2 if x<0:
   3     print 'Negative Number'
   4 elif x==0:
   5     print 'Zero'
   6 else:
   7     print 'Positive Number'

1.1.2. for

   1 a=['cat','door','example']
   2 for x in a:
   3     print x

#如果要修改a的内容,则用a的副本循环,如:

for x in a[:] :
    .....................

   1 >>>range(10,0,-3)
   2 [10,7,4,1]
   3 a=['cat','door','example']
   4 for i in range(len(a)):
   5     print i,a[i]

break,continue 用法和C++中类似

1.1.3. pass

while True:
    pass #忽略,什么也不做

   1 def fib(n=1000):#参数可以有默认值,多个可选参数赋值可以直接写"参数变量名=值"来快速赋值
   2     """这里给函数写文档注释"""
   3     a,b=0,1
   4     while b<n:
   5        print b
   6        a,b=b,a+b

#函数可以重命名,如

   1 f=fib
   2 f(223)

1.1.4. in

  •    1 if 'yes' in  ('y','ye','yes'):print  'ok'
    

1.1.5. 参数格式 **para

#参数格式为 **para 表示接受一个字典,为 *para 表示接受一个元组

   1 def test(para1,*args,**dic):
   2     print para1
   3     for arg in args : print arg
   4     keys=dic.keys()
   5     keys.sort()
   6     for key in keys:print key ,':',dic[key]

1.1.6. Lambda函数

  •    1 def make_incrementor(n):
       2     return lambda x: x+n
       3 f=make_incrementor(n)
       4 >>>f(0)
       5 42
       6 >>>f(1)
       7 43
    

1.1.7. List的函数

append(x)    追加到链尾
extend(L)    追加一个列表
insert(i,x)   在位置i插入x
remove(x)   删除第一个值为x的元素,如果不存在会抛出异常
pop([i])   返回并删除位置为i的元素,i未给定时默认作用在最后一个元素.[i]表示i为可选的
index(x)    返回第一个值为x的元素,不存在则抛出异常
count(x)   返回x出现的次数
sort()   排序
reverse()   翻转,反转

filter(function函数 , sequence序列)  返回sequence中使filer为true的

map(function,sequence,[sequence...])  返回新的sequence,序列中值为对每个元素分别调用function.

reduce(function,sequence,[init])  返回一个单值为,计算步骤为 :

  • 第1个结果=function(sequence[0],sequence[1])
  • 第2个结果=function(第1个结果,sequence[2])
  • 返回最后一个计算得值
  • 如果有init,则先调用

function(init,sequence[0])  sequence只有一个元素时,返回该元素,为空时抛出异常.

1.1.8. 列表推导式

freshfruit=['  banana  ','   loganberry  ']
>>>[weapon.strip() for weapon in freshfruit]
['banana','loganberry']
vec=[2,4,6]
>>>[3*x for x in vec if x>3]
[12,18]
>>>[(x,x**2) for x in vec]    #一个元素一定要是一个sequence,而
[x,x**2 for x in vec]是错误的
[(2,4),(4,16),(6,36)]
vec2=[4,3,-9]
[x*y for x in vec for y in vec2]
[vec[i]+vec2[i] for i in range(len(vec))]
[str(round(355/113.0,i)) for i in range(1,6)] #str()是转换类型为可以打印的字符

1.1.9. del

   1 a=[1,2,3,4,5,6]
   2 del a[0]
   3 >>>a
   4 [2,3,4,5,6]
   5 del a[2:4]
   6 >>>a
   7 [2,3,6]
   8 del a[:]
   9 >>>a
  10 []
  11 del a
  12 >>>a

抛出异常

1.1.10. 元组

t=1234,5567,'hello'
x,y,z=t    #拆分操作可以应用于所有sequence
>>>x
1234
u=t,(1,2,3)
>>>u
((1234,5567,'hello'),(1,2,3))
empty=() #空元组
singleton='hi', #单个元素的元组

1.1.11. set

set(集合):无序不重复的元素集

   1 basket = ['apple','orange','apple','pear','apple','banana']
   2 fruit=set(basket)
   3 >>>fruit
   4 set(['orange', 'pear', 'apple', 'banana'])
   5 >>>'orange' in fruit
   6 True
   7 a=set('abracadabew')
   8 >>>a
   9 set(['a', 'c', 'b', 'e', 'd', 'r', 'w'])
  10 b=set('wajgwaoihwb')
  11 >>> b
  12 set(['a', 'b', 'g', 'i', 'h', 'j', 'o', 'w'])
  13 >>> a-b    #差
  14 set(['c', 'r', 'e', 'd'])
  15 >>> a|b   #并
  16 set(['a', 'c', 'b', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r', 'w'])
  17 >>> a&b   #交
  18 set(['a', 'b', 'w'])
  19 >>>a^b   #(并-交)
  20 set(['c', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r'])

1.1.12. dict

字典:关键字为不可变类型,如字符串,整数,只包含不可变对象的元组.列表等不可以作为关键字.如果列表中存在关键字对,可以用dict()直接构造字典.而这样的列表对通常是由列表推导式生成的.

   1 tel={'jack':4098,'sape':4139}
   2 tel['guido']=4127
   3 >>> tel
   4 {'sape': 4139, 'jack': 4098, 'guido': 4127}
   5 >>>tel['jack']
   6 4098
   7 del tel['sape']
   8 >>>tel.keys()
   9 ['jack', 'guido']
  10 >>>tel.has_key('jack')
  11 True
  12 knight={'gallahad':'the pure','robin':'the brave'}
  13 for k,v in knight.iteritems():
  14     print k,v

输出:

   1 gallahad the pure
   2 robin the brave

enumerate()返回索引位置和对应的值

   1 for i,v in enumerate(['tic','tac','toe'])
   2     print i,v

输出: 0 tic

1 tac

2 toe

1.1.13. zip

zip用于多个sequence的循环

   1 questions=['name','quest','favorite color']
   2 answers=['lancelot','the holy grail','blue']
   3 for q,a in zip(questions,answers):
   4     print 'What is your %s ? It is %s.'%(q,a)

输出:

What is your name ? It is lancelot.
What is your quest ? It is the holy grail.
What is your favorite color ? It is blue.

1.1.14. reversed反向循环

   1 for i in reversed(range(1,4)):
   2     print i

输出:

3
2
1

1.1.15. sorted排序

1.1.16. sequence比大小

list<string<tuple(因为字母表中l在s前...)

同类比大小按照字典序

1.1.17. 导入模块

模块的查找路径

1.当前的目录

2.环境变量PYTHONPATH所指的目录列表

3.python解释器的安装目录

如将代码保存上述的一个目录中的的fibo.py文件中,便可以

   1 import fibo
   2 fibo.function().............

如果想直接使用fibo.function可以重命名这个函数,如

   1 f=fibo.function
   2 f()

也可以

   1 form fibo import function
   2 function()

甚至可以form fibo import * 

可以 form 包.子包.模块 imort 函数 

然后就直接使用该函数,不需要加前缀

1.1.18. 包

引用推荐写法为

form 包 import 模块

几个功能类似的模块可以组合成一个包,

比如一个可以处理.wav,.mp3,.wma等音频文件的有类似如下结构:

Sound/
        __init__.py
        Formats/
                __init__.py
                wavread.py
                wavwrite.py
                mp3read.py
                mp3write.py
                wmaread.py
                wmawrite.py
        Effects/
                __init__.py
                echo.py
                surround.py
                reverse.py

只有当init.py存在时python才将该文件夹视为一个包,该文件可以为空文件 一般在init.py文件中定义一个all列表,包含要import *时要导入的模块. 如Sound/Effects/init.py可以有如下内容

__all__=["echo","surround","reverse"]

包的作者在发布包时可以更新这个列表,也可以根据需要让某个模块不支持import *

对于包中同一个文件夹下的模块可以把

form 包.子包 imort 模块

简写为 imort 模块

1.1.19. 格式化输出

for x in xrange(1,11):
    print repr(x).rjust(2),repr(x*x).rjust(3)
    #repr是将变量类型转换为可以被编译器处理的文字格式
    #rjust是调整宽度为参数个字符,r表示右对齐;ljust为左对齐,ljust(n)[:n]可
以截断输出;center为居中
    #zfill()可以向数值表达式的左侧填充0

1.1.20. 等效代码

for x in xrange(1,11):
    print '%2d %3d' % (x,x*x)
#%10s 表示用str转化为字符串
#小数输出如   %5.3f

对于字典可以用变量名来直接格式化,如:

>>>table={'Sjoerd':4127,'Jack':4098,'Dcab':8637678}
>>>print 'Jack:%(Jack)d; Sjoerd:%(Sjoerd)d; Dcab:%(Dcab)d' %
table
Jack:4098; Sjoerd:4127; Dcab:8637678

同时,函数vars()返回包含所有变量的字典,配合使用,无坚不摧!

1.1.21. 读写文件:

f=open('/tmp/hello','w')

#open(路径+文件名,读写模式)

#读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式

如:'rb','wb','r+b'等等

f.read([size]) size未指定则返回整个文件,如果文件大小>2倍内存则有问题.f.read()读到文件尾时返回""(空字串)

file.readline() 返回一行

file.readline([size]) 返回包含size行的列表,size 未指定则返回全部行

for line in f: #交换通道

  • print line

f.write("hello\n") #如果要写入字符串以外的数据,先将他转换为字符串.

f.tell() 返回一个整数,表示当前文件指针的位置(就是到文件头的比特数).

f.seek(偏移量,[起始位置])

用来移动文件指针

偏移量:单位:比特,可正可负

起始位置:0-文件头,默认值;1-当前位置;2-文件尾

f.close() 关闭文件


1.1.22. pickle

pickle 序列化/保存对象/封装

pickle.dump(x,f) #把文件对象f保存到x变量中

x=pickle.load(f) #还原这个对象


try:

  • .............

except 异常类型:

#如果有多个异常类型,可以将他们放在括号()中

#如except(RuntimeError,TypeError,NameError):

  • ..............

最后一个异常名可以省略异常类型,作为通配项将所有异常pass.慎用!!!

在except后可以包含一个else

raise可以抛出异常,同时可以附带异常参数

try:

  • rasie Exception('message1','message2') #只有一个参数时也可以这样写rasie Exception,'message1'

except Exception,inst:

  • #inst是该异常类的一个实例 print inst.args #打印出参数名

    print inst #直接打印str属性 x,y=inst print 'x=',x print 'y=',y

如果抛出异常没有指定参数,则捕获异常时的参数包含的是该异常的默认信息

>>>try:

Handling run-time error:integer division or modulo by zero


自定义异常:惯例是以Error结尾的类,同类的异常一般派生自同一个基类,基类异常可以匹配派生类异常

class MyError(Exception):

  • def init(self,value):

    • self.value=value

    def str(self):

    • return reper(self.value)

try:

except MyError,e:

  • print 'My exeception occurred,value',e.value

>>>

My exeception occurred,value 4


finally:和C++中类似,即使是break,continue或return后一样会执行。一般用于释放资源,如文件,网络连接。

def divide(x,y):

  • try: finally:
    • print 'finish'


with #with可以帮助你自动释放资源,下一个版本可用

with open('myfile.txt') as f:

  • for line in f:
    • print line

#该文件会自动被释放


1.1.23. 初识类

class ClassName:

  • "类文档,可以通过类名.doc访问" #类的私有变量是至少以双下划线开头,最多以单下划线结尾的类变量,调用时会变量名会被混淆成 _ClassName变量名 i=12345 def f(self)

    • return "hello world"
  • def init(self):

    • "构造函数,可以初始化变量,可以有参数" #可以通过self调用当前类的函数和数据 self.data=[]

#创建类实例

x=ClassName()

#给类的方法重命名

xf=ClassName.f


1.1.23.1. 类继承

class DerivedClassName(BassClassName):

  • ...................................

如果基类定义在另一个模块中, 要写成

modname.BaseClassName

派生类的函数会覆盖基类的同名函数,如果想扩充而不是改写基类的函数,可以这样调用基类函数

BaseClassName.methodname(self,arguments)

注意:该基类要在当前全局域或被导入


1.1.23.2. 多重继承

类多继承//小心使用

class DerivedClassName(Base1,Base2,Base3):

.................

对于一个函数的解析规则是深度优先,先是Base1,然后是Base1的基类,诸如此类.


1.1.24. Iterators 迭代器

for element in [1,2,3]:

  • print element

for key in {'one':1,'two':2}:

  • print key

for line in open("my.txt"):

  • print line

在容器中定义next()逐一返回元素,并在迭代完成时抛出StopIteration异常,然后定义iter()返回self,便可以for...in循环

用Generators(生成器)可以快速生成一个序列,在调用next()时依次返回序列中的值.

def reverse(data):

  • for index in range(len(data)-1,-1,-1):
    • yield data[index]

#yield会自动创建next()和iter(),每次调用next()时yield会进行下一步循环,并在yield处返回值


1.1.25. 生成器表达式

生成器表达式:类似于没有中括号的列表推导式,可用在参数中

>>>sum(i*i for i in range(10))

285

>>>xvec=[10,20,30]

>>>yvec=[7,5,3]

>>>sum(x*y for x,y in zip(xvec,yvec))

  • 260

>>>from math import pi,sin

>>>sine_table=dict((x,sin(x*pi/180) for x in range(1,91))

>>>unique_words=set(word for line in page for word in line.split())

>>>data='golf'

>>>list(data[i] for i in range(len (data)-1,-1,-1))

['f','l','o','g']


1.2. 常用函数不完全手册

dir(模块) #来获取模块的函数/变量列表

help(模块/函数) #获取相关的帮助


模块:os

  • 与操作系统相关的函数

例:

import os

os.getcwd() #当前脚本的工作目录

os.chidr() #改变当前工作目录


模块:shutil

  • 目录和文件管理的接口

例:

import shutil

shutil.copyfile('data.txt','archive.txt')

shutil.move('/build/a.txt','b.txt')


模块:glob

生成文件列表,支持通配符

例:

import glob

>>>glob.glob('*.py')

['primes.py','random.py','quote.py']


模块:sys

提供命令行参数,错误输出重定向和脚本终止

例:

命令行参数

如执行python demo.py one,two,three后

import sys

print sys.argv

会输出

['demo.py','one','two','three']

终止脚本

sys.exit()

错误输出重定向,可以在stdout被重定向时显示错误信息

>>>sys.stderr.write('Warning , log file not found starting a new one\n')

Warning , log file not found starting a new one


模块:re

字符正值表达式匹配

例:

import re

>>>re.findall(r'\bf[a-z]*','which foot or hand fell fastest')

['foot','fell','fastest']

>>>re.sub(r'(\b[a-z]+)\l',r'\l','cat in the hat')

'cat in the hat


模块:math

为浮点运算提供了底层C函数库的访问

例:

>>>math.cos(math.pi/4.0)

0.70710678118654757

>>>math.log(1024,2)

10.0


模块:random

生成随机数

例:

import random

>>>random.choice(['apple','pear','banana'])

'apple'

>>> random.sample(xrange(100),10) #随机值不会重复

[20,42,12,44,57,88,93,80,75,56]

>>>random.random()

0.26676389968666669

>>> random.randrange(10)

7


模块:urblib2

打开url地址

例:

for line in urllib2.urlopen('http:\\www.python.org\')

  • print line


模块:smtplib

发送电子邮件

例:

sever=smtplib.smtp('localhost')

sever.sendmail(' [email protected] ',' [email protected] ')

"""TO:[email protected]

From:[email protected]

"""

sever.quit()


模块:datetime

时间日期相关算法以及格式化输出

例:

from datetime import date

now=date.today

>>>now

datetime.date(2006, 9, 13)

>>>now.strftime("%m-%d-%y . %d %b %Y is a %A on the %d day of %B . ")

'09-13-06 . 13 Sep 2006 is a Wednesday on the 13 day of September . '

birthday=date(1986,6,30)

age=now-birthday

>>> age.days

7380


模块:zipfile / tarfile

数据打包和压缩,支持格式:zlib,gzip,bz2,zipfile和tarfile

例:

import zlib

s="which which which which"

  • t=zlib.compress(s)

>>>len(s)

23

>>>len(t)

16

>>>zlib.decompress(t)

"which which which which"

>>>zilb.crc(32)

-487390043


模块:timeit

性能测试

例:

from timeit import Timer

#Timer的第一个参数是要测试时间的语句,第二个参数是初始化

#timeit的参数是测试语句的执行次数,默认执行1000000次

>>> Timer('t=a;a=b;b=t','a=1;b=2').timeit()

0.31399409701512582

>>> Timer('a,b=b,a','a=1;b=2').timeit()

0.247945758469313663

模块:profile和pstats提供了对更大代码块的测量工具


模块:doctest

质量测试,测试代码.

他将对函数文档中的测试代码进行测试,他使文档的撰写和软件的测试融合了起来

例:

def average(values):

  • """Computer average

#注意>>> 和测试代码之间要空一格,测试结果前面不要有空格

>>> print average([10,90,53])

51.0

  • """ return sum(values,0.0)/len(values)

import doctest

doctest.testmod()


模块:unittest

可以在一个独立的文件中提供一个更全面的代码测试.

例:

import unittest

class TestStatisticalFunctions(unittest.TestCase):

  • def test_average(self):
    • self.assertEqual(average([20,30,70]),40.0) self.assertEqual(round([1,5,7]),1)

      self.assertRaises(ZeroDivisionError,average,[]) self.assertRaises(TypeError,average,20,30,70)

unittest.main()


其他一些常用模块

xmlrpclib和SimpleXMLRPCServer可以在琐碎的任务中调用远程过程

email可以构建和解析复杂的消息结构,包括附件,文字编码和头协议等等

xml.dom和xml.sax

csv通用数据库中直接读写

gettext,locale,codecs国际化(i18n)支持


模块:pprint

美化打印(pretty printer)

例:

import pprint

t=[[ ['blue','cyan'] ,['green','red'],'yellow' ],'while']

>>> pprint.pprint(t,width=30)

[[['blue', 'cyan'],

  • ['green', 'red'], 'yellow'],
  • 'while']


模块:textwrap

格式化段落来适应行宽

例:

import textwrap

doc="""Object for wrapping/filling text. The public interface consists of the wrap() and fill() methods; the other methods are just there for subclasses to override in order to tweak the default behaviour. If you want to completely replace the main wrapping algorithm, you'll probably have to override _wrap_chunks()."""

>>> print textwrap.fill(doc,width=40)

Object for wrapping/filling text. The

public interface consists of the wrap()

and fill() methods; the other methods

are just there for subclasses to

override in order to tweak the default

behaviour. If you want to completely

replace the main wrapping algorithm,

you'll probably have to override

_wrap_chunks().


模块:locale

国际化

例:

import locale

locale.setlocale(locale.LC_ALL,'English_United States.1252')

x=1234567.8

>>>locale.format("%d",x,grouping=True)

'1,234,567'

conv=locale.localeconv()

>>> locale.format("%s%.*f",(conv['currency_symbol'],conv['frac_digits'],x),grouping=True)

'$1,234,567.80'


模块:string.template

生成句子的模版,输入"张沈鹏",生成"大家好,我的名字是张沈鹏."

例:

from string import Template

t=Template('${village}flok send $$10 to $cause')

t.substitute(village='Nottingham',cause='the ditch fund')

'Nottinghamflok send $10 to the ditch fund'

当占位符没有提供时substitute会抛出KeyError的异常

而safe_substitute可以在占位符提供不完整时保留占位符,而不抛出异常


模块:struct

用于读取二进制格式的文件

例:#H 代表unsigned short , read(2)因为unsigned short占2个bytes

#L 代表4字节的无符号整数

data=open('myfile.zip','rb').read()

start=0

for i in xrange(3):

  • start+=14 fields=struct.unpack('LLLHH',data[start:start+16]) crc32,comp_size,uncompsize,filenamesize,extra_size=fields start+=16 filename=data[start:start+filenamesize] start+=filenamesize extra=data[start:start+extra_size] print filename,hex(crc32),comp_size,uncomp_size start+=extra_size+comp_size #下一个头文件


模块:threading

线程

例:

import threading,zipfile

class AsyncZip(threading.Thread)

  • def init(self , infile , outfile):

    • self.infile=infile self.outfile=outfile
    def run(self):
    • f=zipfile.ZipFile(self.outfile , 'w' , zipfile.DEFLATED) f.write(self.infile) f.close() print 'Finished background zip of: ', self.infile

background=AsyncZip('mydata.txt','myarchive.zip')

background.start()

print 'The main program continues to run in foreground'

background.join() #Wait for the background task finish

print 'Main program waitwd until background was done .'


  • 模块:Queue 协多线程的资源调用


模块:logging

日志

例:

import logging

logging.debug('Debugging information')

logging.info('Information message')

logging.warning('Warning:coinfig file %s not found','server.conf')

logging.error('Error occurred')

logging.critical('Critical error -- shutting down')

输出:

WARNING:root:Warning:config file server.conf not found

ERROR:root:Error occurred

CRITICAL:root:Critical error -- shutting down


模块:wearef

不创建引用来跟踪对象

例:

>>>import weakref,gc

>>>class A :

...def init (self,value):

... self.value=value

...def repr(self):

... return str(self.value)

...

>>>a=A(10) #create a reference

>>>d=weakref.WeakValueDictionary()

>>>d[’primary’]=a #does not create a reference

>>>d[’primary’] #fetch the object if it is still alive

10

>>>del a #remove the one reference

>>>gc.collect() #run garbage collection right away

0

>>>d[’primary’] #entry was automatically removed

Traceback(mostrecentcalllast):

File "<pyshell#108>",line1,in-toplevel-

d[’primary’] #entry was automatically removed

File "C:/PY24/lib/weakref.py" , line46 , in getitem

o = self.data[key]()

KeyError:’primary’


  • 模块:array 类似列表的对象,比默认的列表更紧凑,仅用来存储数据 例: from array import array

#存储双字节无符号整数,编码类型为H a=array('H',[4000,10,700,22222])

>>> sum(a)

26932

>>>a[1:3]

array('H',[10,700])


模块:collections

提供了类似列表的deque对象,它从左边添加(append)和弹出(pop)更快,但在内部查询更慢.适用于队列的实现和广度优先树的搜索

例:

from collection import deque

d=deque["task1","task2","task3"]

d.append("task4")

>>>print "Handling",d.popleft()

Handling task1

unsearched=deque([start_nodel])

def breadth_first_search(unsearched):

  • node=unsearch.popleaf() for m in gen_moves(node):
    • if is_goal(m):
      • return m
      unsearched.append(m)


模块:bisect

操作存储列表

例:

import bisect

scores=[(100,'perl'),(200,'tcl'),(400,'lua'),(500,'python')]

bisect.insort(scores,(300,'ruby'))

>>>scores

[(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]


模块:heapq

提供基于正常列表的堆实现,最小总是第一个元素(0点),对希望循环访问最小元素但不想执行完整堆排列非常有用

例:

from heapq import heapify,heappop,heappush

data=[1,3,5,6,7,8,9,65,0]

heapify(data)

heappush(data,-5)

[heappop(data) for i in range(3) ]

>>> [heappop(data) for i in range(3) ]

[-5, 0, 1]


  • 模块:decimal

提供了一个Decimal数据类型,用于浮点数高精度的计算,高精度使Decimal可以执行二进制浮点数无法进行的模运算和等值测试

例:

from decimal import *

>>>Decimal('0.70')*Decimal('1.05'))

0.7350

>>> .70*1.05

0.73499999999999999

>>>sum([Decimal('0.1')*10==Decimal('1.0')])

True

>>>sum([0.1*10])==1.0

False

>>>Decimal('1.00')%Decimal('.10')

Decimal("0.00")

>>> 1.00%0.10

0.09999999999999995

getcontext().prec=36

>>>print Decimal(1)/Decimal(7)

0.142857142857142857142857142857142857


2. 反馈

PyAbsolutelyZipManual (last edited 2009-12-25 07:09:10 by localhost)