11. 標(biāo)準(zhǔn)庫(kù)簡(jiǎn)介 —— 第二部分?

第二部分涵蓋了專業(yè)編程所需要的更高級(jí)的模塊。這些模塊很少用在小腳本中。

11.1. 格式化輸出?

reprlib 模塊提供了一個(gè)定制化版本的 repr() 函數(shù),用于縮略顯示大型或深層嵌套的容器對(duì)象:

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

pprint 模塊提供了更加復(fù)雜的打印控制,其輸出的內(nèi)置對(duì)象和用戶自定義對(duì)象能夠被解釋器直接讀取。當(dāng)輸出結(jié)果過長(zhǎng)而需要折行時(shí),“美化輸出機(jī)制”會(huì)添加換行符和縮進(jìn),以更清楚地展示數(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)給定的屏幕寬度:

>>>
>>> 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 模塊處理與特定地域文化相關(guān)的數(shù)據(jù)格式。locale 模塊的 format 函數(shù)包含一個(gè) grouping 屬性,可直接將數(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'

11.2. 模板?

string 模塊包含一個(gè)通用的 Template 類,具有適用于最終用戶的簡(jiǎn)化語(yǔ)法。它允許用戶在不更改應(yīng)用邏輯的情況下定制自己的應(yīng)用。

上述格式化操作是通過占位符實(shí)現(xiàn)的,占位符由 $ 加上合法的 Python 標(biāo)識(shí)符(只能包含字母、數(shù)字和下劃線)構(gòu)成。一旦使用花括號(hào)將占位符括起來,就可以在后面直接跟上更多的字母和數(shù)字而無(wú)需空格分割。$$ 將被轉(zhuǎn)義成單個(gè)字符 $:

>>>
>>> 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.'

如果在字典或關(guān)鍵字參數(shù)中未提供某個(gè)占位符的值,那么 substitute() 方法將拋出 KeyError。對(duì)于郵件合并類型的應(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.'

Template 的子類可以自定義分隔符。例如,以下是某個(gè)照片瀏覽器的批量重命名功能,采用了百分號(hào)作為日期、照片序號(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é)分離開來。這使得對(duì) XML 文件、純文本報(bào)表和 HTML 網(wǎng)絡(luò)報(bào)表使用自定義模板成為可能。

11.3. 使用二進(jìn)制數(shù)據(jù)記錄格式?

struct 模塊提供了 pack()unpack() 函數(shù),用于處理不定長(zhǎng)度的二進(jìn)制記錄格式。下面的例子展示了在不使用 zipfile 模塊的情況下,如何循環(huán)遍歷一個(gè) ZIP 文件的所有頭信息。Pack 代碼 "H""I" 分別代表兩字節(jié)和四字節(jié)無(wú)符號(hào)整數(shù)。"<" 代表它們是標(biāo)準(zhǔn)尺寸的小端字節(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

11.4. 多線程?

線程是一種對(duì)于非順序依賴的多個(gè)任務(wù)進(jìn)行解耦的技術(shù)。多線程可以提高應(yīng)用的響應(yīng)效率,當(dāng)接收用戶輸入的同時(shí),保持其他任務(wù)在后臺(tái)運(yùn)行。一個(gè)有關(guān)的應(yīng)用場(chǎng)景是,將 I/O 和計(jì)算運(yùn)行在兩個(gè)并行的線程中。

以下代碼展示了高階的 threading 模塊如何在后臺(tái)運(yùn)行任務(wù),且不影響主程序的繼續(xù)運(yùn)行:

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)的多個(gè)線程之間需要共享數(shù)據(jù)或其他資源。為此,threading 模塊提供了多個(gè)同步操作原語(yǔ),包括線程鎖、事件、條件變量和信號(hào)量。

盡管這些工具非常強(qiáng)大,但微小的設(shè)計(jì)錯(cuò)誤卻可以導(dǎo)致一些難以復(fù)現(xiàn)的問題。因此,實(shí)現(xiàn)多任務(wù)協(xié)作的首選方法是將所有對(duì)資源的請(qǐng)求集中到一個(gè)線程中,然后使用 queue 模塊向該線程供應(yīng)來自其他線程的請(qǐng)求。 應(yīng)用程序使用 Queue 對(duì)象進(jìn)行線程間通信和協(xié)調(diào),更易于設(shè)計(jì),更易讀,更可靠。

11.5. 日志記錄?

logging 模塊提供功能齊全且靈活的日志記錄系統(tǒng)。在最簡(jiǎn)單的情況下,日志消息被發(fā)送到文件或 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')

這會(huì)產(chǎn)生以下輸出:

WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down

默認(rèn)情況下,informational 和 debugging 消息被壓制,輸出會(huì)發(fā)送到標(biāo)準(zhǔn)錯(cuò)誤流。其他輸出選項(xiàng)包括將消息轉(zhuǎn)發(fā)到電子郵件,數(shù)據(jù)報(bào),套接字或 HTTP 服務(wù)器。新的過濾器可以根據(jù)消息優(yōu)先級(jí)選擇不同的路由方式:DEBUG,INFO,WARNINGERROR,和 CRITICAL。

日志系統(tǒng)可以直接從 Python 配置,也可以從用戶配置文件加載,以便自定義日志記錄而無(wú)需更改應(yīng)用程序。

11.6. 弱引用?

Python 會(huì)自動(dòng)進(jìn)行內(nèi)存管理(對(duì)大多數(shù)對(duì)象進(jìn)行引用計(jì)數(shù)并使用 garbage collection 來清除循環(huán)引用)。 當(dāng)某個(gè)對(duì)象的最后一個(gè)引用被移除后不久就會(huì)釋放其所占用的內(nèi)存。

此方式對(duì)大多數(shù)應(yīng)用來說都適用,但偶爾也必須在對(duì)象持續(xù)被其他對(duì)象所使用時(shí)跟蹤它們。 不幸的是,跟蹤它們將創(chuàng)建一個(gè)會(huì)令其永久化的引用。 weakref 模塊提供的工具可以不必創(chuàng)建引用就能跟蹤對(duì)象。 當(dāng)對(duì)象不再需要時(shí),它將自動(dòng)從一個(gè)弱引用表中被移除,并為弱引用對(duì)象觸發(fā)一個(gè)回調(diào)。 典型應(yīng)用包括對(duì)創(chuàng)建開銷較大的對(duì)象進(jìn)行緩存:

>>>
>>> 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:/python312/lib/weakref.py", line 46, in __getitem__
    o = self.data[key]()
KeyError: 'primary'

11.7. 用于操作列表的工具?

許多對(duì)于數(shù)據(jù)結(jié)構(gòu)的需求可以通過內(nèi)置列表類型來滿足。 但是,有時(shí)也會(huì)需要具有不同效費(fèi)比的替代實(shí)現(xiàn)。

array 模塊提供了一種 array() 對(duì)象,它類似于列表,但只能存儲(chǔ)類型一致的數(shù)據(jù)且存儲(chǔ)密集更高。 下面的例子演示了一個(gè)以兩個(gè)字節(jié)為存儲(chǔ)單元的無(wú)符號(hào)二進(jìn)制數(shù)值的數(shù)組 (類型碼為 "H"),而對(duì)于普通列表來說,每個(gè)條目存儲(chǔ)為標(biāo)準(zhǔn) Python 的 int 對(duì)象通常要占用16 個(gè)字節(jié):

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

collections 模塊提供了一種 deque() 對(duì)象,它類似于列表,但從左端添加和彈出的速度較快,而在中間查找的速度較慢。 此種對(duì)象適用于實(shí)現(xiàn)隊(duì)列和廣度優(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)以外,標(biāo)準(zhǔn)庫(kù)也提供了其他工具,例如 bisect 模塊具有用于操作有序列表的函數(shù):

>>>
>>> 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)堆的函數(shù)。 最小值的條目總是保持在位置零。 這對(duì)于需要重復(fù)訪問最小元素而不希望運(yùn)行完整列表排序的應(yīng)用來說非常有用:

>>>
>>> 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]

