- 論壇徽章:
- 0
|
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
§10 模塊
§10.1 模塊簡(jiǎn)介
查找路徑的設(shè)置:
>>> import sys
>>>
sys.path.append('c:/python')
unix只能以絕對(duì)路徑或者形式添加:sys.path.expanduser('~/python').
導(dǎo)入只會(huì)生效一次。
要強(qiáng)制重新加載:
>>>
hello = reload(hello)
Hello,
world!
3.0不會(huì)再有reload功能,可以使用exec,建議最好不要涉及到reload類似的功能。
查看系統(tǒng)路徑:
>>>
import sys, pprint
>>>
pprint.pprint(sys.path)
pprint可以很好地處理較大的數(shù)據(jù)。
模塊一般放在site-packages中。
可以修改環(huán)境變量:PYTHONPATH.--這個(gè)在我的windows好像沒有找到,估計(jì)IDE有自己的設(shè)置方法。也可以在.pth文中設(shè)置的
模塊以.py結(jié)尾,windows下還可以.pyw。
多個(gè)模塊可以組合成包,它是包含__init__.py的目錄。
§10.2 探索模塊
>>>
import copy
查看:dir(copy
>>>
[n for n in dir(copy) if not n.startswith('_')]
['Error',
'PyStringMap', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't']
可以調(diào)用的查看方式:
copy.__all__
獲取幫助:help(copy.copy),跟如下的效果是一樣的。
查看源文件的位置:>>> print copy.__file__
C:\Python26\lib\copy.pyc
一些python內(nèi)置的庫(kù),比如sys;一些用c語言書寫的庫(kù),是不能這樣查看的。
§10.3 標(biāo)準(zhǔn)庫(kù)介紹
sys 模塊提供了許多函數(shù)和變量來處理 Python 運(yùn)行時(shí)環(huán)境的不同部分. 重要的函數(shù)有:argv,exit([arg]),modules,path,platform,stdin,stdout,stderr等。
os 模塊:這個(gè)模塊中的大部分函數(shù)通過對(duì)應(yīng)平臺(tái)相關(guān)模塊實(shí)現(xiàn), 比如 posix 和 nt. os 模塊會(huì)在第一次導(dǎo)入的時(shí)候自動(dòng)加載合適的執(zhí)行模塊.重要的函數(shù)有environ,system,sep,pathsep,linesep,urandom(n)。
environ用于修改環(huán)境變量;
system執(zhí)行外部程序,其他的類似命令有execv,會(huì)退出python,popen打開類似于文件的連接,不過subprocess模塊集合了他們所有的功能;
os.sep用于路徑中的分隔符,unix中為斜杠,windows中是2個(gè)反斜杠。
os.pathsep則為path路徑的分隔符;
os.linesep為行分隔符;
urandom:返回加密的隨機(jī)數(shù)
比如打開火狐:unix中,os.system('/usr/bin/firefox');windows中os.system(r'c:\"Program Files"\"Mozilla
Firefox"\firefox.exe')。加引號(hào)是為了防止不解釋空格。也可以使用windows特有的函數(shù):os.startfile(r'c:\Program
Files\Mozilla Firefox\firefox.exe')。windows程序與os.system (or os.startfile)同時(shí)運(yùn)行,UNIX中python會(huì)等待os.system完成。其實(shí)打開網(wǎng)頁(yè)還有更好的方法:
import webbrowser
webbrowser.open('http://www.python.org')
fileinput
模塊允許你循環(huán)一個(gè)或多個(gè)文本文件的內(nèi)容。重要的函數(shù)有:input([files[, inplace[,
backup]]),filename(),lineno()。filelineno(),isfirstline(),isstdin(),nextfile(),close()
注意fileinput.lineno是多個(gè)文件累積的計(jì)數(shù)。文本文件的替換操作很簡(jiǎn)單. 只需要把 inplace 關(guān)鍵字參數(shù)設(shè)置為 1 , 傳遞給 input 函數(shù), 該模塊會(huì)幫你做好一切.
heaps(堆),對(duì)應(yīng)的模塊為heapq。函數(shù)有6個(gè):heappush(heap, x),heappop(heap),heapify(heap),heapreplace(heap, x),nlargest(n,
iter),nsmallest(n, iter)。
>>>
from heapq import *
>>>
from random import shuffle
>>>
data = range(10)
>>>
shuffle(data)
>>>
heap = []
>>>
for n in data:
...
heappush(heap, n)
>>>
heap
[0, 1,
3, 6, 2, 8, 4, 7, 9, 5]
>>>
heappush(heap, 0.5)
>>>
heap
[0, 0.5,
3, 6, 1, 8, 4, 7, 9, 5, 2]
i總大于i // 2,小于2*i and 2*i + 1).
>>> heap
[0.5, 1,
5, 3, 2, 7, 9, 8, 4, 6]
>>>
heapreplace(heap, 10)
0.5
>>>
heap
[1, 2,
5, 3, 6, 7, 9, 8, 4, 10]
雙端隊(duì)列和其他集合:
>>>
from collections import deque
>>>
q = deque(range(5))
>>>
q.append(5)
>>>
q.appendleft(6)
>>>
q
deque([6,
0, 1, 2, 3, 4, 5])
>>>
q.pop()
5
>>>
q.popleft()
6
>>>
q.rotate(3)
>>>
q
deque([2,
3, 4, 0, 1])
>>>
q.rotate(-1)
>>>
q
deque([3,
4, 0, 1, 2])
另外還有extend和extend方法
其他模塊待下次介紹。
sys 模塊提供了許多函數(shù)和變量來處理 Python 運(yùn)行時(shí)環(huán)境的不同部分. 重要的函數(shù)有:argv,exit([arg]),modules,path,platform,stdin,stdout,stderr等。
os 模塊:這個(gè)模塊中的大部分函數(shù)通過對(duì)應(yīng)平臺(tái)相關(guān)模塊實(shí)現(xiàn), 比如 posix 和 nt. os 模塊會(huì)在第一次導(dǎo)入的時(shí)候自動(dòng)加載合適的執(zhí)行模塊.重要的函數(shù)有environ,system,sep,pathsep,linesep,urandom(n)。
environ用于修改環(huán)境變量;
system執(zhí)行外部程序,其他的類似命令有execv,會(huì)退出python,popen打開類似于文件的連接,不過subprocess模塊集合了他們所有的功能;
os.sep用于路徑中的分隔符,unix中為斜杠,windows中是2個(gè)反斜杠。
os.pathsep則為path路徑的分隔符;
os.linesep為行分隔符;
urandom:返回加密的隨機(jī)數(shù)
比如打開火狐:unix中,os.system('/usr/bin/firefox');windows中os.system(r'c:\"Program Files"\"Mozilla
Firefox"\firefox.exe')。加引號(hào)是為了防止不解釋空格。也可以使用windows特有的函數(shù):os.startfile(r'c:\Program
Files\Mozilla Firefox\firefox.exe')。windows程序與os.system (or os.startfile)同時(shí)運(yùn)行,UNIX中python會(huì)等待os.system完成。其實(shí)打開網(wǎng)頁(yè)還有更好的方法:
import webbrowser
webbrowser.open('http://www.python.org')
fileinput
模塊允許你循環(huán)一個(gè)或多個(gè)文本文件的內(nèi)容。重要的函數(shù)有:input([files[, inplace[,
backup]]),filename(),lineno()。filelineno(),isfirstline(),isstdin(),nextfile(),close()
注意fileinput.lineno是多個(gè)文件累積的計(jì)數(shù)。文本文件的替換操作很簡(jiǎn)單. 只需要把 inplace 關(guān)鍵字參數(shù)設(shè)置為 1 , 傳遞給 input 函數(shù), 該模塊會(huì)幫你做好一切.
heaps(堆),對(duì)應(yīng)的模塊為heapq。函數(shù)有6個(gè):heappush(heap, x),heappop(heap),heapify(heap),heapreplace(heap, x),nlargest(n,
iter),nsmallest(n, iter)。
>>>
from heapq import *
>>>
from random import shuffle
>>>
data = range(10)
>>>
shuffle(data)
>>>
heap = []
>>>
for n in data:
...
heappush(heap, n)
>>>
heap
[0, 1,
3, 6, 2, 8, 4, 7, 9, 5]
>>>
heappush(heap, 0.5)
>>>
heap
[0, 0.5,
3, 6, 1, 8, 4, 7, 9, 5, 2]
i總大于i // 2,小于2*i and 2*i + 1).
>>> heap
[0.5, 1,
5, 3, 2, 7, 9, 8, 4, 6]
>>>
heapreplace(heap, 10)
0.5
>>>
heap
[1, 2,
5, 3, 6, 7, 9, 8, 4, 10]
雙端隊(duì)列和其他集合:
>>>
from collections import deque
>>>
q = deque(range(5))
>>>
q.append(5)
>>>
q.appendleft(6)
>>>
q
deque([6,
0, 1, 2, 3, 4, 5])
>>>
q.pop()
5
>>>
q.popleft()
6
>>>
q.rotate(3)
>>>
q
deque([2,
3, 4, 0, 1])
>>>
q.rotate(-1)
>>>
q
deque([3,
4, 0, 1, 2])
另外還有extend和extend方法
本文來自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/21908/showart_2042546.html |
|