[译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

[译]The Python Tutorial#Brief Tour of the Standard Library — Part II

第二部分介绍更多知足专业编程需求的高级模块,这些模块在小型脚本中不多用到。html

11.1 Output Formatting

reprlib模块为大型或者深度嵌套的容器提供了一个定制版本的repr()函数:node

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

pprint模块为内嵌对象或者用户自定义对象以解释器可读方式打印提供了更精细的控制。当打印结果超过一行时,“打印美化器”添加换行符和缩进,清晰显示原有的数据结构:python

>>> 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模块格式化文本段落适应指定屏幕宽度:git

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

local模块访问特定文化数据格式的数据库。区域设置格式函数的分组属性提供了使用组分隔符格式化数字的直接方法:算法

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

string模块包括一个功能强大的Template类,其简化的语法适用于最终用户的编辑。该类容许用户自定义他们的应用,而不用修改应用。数据库

格式化使用由$和有效Python标识符(字母数字字符以及下划线)组成的占位符。占位符外围的花括号容许其后跟更多数字字母字符而无需中间空格。使用$$转义$编程

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

当参数字典或者关键字参数没有提供对应的占位符时,substitute()方法抛出KeyError异常。对于邮件合并风格的应用程序,用户提供的数据可能不彻底,使用safe_substitute()方法更加合适——该方法会保留为匹配的占位符不变:api

>>> 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的子类能够指定自定义的定界符。例如,图片浏览器的批量重命名工具可能使用百分比号做为占位符,如当前日期,图片序列码或者文件格式:数组

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

模板的另外一种应用是将程序逻辑与多种输出格式的细节分开。这样使得自定义模板替换为XML文件,纯文本报告和HTML网络报告成为可能。浏览器

11.3 Working with Binary Data Record Layouts

struct模块提供了用于处理可变长度二进制记录格式的pack以及unpack。如下示例展现如何在没有zipfile的帮助下遍历ZIP文件的头信息。分组代码“H”和“I”分别表示两个和四个字节的无符号数。<表示它们是标准大小和小端字节顺序:

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 Multi-threading

线程是一种解耦无顺序依赖关系任务的技术。线程有助于提升接收用户输入应用的响应速度,而其余任务在后台运行。相关的用例是运行IO的同时在另外一个线程中做耗时计算操做。

如下代码展现高层模块threading如何在主程序运行的同时在后台执行任务:

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

多线程应用主要的挑战是协调共享数据或者其余资源的多个线程。为了实现这个目标,线程模块提供了一系列同步原语,包括锁,时间,条件变量以及信号。

虽然这些工具很强大,可是很小的设计错误也会致使很难重现的问题。所以,任务协做首选的方案是将全部对资源的访问都集中在一个单线程中,而后使用queue模块为这个单线程提供来自其余线程的请求。使用Queue对象进行跨线程通讯和协调的应用程序更容易设计,可读性更好,更可靠。

11.5 Logging

logging模块提供了功能完备且灵活的日志系统。最简单的用法是将日志信息输出到文件或者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

默认状况下,debug和info级别的信息被抑制,输出目的地是标准错误。其余输出选项包括经过邮件,数据报,套接字以及HTTP服务器的路由信息。新的过滤器能够根据不一样的消息优先级选择不一样的路由:DEBUG, INFO, WARNING, ERROR以及CRITICAL

日志系统能够直接从Python配置,也能够从用户可编辑的配置文件加载,以便于自定义日志,而无需修改应用程序。

11.6 Weak References

Python自动进行内存管理(针对大多数对象引用计数以及垃圾回收以消除循环)。对象最后的引用删除后,其内存当即释放。

这种方法适用于大多数应用,可是偶尔只须要跟踪对象便可。不幸的是,仅仅跟踪它们也会建立永久的应用。weakref模块提供了无需建立引用跟踪对象的工具。一旦再也不须要对象时,其自动从弱引用表中删除,并触发回调。典型的应用包括建立成本高的缓存对象:

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

11.7 Tools for Working with Lists

内置列表类型能够知足许多数据结构的需求。然而,有时候须要不一样性能权衡的替代实现。

array模块提供一个array()对象,就像一个只存储同类型数据,而且存储更加紧凑的列表同样。如下示例展现一个数字数组,它以两个字节无符号二进制数(typecode为"H")存储,而不是一般的每一个对象16字节的常规Python整数对象列表:

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

collections模块提供deque()对象,就像一个拥有速度更快的左端添加和移除操做,更慢的中间查找速度的列表同样。这些对象适用于实现队列以及宽度优先树搜索:

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

除了可选的列表实现外,库也提供了其余工具,好比具备操做有序列表函数的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, 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 Decimal Floating Point Arithmetic

decimal模块为十进制浮点算法提供Decimal数据类型。与二进制浮点型的内嵌的float实现相比,这个类在如下场合及其有用:

  • 金融类应用以及其余须要精确十进制表示的应用;
  • 控制精度;
  • 控制舍入以知足法律上或者管控上的需求;
  • 追踪重要的小数位数,或者
  • 用户其余输出与手工计算匹配的应用

例如,计算对70分的电话费取5%的税,十进制浮点型盒二进制浮点型结果不一样。当结果须要舍入到最近的整分时,它们之间的差别就很重要了:

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

十进制结果保持一个尾随的0,精度自从从两位延伸到了四位。十进制能够从新进行数学运算,就像手工同样,而且避免了二进制浮点数不能精确表示十进制数时会产生的问题。

精确表示使得Decimal类能够执行求余计算以及不适用于二进制浮点型的比较测试:

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