Python3.2官方文档翻译-标准库概览(二)


7.5 字符串模式匹配

re模块为高级字符串成处理提供了正则表达式匹配。 对于复杂的匹配和处理,正则表达式能够提供简明优化的方法:

>>> import re

>>> re.findall(r’\bf[a-z]*’, ’which foot or hand fell fastest’)

[’foot’, ’fell’, ’fastest’]

>>> re.sub(r’(\b[a-z]+) \1’, r’\1’, ’cat in the the hat’)

’cat in the hat’

当仅仅需要一些简单的功能时候,优先使用string方法,因为它更容易读取和调试。

>>> ’tea for too’.replace(’too’, ’two’)

’tea for two’

7.6 数学

数学模块为浮点数运算提供了对底层C函数库的访问支持。

>>> import math

>>> math.cos(math.pi / 4)

0.70710678118654757

>>> math.log(1024, 2)

10.0

Random模块为生成随机选择提供了工具。

>>> import random

>>> random.choice([’apple’, ’pear’, ’banana’])

’apple’

>>> random.sample(range(100), 10) # sampling without replacement

[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]

>>> random.random() # random float

0.17970987693706186

>>> random.randrange(6) # random integer chosen from range(6)

4

SciPy project 项目中包含许多数据计算的模块。

10.7 互联网访问

Python中有许多访问互联网和处理互联网协议的模块。其中最简单的两个就是从链接中获得数据的urllib.request和发送邮件的smtplib.

>>> from urllib.request import urlopen

>>> for line in urlopen(’http://tycho.usno.navy.mil/cgi-bin/timer.pl’):

... line = line.decode(’utf-8’) # Decoding the binary data to text.

... if ’EST’ in line or ’EDT’ in line: # look for Eastern Time

... print(line)


Nov. 25, 09:43:32 PM EST

>>> import smtplib

>>> server = smtplib.SMTP(’localhost’)

>>> server.sendmail(’soothsayer@example.org’, ’jcaesar@example.org’,

... """To: jcaesar@example.org

... From: soothsayer@example.org

...

... Beware the Ides of March.

... """)

>>> server.quit()

(注意第二个例子需要有一个在本地运行的email邮箱服务器)

7.8 时间和日期

Datatime模块提供一些用简单或复杂方式处理时间和日期的类。当处理日期和时间数据时,格式化输出和处理实现的重点就是高校的成员提取。这个模块同样支持时区处理。

>>> # dates are easily constructed and formatted

>>> from datetime import date

>>> now = date.today()

>>> now

datetime.date(2003, 12, 2)

>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")

’12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.’

>>> # dates support calendar arithmetic

>>> birthday = date(1964, 7, 31)

>>> age = now - birthday

>>> age.days

14368

7.9 数据压缩

Python还支持常用数据的打包和压缩。主要涉及到的模块式zlib,gzip, bz2,zipfile and tarfile.

>>> import zlib

>>> s = b’witch which has which witches wrist watch’

>>> len(s)

41

>>> t = zlib.compress(s)

>>> len(t)

37

>>> zlib.decompress(t)

b’witch which has which witches wrist watch’

>>> zlib.crc32(s)

226805979

7.10 性能评测

一些python使用者对于同一问题的不同解决办法的性能很感兴趣。Python提供了一种评测工具就可以马上回答这些问题。

例如, 当封装参数的时候可以用元组封装和拆封特性来代替传统的方法。Timeit模块中可以迅速描述一个性能优势。

>>> from timeit import Timer

>>> Timer(’t=a; a=b; b=t’, ’a=1; b=2’).timeit()

0.57535828626024577

>>> Timer(’a,b = b,a’, ’a=1; b=2’).timeit()

0.54962537085770791

与timeit的细粒度相比,profile和pstate模块提供了在大代码块中识别时间临界区的工具。

6.11 质量控制

开发高质量的软件的方法之一就是对每个功能写测试用例。在开发过程中频繁地运行这些用例。

Doctest模块提供一个扫描模块和验证嵌套在程序文档字符中的测试。测试编制是简单的把一个典型的调用及它的结果剪切并粘贴到文档字符串里。这通过为用户提供一个实例改善了文档,并且它允许doctext模块确认代码和文档相符。

\

Unittest模块不像doctest模块那么容易使用。但是,它允许一个更加复杂的测试来维护分开文件。

v颹噅O守h燏i晊ギ??Хギ?z晦?テiD櫒??6髆沯乾氌e壓h泎f鷌晊壉歔莕后?建ky数据库格式中直接读写。总之,这些模块和包大大简化了python应用程序和其他工具之间的数据交换。

有若干模块可以实现国际化,包括gettext,local和codecs包。

评论关闭