在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ 標(biāo)準(zhǔn)庫(kù)概覽Ⅱ
浮點(diǎn)運(yùn)算
非正式的 Python 簡(jiǎn)介
深入流程控制
標(biāo)準(zhǔn)庫(kù)概覽
交互式輸入行編輯歷史回溯
輸入和輸出
使用 Python 解釋器
標(biāo)準(zhǔn)庫(kù)概覽Ⅱ
錯(cuò)誤和異常
模塊
數(shù)據(jù)結(jié)構(gòu)
附錄
接下來?
激起你的興趣

標(biāo)準(zhǔn)庫(kù)概覽Ⅱ

第二部分包含了支持專業(yè)編程工作所需的更高級(jí)的模塊,這些模塊很少出現(xiàn)在小腳本中。

輸出格式

reprlib?模塊為大型的或深度嵌套的容器縮寫顯示提供了?repr()?函數(shù)的一個(gè)定制版本:

>>> import reprlib  
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))  
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])" 

pprint?模塊給老手提供了一種解釋器可讀的方式深入控制內(nèi)置和用戶自定義對(duì)象的打印。當(dāng)輸出超過一行的時(shí)候,“美化打?。╬retty printer)”添加斷行和標(biāo)識(shí)符,使得數(shù)據(jù)結(jié)構(gòu)顯示的更清晰:

>>> import pprint  
>>> t = [[[['black', 'cyan'], 'white', ['green', 'red']],   [['magenta',  
...      'yellow'], 'blue']]]  
...  
>>> pprint.pprint(t, width=30)  
[[[['black', 'cyan'],  
   'white',  
   ['green', 'red']],  
  [['magenta', 'yellow'],  
   'blue']]]  

textwrap?模塊格式化文本段落以適應(yīng)設(shè)定的屏寬:

>>> import textwrap  
>>> doc = """The wrap() method is just like fill() except that it returns  
... a list of strings instead of one big string with newlines to separate  
... the wrapped lines."""  
...  
>>> print(textwrap.fill(doc, width=40))  
The wrap() method is just like fill()  
except that it returns a list of strings
instead of one big string with   newlines
to separate the wrapped lines.     

locale?模塊按訪問預(yù)定好的國(guó)家信息數(shù)據(jù)庫(kù)。locale 的格式化函數(shù)屬性集提供了一個(gè)直接方式以分組標(biāo)示格式化數(shù)字:

>>> import locale  
>>> locale.setlocale(locale.LC_ALL, 'English_United  States.1252')
'English_United States.1252'  
>>> conv = locale.localeconv()         # get a mapping of conventions  
>>> x = 1234567.8  
>>> locale.format("%d", x, grouping=True)  
'1,234,567'  
>>> locale.format_string("%s%.*f", (conv['currency_symbol'],  
...                       conv['frac_digits'], x), grouping=True)  
'$1,234,567.80'  

模板

string?提供了一個(gè)靈活多變的模版類?Template?,使用它最終用戶可以用簡(jiǎn)單的進(jìn)行編輯。這使用戶可以在不進(jìn)行改變的情況下定制他們的應(yīng)用程序。

格式使用?$?為開頭的 Python 合法標(biāo)識(shí)(數(shù)字、字母和下劃線)作為占位符。占位符外面的大括號(hào)使它可以和其它的字符不加空格混在一起。$$?創(chuàng)建一個(gè)單獨(dú)的?$:

>>> from string import Template  
>>> t = Template('${village}folk send $$10 to $cause.')  
>>> t.substitute(village='Nottingham', cause='the ditch fund')  
'Nottinghamfolk send $10 to the ditch fund.'  

當(dāng)一個(gè)占位符在字典或關(guān)鍵字參數(shù)中沒有被提供時(shí),?substitute()?方法就會(huì)拋出一個(gè)?KeyError?異常。 對(duì)于郵件合并風(fēng)格的應(yīng)用程序,用戶提供的數(shù)據(jù)可能并不完整,這時(shí)使用?safe_substitute()?方法可能更適合 — 如果數(shù)據(jù)不完整,它就不會(huì)改變占位符:

>>> t = Template('Return the $item to $owner.')  
>>> d = dict(item='unladen swallow')  
>>> t.substitute(d)
Traceback (most recent call last):  
    ...
  KeyError: 'owner'  
>>> t.safe_substitute(d)
'Return the unladen swallow to $owner.'  

模板子類可以指定一個(gè)自定義分隔符。 例如,圖像查看器的批量重命名工具可能選擇使用百分號(hào)作為占位符,像當(dāng)前日期,圖片序列號(hào)或文件格式:

>>> import time, os.path  
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']  
>>> class BatchRename(Template):  
...      Delimiter = '%'  
>>> fmt = input('Enter rename style (%d-date %n-seqnum %f-format):  ')    
Enter rename style (%d-date %n-seqnum %f-format):  Ashley_%n%f

