查看全部 Python3 基础教程
概览(二)涵盖了专业编程所需的更高级的模块,这些模块在小型脚本中很少出现。
格式化输出
reprlib 模块提供了简短显示大型或深层嵌套容器的 repr() 定制版本:
>>> import reprlib
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))
"{'a', 'c', 'd', 'e', 'f', 'g', ...}"
pprint 模块提供了更复杂的控制方式,以让内置的和用户定义的对象对解释器更具可读性。当显示的结果超出一行的长度时,“pretty printer” 会增加换行符和缩进来更清楚地显示数据结构:
>>> 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 模块可访问某个具有特定文化的数据格式的数据库。locale
的 format
函数的 grouping
属性提供了一种直接使用组分隔符格式化数字的方式:
>>> 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 模块包括一个通用 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 错误。对于 mail-merge 风格的应用,用户提供的数据可能不完整,所以 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.'
Template
子类可以指定一个自定义的 delimiter。例如,用于照片浏览的批量重命名工具可以对像当前日期、图像序号或文件格式这样的占位符选择使用百分号:
>>> 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 网络报表替换其自定义模板。
使用二进制数据记录格式
struct 模块提供了 pack() 和 unpack() 函数,用于处理不定长度的二进制记录格式。下例展示了在不使用 zipfile 模块的情况下如何循环遍历 ZIP 文件中的头部信息。Pack 代码 "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
多线程
线程技术用于解耦非顺序依赖的任务。对于接受用户输入但同时还有其他任务在后台运行的应用来说,线程可以提高其响应能力。一个相关的用例就是 I/O 和计算任务在两个线程中并行运行。
以下代码展示了在主程序继续运行的情况下,高级 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.')
多线程应用程序的主要挑战是协调共享数据或其他资源的线程。为此,threading
模块提供了许多同步原语,包括锁、事件、条件变量和信号量。
虽然这些工具很强大,但轻微的设计错误就能导致难以复现的问题。因此,协调任务的首选方式是,将所有对资源的访问集中到某一个线程中,然后使用 queue 模块向该线程提供来自其他线程的请求。使用 Queue 对象进行线程间通信和协调的应用程序更容易设计、更具可读性且更可靠。
日志
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 级别的日志,并且输出会发送到标准错误流。其他输出选项包括将消息转发到电子邮件,数据报,套接字或 HTTP 服务器。新的过滤器可以根据消息优先级选择不同的路由方式:DEBUG, INFO, WARNING, ERROR 和 CRITICAL。
日志系统可以直接从 Python 配置,也可以从用户配置文件加载,以便自定义日志记录而无需更改应用程序。
弱引用
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:/python39/lib/weakref.py", line 46, in __getitem__
o = self.data[key]()
KeyError: 'primary'
操作列表的工具
内置列表类型能满足许多对于数据结构的需求,但是,有时需要具有不同性能权衡的替代实现。
array 模块提供了一个 array() 对象,它像于列表一样只能存储同类型的数据且存储得更紧凑。下例演示了一个以两字节为存储单元的无符号二进制数值的数组(类型码为 "H"
),而对于常规列表来说,每个条目是以16 个字节的 Python int
对象来存储:
>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a)
26932
>>> a[1:3]
array('H', [10, 700])
collections 模块提供了一个类似列表的 deque() 对象,其从左侧 append
和 pop
的速度更快,但在中间查找的速度更慢。这些对象很适合实现队列和广度优先树搜索:
>>> 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) # 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]
十进制浮点运算
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
结果会保留尾部的零,并从两个有效位的被乘数自动推断出四个有效位。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')
参考资料
Python3 Tutorial – Brief Tour of the Standard Library — Part II