11. Python 标准库概览 – Part II

未匹配的标注
本文档最新版为 3.8,旧版本可能放弃维护,推荐阅读最新版!

第二篇概览囊括更多的支持专业编程需求的高级模块。 这些模块很少出现在小型脚本代码中。

11.1. 格式化输出

模块reprlib 提供了一份定制的repr(),用于简洁 地展示各种大的或者多层嵌套的容器变量:

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

 模块pprint以解释器可读的方式提供了更复杂的控制内置或用户自定义对象的打印方式的机制。 当输出结果长于一行时,这个「 漂亮的打印器 」就会通过添加换行符和缩进的方式更清晰地揭示数据的结构:

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

>>> 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使用一份包含地区习俗相关的数据库。localeformat 函数的 grouping 参数提供了一种直接通过分组符来格式化数字的方式:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv()          # 获取一种惯例的映射
>>> 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 模块包含了 丰富的模板 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() 方法--- 当出现数据缺失的时候它会忽略对应的占位符:

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

模板类的子类支持自定义设置占位符。例如,照片浏览器的批量重命名实用程序可以选择百分号作为占位符,当前日期,图像序列号或文件格式都可以这样做:

>>> 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 web文件成为了可能。

11.3. Working with Binary Data Record Layouts

 struct 模块提供了 pack() 和 unpack() 两种功能,以便操作不同长度的二进制记录形式。下面我们将提供一个例子,来演示如何在不使用 zipfile 模块的情况下,通过头文件来进行循环操作。 Pack 代码 "H""I" 分别代表两字节和四字节无符号数字。 "<" 表示这些数据是标准长度并且是按 little-endian 字节排列的。

import struct

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

start = 0
for i in range(3):                      # 显示前三行的头文件
    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     # 跳过下一个头文件

11.4. 多线程

线程化是一种用于对一些没有顺序依赖的任务进行解耦的技术。多线程可以通过在接受用户输入的同时保持其他任务在后台运行,提高应用的响应效率。并行地进行输入输出操作,并在另外一个线程中执行计算就是一个相关的例子。

下面的代码段展示了主程序保持运行的同时, 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('后台解压文件完成:', self.infile)

background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print('主程序持续执行.')

background.join()    # Wait for the background task to finish
print('主程序等待后台任务结束.')

多线程应用中主要的挑战是如何协调共享数据及其他资源的多个线程。threading 模块提供了一些同步的基本单元来解决这个问题,包括锁(lock)、事件(event)、条件变量(condition variable)、信号量(semaphore)。

在多线程应用中,微小的设计错误也能导致难以复现的问题。一种建议的协调多任务的方法为,将所有对同一资源的访问封装到一个线程中,然后在这个线程中使用 队列(queue 处理其他线程的访问请求。在线程间通信中使用 queue 模块中的 Queue 对象可以让应用变得更容易设计,更可读,也更可依赖。

11.5. 日志管理

日志管理模块 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

默认情况下,INFO 和 DEBUG 级别的信息会被抑制,而输出则会被发送到标准错误流。其他的一些日志信息输出路径包括电子邮件,数据报,sockets,甚至一台HTTP 服务器。新的过滤器允许日志系统根据不同的信息级别(DEBUGINFOWARNINGERROR, 和 CRITICAL)选择不同的输出方式。

我们可以直接通过 Python 程序配置日志管理系统,也可以通过从配置文件中读取配置信息进而设置日志系统。显然,配置文件的方式可以避免我们去修改应用程序。

11.6. 弱引用

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

11.7. 处理列表的工具

内置的列表类型可以满足许多数据结构的需要。然而,有时候需要具有不同性能权衡的替代实现。

 array 模块提供了一个 array() 对象,该对象和列表很像,只存储同构数据,并且存储比列表更紧凑。下面的示例说明一个数值数组被存储为两个字节的无符号二进制数 (类型码 「H」) 而不是 Python 整数对象的常规列表中 每个项通常的 16 字节:

>>> 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 模块提供了基于常规列表实现堆的函数。最小值项总位于零位置,这对于重复访问最小元素但不想运行完整列表排序的应用程序非常有用。

>>> from heapq import heapify, heappop, heappush
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
>>> heapify(data)                      # 将列表重新排列为堆顺序
>>> heappush(data, -5)                 # 添加一个新项
>>> [heappop(data) for i in range(3)]  # 获取三个最小的项
[-5, 0, 1]

11.8. 十进制浮点运算

本章是关于 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

Decimal 的结果保留了尾部的零,从两位精确度的被乘数上自动推导出结果的 4 位精确度。Decimal 的数学过程模拟了人工计算的方法,从而避免了二进制浮点运算不能精确表示十进制数量时可能出现的问题。

完全表示法让 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')

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/python-tutorial...

译文地址:https://learnku.com/docs/python-tutorial...

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~