>>> t = BatchRename(fmt)  
>>> date = time.strftime('%d%b%y')  
>>> for i, filename in enumerate(photofiles):  
...     base, ext = os.path.splitext(filename)    
...     newname = t.substitute(d=date, n=i, f=ext)  
...     print('{0} --> {1}'.format(filename, newname)) 

img_1074.jpg --> Ashley_0.jpg  
img_1076.jpg --> Ashley_1.jpg  
img_1077.jpg --> Ashley_2.jpg  

模板的另一個(gè)應(yīng)用是把多樣的輸出格式細(xì)節(jié)從程序邏輯中分類出來。這便使得 XML 文件,純文本報(bào)表和 HTML WEB 報(bào)表定制模板成為可能。

使用二進(jìn)制數(shù)據(jù)記錄布局

struct?模塊為使用變長(zhǎng)的二進(jìn)制記錄格式提供了?pack()?和?unpack()?函數(shù)。 下面的示例演示了在不使用?zipfile?模塊的情況下如何迭代一個(gè) ZIP 文件的頭信息。 壓縮碼?"H"?和?"I"?分別表示 2 和 4 字節(jié)無符號(hào)數(shù)字,?"<"?表明它們都是標(biāo)準(zhǔn)大小并且按照 little-endian 字節(jié)排序。

import struct

with open('myfile.zip', 'rb') as f:      
    data = f.read()

start = 0  
for i in range(3):                     # show the first 3 file headers   
    start += 14  
    fields = struct.unpack('<IIIHH', data[start:start+16])     
    crc32, comp_size, uncomp_size, 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      # skip to the next header  

多線程

線程是一個(gè)分離無順序依賴關(guān)系任務(wù)的技術(shù)。在某些任務(wù)運(yùn)行于后臺(tái)的時(shí)候應(yīng)用程序會(huì)變得遲緩,線程可以提升其速度。一個(gè)有關(guān)的用途是在 I/O 的同時(shí)其它線程可以并行計(jì)算。

下面的代碼顯示了高級(jí)模塊?threading?如何在主程序運(yùn)行的同時(shí)運(yùn)行任務(wù):

import threading, zipfile 

class AsyncZip(threading.Thread):  
    def __init__(self, infile, outfile):  
        threading.Thread.__init__(self)  
        self.infile = infile  
        self.outfile = outfile  
    def run(self):  
        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_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 to finish  
print('Main program waited until background was done.')  

多線程應(yīng)用程序的主要挑戰(zhàn)是協(xié)調(diào)線程,諸如線程間共享數(shù)據(jù)或其它資源。 為了達(dá)到那個(gè)目的,線程模塊提供了許多同步化的原生支持,包括:鎖,事件,條件變量和信號(hào)燈。

盡管這些工具很強(qiáng)大,微小的設(shè)計(jì)錯(cuò)誤也可能造成難以挽回的故障。 因此,任務(wù)協(xié)調(diào)的首選方法是把對(duì)一個(gè)資源的所有訪問集中在一個(gè)單獨(dú)的線程中,然后使用?queue?模塊用那個(gè)線程服務(wù)其他線程的請(qǐng)求。 為內(nèi)部線程通信和協(xié)調(diào)而使用?Queue?對(duì)象的應(yīng)用程序更易于設(shè)計(jì),更可讀,并且更可靠。

日志

logging?模塊提供了完整和靈活的日志系統(tǒng)。它最簡(jiǎn)單的用法是記錄信息并發(fā)送到一個(gè)文件或?sys.stderr:

