Python 学习笔记之十二——标准库概览 --第Ⅱ部分

第二部分提供了更高级的模块用来支持专业编程的需要。这些模块很少出现在小型的脚本里。

输出格式

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. 

模版

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

Template 类的子类可以指定自定义的分隔符。例如,图像浏览器的批量命名工具可能选用百分号作为表示当前日期、图像、序列号或文件格式的占位符:

import time,os.path
from string import Template
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):')
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)) 

把上面的代码保存到template.py 中,执行:

python template.py 
#输入Ashley_%n%f 输出如下:
img_1074.jpg --> Ashley_0.jpg 
img_1076.jpg --> Ashley_1.jpg 
img_1077.jpg --> Ashley_2.jpg 

模板的另一个应用是把多样的输出格式细节从程序逻辑中分类出来。这使它能够替代用户的 XML 文件、 纯文本报告和 HTML 网页报表。

二进制数据记录格式

模块struct 提供了查看、记录二进制格式的数据。
A basic example of packing/unpacking three integers:

>>> from struct import *
>>> pack('hhl', 1, 2, 3)
b'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8

pack 和unpack的第一个参数是按照什么类型(类型的不同决定了字节的多少)来转换,这里的h 是integer类型(即相当于C语言中的short短整型),l也是integer类型(相当于C语言中的long类型),字节大小分别是2,2,4,以下是Python中不同类型的字节大小:

FormatC TypePython typeStandard size
xpad byteno value
ccharbytes of length 11
bsigned charinteger1
Bunsigned charinteger1
?_Boolbool1
hshortinteger2
Hunsigned shortinteger2
iintinteger4
Iunsigned intinteger4
llonginteger4
Lunsigned longinteger4
qlong?longinteger8
Qunsigned long longinteger8
nssize_tinteger
Nsize_tinteger
ffloatfloat4
ddoublefloat8
schar[]bytes
pchar[]bytes
Pvoid *integer

具体介绍见 Python struct 介绍

多线程

  线程是一种解耦非顺序依赖任务的技术。线程可以用来提高应用程序的响应速度,而其他任务同时在后台运行。一个相关的使用场景是I/O操作与另一个线程中的计算并行执行。
下面的代码演示在主程序连续运行的同时,threading 模块如何在后台运行任务(thread.py)

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

#运行结果
$python thread_test.py 
The main program continues to run in foreground.
Finished background zip of: filetest.txt
Main program waited until background was done.

多线程应用程序的最主要挑战是是协调线程间共享的数据或其他资源。为此目的,该线程模块提供了许多同步原语包括锁、 事件、 条件变量和信号量。

尽管这些工具很强大,很小的设计错误也可能导致很难复现的问题。因此,任务协调的首选 方法是把对一个资源的所有访问集中在一个单独的线程中,然后使用 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 

默认情况下,信息和调试消息被压缩并输出到标准错误。其他输出选项包括将消息通过email、datagrams、sockets发送,或者发送到 HTTP 服务器。根据消息的优先级,新的过滤器可以选择不同的方式:DEBUG、INFO、WARNING、ERROR和CRITICAL。
日志系统可以直接在 Python 代码中定制,也可以不经过应用程序直接在一个用户可编辑的配置文件中加载。

弱引用

Python 会自动进行内存管理 (对大多数的对象进行引用计数和垃圾回收以循环利用)。在最后一个引用消失后,内存会立即释放。
有时候在程序执行过程中需要跟踪对象,但是跟踪程序也会创建一个引用,并且这个引用会永久保留。weakref模块
提供工具用来无需创建一个引用跟踪对象。当不再需要该对象时,它会自动从 weakref 表中删除并且会为 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:/python34/lib/weakref.py", line 46, in __getitem__ 
    o = self.data[key]() 
KeyError: 'primary' 

列表工具

很多数据结构使用内置列表类型就可以满足需求。然而,有时候需要根据性能需要采用不同的替代工具。
array 模块提供了一个array() 对象,它和list 比较类似,能够存储同一类型的数据,能和列表一样都可以做切片操作。下面的例子是一个数组,存储了一些2字节无符号类型的数据(即Python 类型是”H”):

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

注意这里的 insort方法是插入并排序,并不是单词写错了。
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 的结果总是保有结尾的0,自动从两位精度延伸到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') 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值