11.8. 十進(jìn)制浮點(diǎn)運(yùn)算?

decimal 模塊提供了一種 Decimal 數(shù)據(jù)類型用于十進(jìn)制浮點(diǎn)運(yùn)算。 相比內(nèi)置的 float 二進(jìn)制浮點(diǎn)實(shí)現(xiàn),該類特別適用于

  • 財(cái)務(wù)應(yīng)用和其他需要精確十進(jìn)制表示的用途,

  • 控制精度,

  • 控制四舍五入以滿足法律或監(jiān)管要求,

  • 跟蹤有效小數(shù)位,或

  • 用戶期望結(jié)果與手工完成的計(jì)算相匹配的應(yīng)用程序。

例如,使用十進(jìn)制浮點(diǎn)和二進(jìn)制浮點(diǎn)數(shù)計(jì)算70美分手機(jī)和5%稅的總費(fèi)用,會(huì)產(chǎn)生的不同結(jié)果。如果結(jié)果四舍五入到最接近的分?jǐn)?shù)差異會(huì)更大:

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

Decimal 表示的結(jié)果會(huì)保留尾部的零,并根據(jù)具有兩個(gè)有效位的被乘數(shù)自動(dòng)推出四個(gè)有效位。 Decimal 可以模擬手工運(yùn)算來避免當(dāng)二進(jìn)制浮點(diǎn)數(shù)無(wú)法精確表示十進(jìn)制數(shù)時(shí)會(huì)導(dǎo)致的問題。

精確表示特性使得 Decimal 類能夠執(zhí)行對(duì)于二進(jìn)制浮點(diǎn)數(shù)來說不適用的模運(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 模塊提供了運(yùn)算所需要的足夠精度:

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