import logging
logging.debug('Debugging information')  
logging.info('Informational message')
logging.warning('Warning:config 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

默認(rèn)情況下捕獲信息和調(diào)試消息并將輸出發(fā)送到標(biāo)準(zhǔn)錯(cuò)誤流。其它可選的路由信息方式通過 email,數(shù)據(jù)報(bào)文,socket 或者 HTTP Server?;谙傩?,新的過濾器可以選擇不同的路由:DEBUG,?INFO,?WARNING,ERROR, 和?CRITICAL?。

日志系統(tǒng)可以直接在 Python 代碼中定制,也可以不經(jīng)過應(yīng)用程序直接在一個(gè)用戶可編輯的配置文件中加載。

弱引用

Python 自動(dòng)進(jìn)行內(nèi)存管理(對(duì)大多數(shù)的對(duì)象進(jìn)行引用計(jì)數(shù)和垃圾回收——?garbage collection?——以循環(huán)利用)在最后一個(gè)引用消失后,內(nèi)存會(huì)很快釋放。

這個(gè)工作方式對(duì)大多數(shù)應(yīng)用程序工作良好,但是偶爾會(huì)需要跟蹤對(duì)象來做一些事。不幸的是,僅僅為跟蹤它們創(chuàng)建引用也會(huì)使其長(zhǎng)期存在。weakref?模塊提供了不用創(chuàng)建引用的跟蹤對(duì)象工具,一旦對(duì)象不再存在,它自動(dòng)從弱引用表上刪除并觸發(fā)回調(diào)。典型的應(yīng)用包括捕獲難以構(gòu)造的對(duì)象:

>>> 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  (most recent call last):
  File "<stdin>", line 1, in <module>    
     d['primary']               # entry was automatically removed  
  File "C:/python34/lib/weakref.py", line 46, in __getitem__  
    O = self.data[key]()  
KeyError: 'primary' 

列表工具

很多數(shù)據(jù)結(jié)構(gòu)可能會(huì)用到內(nèi)置列表類型。然而,有時(shí)可能需要不同性能代價(jià)的實(shí)現(xiàn)。

array模塊提供了一個(gè)類似列表的?array()?對(duì)象,它僅僅是存儲(chǔ)數(shù)據(jù),更為緊湊。以下的示例演示了一個(gè)存儲(chǔ)雙字節(jié)無符號(hào)整數(shù)的數(shù)組(類型編碼?"H"?)而非存儲(chǔ) 16 字節(jié) Python 整數(shù)對(duì)象的普通正規(guī)列表:

>>> from array import array  
>>> a = array('H', [4000, 10, 700, 22222])  
>>> sum(a)  
26932 
>>> a[1:3]  
array('H', [10, 700])  

collections?模塊提供了類似列表的?deque()?對(duì)象,它從左邊添加(append)和彈出(pop)更快,但是在內(nèi)部查詢更慢。這些對(duì)象更適用于隊(duì)列實(shí)現(xiàn)和廣度優(yōu)先的樹搜索:

>>> from collections import deque  
>>> d = deque(["task1", "task2", "task3"])  
>>> d.append("task4")  
>>> print("Handling", d.popleft())  
Handling task1
unsearched = deque([starting_node]) 
def breadth_first_search(unsearched):  
    node = unsearched.popleft()      
    for m in gen_moves(node):    
        if is_goal(m):      
            return m  
        unsearched.append(m)  

除了鏈表的替代實(shí)現(xiàn),該庫(kù)還提供了?bisect?這樣的模塊以操作存儲(chǔ)鏈表:

>> 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?提供了基于正規(guī)鏈表的堆實(shí)現(xiàn)。最小的值總是保持在 0 點(diǎn)。這在希望循環(huán)訪問最小元素但是不想執(zhí)行完整堆排序的時(shí)候非常有用:

>>> from heapq import heapify, heappop, heappush  
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]  
>>> heapify(data)                      # rearrange the list into heap order  
>>> heappush(data, -5)                 # add a new entry  
>>> [heappop(data) for i in range(3)]  # fetch the three smallest entries  
[-5, 0, 1]  

十進(jìn)制浮點(diǎn)數(shù)算法

decimal?模塊提供了一個(gè)?Decimal?數(shù)據(jù)類型用于浮點(diǎn)數(shù)計(jì)算。相比內(nèi)置的二進(jìn)制浮點(diǎn)數(shù)實(shí)現(xiàn)?float?,這個(gè)類型有助于

  • 金融應(yīng)用和其它需要精確十進(jìn)制表達(dá)的場(chǎng)合,
  • 控制精度,
  • 控制舍入以適應(yīng)法律或者規(guī)定要求,
  • 確保十進(jìn)制數(shù)位精度,或者
  • 用戶希望計(jì)算結(jié)果與手算相符的場(chǎng)合。

例如,計(jì)算 70 分電話費(fèi)的 5% 稅計(jì)算,十進(jìn)制浮點(diǎn)數(shù)和二進(jìn)制浮點(diǎn)數(shù)計(jì)算結(jié)果的差別如下。如果在分值上舍入,這個(gè)差別就很重要了:

>>> from decimal import *  
>>> round(Decimal('0.70') * Decimal('1.05'), 2)  
Decimal('0.74')  
>>> round(.70 * 1.05, 2)  
0.73  

Decimal?的結(jié)果總是保有結(jié)尾的 0,自動(dòng)從兩位精度延伸到4位。 Decimal 重現(xiàn)了手工的數(shù)學(xué)運(yùn)算,這就確保了二進(jìn)制浮點(diǎn)數(shù)無法精確保有的數(shù)據(jù)精度。 高精度使 Decimal 可以執(zhí)行二進(jìn)制浮點(diǎn)數(shù)無法進(jìn)行的模運(yùn)算和等值測(cè)試:

>>> Decimal('1.00') % Decimal('.10')  
Decimal('0.00')  
>>> 1.00 % 0.10  
0.09999999999999995  

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

decimal?提供了必須的高精度算法:

>>> getcontext().prec = 36  
>>> Decimal(1) / Decimal(7)  
Decimal('0.142857142857142857142857142857142857')