Python Standard Library

翻译: Python 江湖群

2008-03-28 13:11:55

Contents

  1. 1. 其他模块
    1. 1.1. 概览
    2. 1.2. pyclbr 模块
        1. 1.2.0.1. Example 14-1. 使用 pyclbr 模块
        2. 1.2.0.2. Example 14-2. 使用 pyclbr 模块读取类和函数
        3. 1.2.0.3. Example 14-3. 使用 pyclbr 模块
    3. 1.3. filecmp 模块
        1. 1.3.0.1. Example 14-4. 使用 filecmp 模块
    4. 1.4. cmd 模块
        1. 1.4.0.1. Example 14-5. 使用 cmd 模块
    5. 1.5. rexec 模块
        1. 1.5.0.1. Example 14-6. 使用 rexec 模块
    6. 1.6. Bastion 模块
        1. 1.6.0.1. Example 14-7. 使用 Bastion 模块
        2. 1.6.0.2. Example 14-8. 使用 Bastion 模块处理非标准过滤器
    7. 1.7. readline 模块
        1. 1.7.0.1. Example 14-9. 使用 readline 模块
    8. 1.8. rlcompleter 模块
        1. 1.8.0.1. Example 14-10. 使用 rlcompleter 模块展开名字
    9. 1.9. statvfs 模块
        1. 1.9.0.1. Example 14-11. 使用 statvfs 模块
    10. 1.10. calendar 模块
        1. 1.10.0.1. Example 14-12. 使用 calendar 模块
        2. 1.10.0.2. Example 14-13. 使用 calendar 模块
    11. 1.11. sched 模块
        1. 1.11.0.1. Example 14-14. 使用 sched 模块
    12. 1.12. statcache 模块
        1. 1.12.0.1. Example 14-15. 使用 statcache 模块
    13. 1.13. grep 模块
        1. 1.13.0.1. Example 14-16. 使用 grep 模块
    14. 1.14. dircache 模块
        1. 1.14.0.1. Example 14-17. 使用 dircache 模块
    15. 1.15. dircmp 模块
        1. 1.15.0.1. Example 14-18. 使用 dircmp 模块
    16. 1.16. cmp 模块
        1. 1.16.0.1. Example 14-19. 使用 cmp 模块
    17. 1.17. cmpcache 模块
        1. 1.17.0.1. Example 14-20. 使用 cmpcache 模块
    18. 1.18. util 模块
        1. 1.18.0.1. Example 14-21. 实现 util 模块的 remove 函数
        2. 1.18.0.2. Example 14-22. 实现 util 模块的 readfile 函数
        3. 1.18.0.3. Example 14-23. 实现 util 模块的 readopenfile 函数
    19. 1.19. soundex 模块
        1. 1.19.0.1. Example 14-24. 使用 soundex 模块
    20. 1.20. timing 模块
        1. 1.20.0.1. Example 14-25. 使用 timing 模块
        2. 1.20.0.2. Example 14-26. 模拟 timing 模块
    21. 1.21. posixfile 模块
        1. 1.21.0.1. Example 14-27. 使用 posixfile 模块
    22. 1.22. bisect 模块
        1. 1.22.0.1. Example 14-28. 使用 bisect 模块向列表插入条目
        2. 1.22.0.2. Example 14-29. 使用 bisect 模块获得插入点位置
    23. 1.23. knee 模块
        1. 1.23.0.1. Example 14-30. 使用 knee 模块
    24. 1.24. tzparse 模块
        1. 1.24.0.1. Example 14-31. 使用 tzparse 模块
    25. 1.25. regex 模块
        1. 1.25.0.1. Example 14-32. 使用 regex 模块
    26. 1.26. regsub 模块
        1. 1.26.0.1. Example 14-33. 使用 regsub 模块
    27. 1.27. reconvert 模块
        1. 1.27.0.1. Example 14-34. 使用 reconvert 模块
    28. 1.28. regex_syntax 模块
        1. 1.28.0.1. Example 14-35. 使用 regex_syntax 模块
    29. 1.29. find 模块
        1. 1.29.0.1. Example 14-36. 使用 find 模块


[index.html 返回首页]


1. 其他模块


1.1. 概览

本章描述的是一些并不怎么常见的模块. 一些是很实用的, 另些是已经废弃的模块.


1.2. pyclbr 模块

pyclbr 模块包含一个基本的 Python 类解析器, 如 Example 14-1 所示.

版本 1.5.2 中, 改模块只包含一个 readmodule 函数, 解析给定模块, 返回一个模块所有顶层类组成的列表.

1.2.0.1. Example 14-1. 使用 pyclbr 模块

File: pyclbr-example-1.py

import pyclbr

mod = pyclbr.readmodule("cgi")

for k, v in mod.items():
    print k, v

*B*MiniFieldStorage <pyclbr.Class instance at 7873b0>
InterpFormContentDict <pyclbr.Class instance at 79bd00>
FieldStorage <pyclbr.Class instance at 790e20>
SvFormContentDict <pyclbr.Class instance at 79b5e0>
StringIO <pyclbr.Class instance at 77dd90>
FormContent <pyclbr.Class instance at 79bd60>
FormContentDict <pyclbr.Class instance at 79a9c0>*b*

2.0 及以后版本中, 添加了另个接口 readmodule_ex , 它还会读取全局函数. 如 Example 14-2 所示.

1.2.0.2. Example 14-2. 使用 pyclbr 模块读取类和函数

File: pyclbr-example-3.py

import pyclbr

# 2.0 and later
mod = pyclbr.readmodule_ex("cgi")

for k, v in mod.items():
    print k, v

*B*MiniFieldStorage <pyclbr.Class instance at 00905D2C>
parse_header <pyclbr.Function instance at 00905BD4>
test <pyclbr.Function instance at 00906FBC>
print_environ_usage <pyclbr.Function instance at 00907C94>
parse_multipart <pyclbr.Function instance at 00905294>
FormContentDict <pyclbr.Class instance at 008D3494>
initlog <pyclbr.Function instance at 00904AAC>
parse <pyclbr.Function instance at 00904EFC>
StringIO <pyclbr.Class instance at 00903EAC>
SvFormContentDict <pyclbr.Class instance at 00906824>
...*b*

访问类实例的属性可以获得关于类的更多信息, 如 Example 14-3 所示.

1.2.0.3. Example 14-3. 使用 pyclbr 模块

File: pyclbr-example-2.py

import pyclbr
import string

mod = pyclbr.readmodule("cgi")

def dump(c):
    # print class header
    s = "class " + c.name
    if c.super:
        s = s +  "(" + string.join(map(lambda v: v.name, c.super), ", ") + ")"
    print s + ":"
    # print method names, sorted by line number
    methods = c.methods.items()
    methods.sort(lambda a, b: cmp(a[1], b[1]))
    for method, lineno in methods:
        print "  def " + method
    print

for k, v in mod.items():
    dump(v)

*B*class MiniFieldStorage:
  def _ _init_ _
  def _ _repr_ _

class InterpFormContentDict(SvFormContentDict):
  def _ _getitem_ _
  def values
  def items

...*b*


1.3. filecmp 模块

( 2.0 新增) filecmp 模块用于比较文件和目录, 如 Example 14-4 所示.

1.3.0.1. Example 14-4. 使用 filecmp 模块

File: filecmp-example-1.py

import filecmp

if filecmp.cmp("samples/sample.au", "samples/sample.wav"):
    print "files are identical"
else:
    print "files differ!"

# files differ!

1.5.2 以及先前版本中, 你可以使用 cmpdircmp 模块代替.


1.4. cmd 模块

cmd 模块为命令行接口( command-line interfaces , CLI )提供了一个简单的框架. 它被用在 pdb 模块中, 当然你也可以在自己的程序中使用它, 如 Example 14-5 所示.

你只需要继承 Cmd 类, 定义 dohelp 方法. 基类会自动地将这些方法转换为对应命令.

1.4.0.1. Example 14-5. 使用 cmd 模块

File: cmd-example-1.py

import cmd
import string, sys

class CLI(cmd.Cmd):

    def _ _init_ _(self):
        cmd.Cmd._ _init_ _(self)
        self.prompt = '> '

    def do_hello(self, arg):
        print "hello again", arg, "!"

    def help_hello(self):
        print "syntax: hello [message]",
        print "-- prints a hello message"

    def do_quit(self, arg):
        sys.exit(1)

    def help_quit(self):
        print "syntax: quit",
        print "-- terminates the application"

    # shortcuts
    do_q = do_quit

#
# try it out

cli = CLI()
cli.cmdloop()

*B*> help

Documented commands (type help <topic>):
========================================
hello           quit

Undocumented commands:
======================
help            q

> hello world
hello again world !
> q*b*


1.5. rexec 模块

Feather 注: 版本 2.3 时取消了改模块的支持, 具体原因请参阅 : http://www.amk.ca/python/howto/rexec/http://mail.python.org/pipermail/python-dev/2002-December/031160.html

解决方法请参阅: http://mail.python.org/pipermail/python-list/2003-November/234581.html

rexec 模块提供了在限制环境下的 exec , eval , 以及 import 语句, 如 Example 14-6 所示. 在这个环境下, 所有可能对机器造成威胁的函数都不可用.

1.5.0.1. Example 14-6. 使用 rexec 模块

File: rexec-example-1.py

import rexec

r = rexec.RExec()
print r.r_eval("1+2+3")
print r.r_eval("_ _import_ _('os').remove('file')")

*B*6
Traceback (innermost last):
  File "rexec-example-1.py", line 5, in ?
    print r.r_eval("_ _import_ _('os').remove('file')")
  File "/usr/local/lib/python1.5/rexec.py", line 257, in r_eval
    return eval(code, m._ _dict_ _)
  File "<string>", line 0, in ?
AttributeError: remove*b*


1.6. Bastion 模块

Feather 注: 版本 2.3 时取消了改模块的支持, 具体原因请参阅 : http://www.amk.ca/python/howto/rexec/http://mail.python.org/pipermail/python-dev/2003-January/031848.html

Bastion 模块, 允许你控制给定对象如何使用, 如 Example 14-7 所示. 你可以通过它把对象从未限制部分传递到限制部分.

默认情况下, 所有的实例变量都是隐藏的, 所有的方法以下划线开头.

1.6.0.1. Example 14-7. 使用 Bastion 模块

File: bastion-example-1.py

import Bastion

class Sample:
    value = 0

    def _set(self, value):
        self.value = value

    def setvalue(self, value):
        if 10 < value <= 20:
            self._set(value)
        else:
            raise ValueError, "illegal value"

    def getvalue(self):
        return self.value

#
# try it

s = Sample()
s._set(100) # cheat
print s.getvalue()

s = Bastion.Bastion(Sample())
s._set(100) # attempt to cheat
print s.getvalue()

*B*100
Traceback (innermost last):
...
AttributeError: _set*b*

你可以控制发布哪个函数. 在 Example 14- 中, 内部方法可以从外部调用, 但 getvalue 不再起作用.

1.6.0.2. Example 14-8. 使用 Bastion 模块处理非标准过滤器

File: bastion-example-2.py

import Bastion

class Sample:
    value = 0

    def _set(self, value):
        self.value = value

    def setvalue(self, value):
        if 10 < value <= 20:
            self._set(value)
        else:
            raise ValueError, "illegal value"

    def getvalue(self):
        return self.value

#
# try it

def is_public(name):
    return name[:3] != "get"

s = Bastion.Bastion(Sample(), is_public)
s._set(100) # this works
print s.getvalue() # but not this

*B*100
Traceback (innermost last):
...
AttributeError: getvalue*b*


1.7. readline 模块

(可选) readline 模块使用 GNU readline 库(或兼容库)实现了 Unix 下增强的输入编辑支持. Example 14-9 所示.

该模块提供了增强的命令行编辑功能, 例如命令行历史等. 它还增强了 inputraw_input 函数.

1.7.0.1. Example 14-9. 使用 readline 模块

File: readline-example-1.py

import readline # activate readline editing


1.8. rlcompleter 模块

(可选, 只用于 Unix ) rlcompleter 模块为 readline 模块提供了单词自动完成功能.

导入该模块就可以启动自动完成功能. 默认情况下完成函数被绑定在了 Esc 键上. 按两次 Esc 键就可以自动完成当前单词. 你可以使用下面的代码修改所绑定的键:

import readline
readline.parse_and_bind("tab: complete")

Example 14-10 展示了如何在程序中使用自动完成函数.

1.8.0.1. Example 14-10. 使用 rlcompleter 模块展开名字

File: rlcompleter-example-1.py

import rlcompleter
import sys

completer = rlcompleter.Completer()

for phrase in "co", "sys.p", "is":
    print phrase, "=>",
    # emulate readline completion handler
    try:
        for index in xrange(sys.maxint):
            term = completer.complete(phrase, index)
            if term is None:
                break
            print term,
    except:
        pass
    print

co => continue compile complex coerce completer
sys.p => sys.path sys.platform sys.prefix
is => is isinstance issubclass


1.9. statvfs 模块

statvfs 模块包含一些与 os.statvfs (可选)函数配合使用的常量和函数, 该函数会返回文件系统的相关信息. 如 Example 14-11 所示.

1.9.0.1. Example 14-11. 使用 statvfs 模块

File: statvfs-example-1.py

import statvfs
import os

st = os.statvfs(".")

print "preferred block size", "=>", st[statvfs.F_BSIZE]
print "fundamental block size", "=>", st[statvfs.F_FRSIZE]
print "total blocks", "=>", st[statvfs.F_BLOCKS]
print "total free blocks", "=>", st[statvfs.F_BFREE]
print "available blocks", "=>", st[statvfs.F_BAVAIL]
print "total file nodes", "=>", st[statvfs.F_FILES]
print "total free nodes", "=>", st[statvfs.F_FFREE]
print "available nodes", "=>", st[statvfs.F_FAVAIL]
print "max file name length", "=>", st[statvfs.F_NAMEMAX]

*B*preferred block size => 8192
fundamental block size => 1024
total blocks => 749443
total free blocks => 110442
available blocks => 35497
total file nodes => 92158
total free nodes => 68164
available nodes => 68164
max file name length => 255*b*


1.10. calendar 模块

calendar 模块是 Unix cal 命令的 Python 实现. 它可以将给定年份/月份的日历输出到标准输出设备上.

prmonth(year, month) 打印给定月份的日历, 如 Example 14-12 所示.

1.10.0.1. Example 14-12. 使用 calendar 模块

File: calendar-example-1.py

import calendar
calendar.prmonth(1999, 12)

*B*    December 1999
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31*b*

prcal(year) 打印给定年份的日历, 如 Example 14-13 所示.

1.10.0.2. Example 14-13. 使用 calendar 模块

File: calendar-example-2.py

import calendar
calendar.prcal(2000)

*B*                                  2000

       January                  February                    March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2          1  2  3  4  5  6             1  2  3  4  5
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       6  7  8  9 10 11 12
10 11 12 13 14 15 16      14 15 16 17 18 19 20      13 14 15 16 17 18 19
17 18 19 20 21 22 23      21 22 23 24 25 26 27      20 21 22 23 24 25 26
24 25 26 27 28 29 30      28 29                     27 28 29 30 31
31

        April                      May                      June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2       1  2  3  4  5  6  7                1  2  3  4
 3  4  5  6  7  8  9       8  9 10 11 12 13 14       5  6  7  8  9 10 11
10 11 12 13 14 15 16      15 16 17 18 19 20 21      12 13 14 15 16 17 18
17 18 19 20 21 22 23      22 23 24 25 26 27 28      19 20 21 22 23 24 25
24 25 26 27 28 29 30      29 30 31                  26 27 28 29 30

        July                     August                   September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2          1  2  3  4  5  6                   1  2  3
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       4  5  6  7  8  9 10
10 11 12 13 14 15 16      14 15 16 17 18 19 20      11 12 13 14 15 16 17
17 18 19 20 21 22 23      21 22 23 24 25 26 27      18 19 20 21 22 23 24
24 25 26 27 28 29 30      28 29 30 31               25 26 27 28 29 30
31

       October                  November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                   1             1  2  3  4  5                   1  2  3
 2  3  4  5  6  7  8       6  7  8  9 10 11 12       4  5  6  7  8  9 10
 9 10 11 12 13 14 15      13 14 15 16 17 18 19      11 12 13 14 15 16 17
16 17 18 19 20 21 22      20 21 22 23 24 25 26      18 19 20 21 22 23 24
23 24 25 26 27 28 29      27 28 29 30               25 26 27 28 29 30 31
30 31*b*

注意这里的日历是按照欧洲习惯打印的, 也就是说星期一是一个星期的第一天, 其他情况需要请参考模块中的几个类. (和咱们一样, 不用管了)

该模块中的其他类或函数可以帮助你输出自己需要的格式.


1.11. sched 模块

sched 模块为非线程环境提供了一个简单的计划任务模式. 如 Example 14-14 所示.

1.11.0.1. Example 14-14. 使用 sched 模块

File: sched-example-1.py

import sched
import time, sys

scheduler = sched.scheduler(time.time, time.sleep)

# add a few operations to the queue
scheduler.enter(0.5, 100, sys.stdout.write, ("one\n",))
scheduler.enter(1.0, 300, sys.stdout.write, ("three\n",))
scheduler.enter(1.0, 200, sys.stdout.write, ("two\n",))

scheduler.run()

*B*one
two
three*b*


1.12. statcache 模块

statcache 模块提供了访问文件相关信息的相关函数. 它是 os.stat 的扩展模块, 而且它会缓存收集到的信息. 如 Example 14-15 所示.

2.2 后该模块被废弃, 请使用 os.stat() 函数代替, 原因很简单, 它导致了更复杂的缓存管理, 反而降低了性能.

1.12.0.1. Example 14-15. 使用 statcache 模块

File: statcache-example-1.py

import statcache
import os, stat, time

now = time.time()
for i in range(1000):
    st = os.stat("samples/sample.txt")
print "os.stat", "=>", time.time() - now

now = time.time()
for i in range(1000):
    st = statcache.stat("samples/sample.txt")
print "statcache.stat", "=>", time.time() - now

print "mode", "=>", oct(stat.S_IMODE(st[stat.ST_MODE]))
print "size", "=>", st[stat.ST_SIZE]
print "last modified", "=>", time.ctime(st[stat.ST_MTIME])

*B*os.stat => 0.371000051498
statcache.stat => 0.0199999809265
mode => 0666
size => 305
last modified => Sun Oct 10 18:39:37 1999*b*


1.13. grep 模块

grep 模块提供了在文本文件中搜索字符串的另种方法, 如 Example 14-16 所示.

版本 2.1 时被声明不支持, 及就是说, 当前版本已经无法使用该模块.

1.13.0.1. Example 14-16. 使用 grep 模块

File: grep-example-1.py

import grep
import glob

grep.grep("\<rather\>", glob.glob("samples/*.txt"))

# 4: indentation, rather than delimiters, might become


1.14. dircache 模块

(已经废弃) 与 statcache 类似, 该模块是 os.listdir 函数的一个扩展, 提供了缓存支持, 可能因为同样的原因被废弃吧~ MUHAHAHAHAHA~~~~ . 请使用 os.listdir 代替. Example 14-17 所示.

1.14.0.1. Example 14-17. 使用 dircache 模块

File: dircache-example-1.py

import dircache

import os, time

# 
# test cached version

t0 = time.clock()

for i in range(100):
    dircache.listdir(os.sep)

print "cached", time.clock() - t0

# 
# test standard version

t0 = time.clock()

for i in range(100):
    os.listdir(os.sep)

print "standard", time.clock() - t0

*B*cached 0.0664509964968
standard 0.5560845807*b*


1.15. dircmp 模块

(已废弃, 只用于 1.5.2) dircmp 模块用于比较两个目录的内容, Example 14-18 所示.

1.15.0.1. Example 14-18. 使用 dircmp 模块

File: dircmp-example-1.py

import dircmp

d = dircmp.dircmp()
d.new("samples", "oldsamples")
d.run()
d.report()

*B*diff samples oldsamples
Only in samples : ['sample.aiff', 'sample.au', 'sample.wav']
Identical files : ['sample.gif', 'sample.gz', 'sample.jpg', ...]*b*

Python 2.0 后, 该模块被 filecmp 替换.


1.16. cmp 模块

(已废弃, 只用于 1.5.2) cmp 模块用于比较两个文件, 如 Example 14-19 所示.

1.16.0.1. Example 14-19. 使用 cmp 模块

File: cmp-example-1.py

import cmp

if cmp.cmp("samples/sample.au", "samples/sample.wav"):
    print "files are identical"
else:
    print "files differ!"

*B*files differ!*b*

Python 2.0 后, 该模块被 filecmp 替换.


1.17. cmpcache 模块

(已废弃, 只用于 1.5.2) cmpcache 模块用于比较两个文件. 它是 cmp 模块的扩展, 提供了缓存支持. 如 Example 14-20 所示.

1.17.0.1. Example 14-20. 使用 cmpcache 模块

File: cmpcache-example-1.py

import cmpcache

if cmpcache.cmp("samples/sample.au", "samples/sample.wav"):
    print "files are identical"
else:
    print "files differ!"

*B*files differ!*b*

Python 2.0 后, 该模块被 filecmp 替换.

filecmp 已经不提供缓存支持.


1.18. util 模块

(已废弃, 只用于 1.5.2) util 模块提供了常见操作的封装函数. 新代码可以使用如 Examples 14-21 到 14-23 的实现方法.

Example 14-21 展示了 remove(sequence, item) 函数.

1.18.0.1. Example 14-21. 实现 util 模块的 remove 函数

File: util-example-1.py

def remove(sequence, item):
    if item in sequence:
        sequence.remove(item)

Example 14-22 展示了 readfile(filename) => string 函数.

1.18.0.2. Example 14-22. 实现 util 模块的 readfile 函数

File: util-example-2.py

def readfile(filename):
    file = open(filename, "r")
    return file.read()

Example 14-23 展示了 `readopenfile(file) => string 函数.

1.18.0.3. Example 14-23. 实现 util 模块的 readopenfile 函数

File: util-example-3.py

def readopenfile(file):
    return file.read()


1.19. soundex 模块

(已废弃, 只用于 1.5.2) soundex 实现了一个简单的 hash 算法, 基于英文发音将单词转换为 6 个字符的字符串.

版本 2.0 后, 该模块已从标准库中删除.

get_soundex(word) 返回给定单词的 soundex 字符串. sound_similar(word1, word2) 判断两个单词的 soundex 是否相同. 一般说来发音相似的单词有相同的 soundex . 如 Example 14-24 所示.

1.19.0.1. Example 14-24. 使用 soundex 模块

File: soundex-example-1.py

import soundex

a = "fredrik"
b = "friedrich"

print soundex.get_soundex(a), soundex.get_soundex(b)

print soundex.sound_similar(a, b)

*B*F63620 F63620
1*b*


1.20. timing 模块

(已废弃, 只用于 Unix ) timing 用于监控 Python 程序的执行时间. 如 Example 14-25 所示.

1.20.0.1. Example 14-25. 使用 timing 模块

File: timing-example-1.py

import timing
import time

def procedure():
    time.sleep(1.234)

timing.start()
procedure()
timing.finish()

print "seconds:", timing.seconds()
print "milliseconds:", timing.milli()
print "microseconds:", timing.micro()

*B*seconds: 1
milliseconds: 1239
microseconds: 1239999*b*

你可以按照 Example 14-26 中的方法用 time 模块实现 timing 模块的功能.

1.20.0.2. Example 14-26. 模拟 timing 模块

File: timing-example-2.py

import time

t0 = t1 = 0

def start():
    global t0
    t0 = time.time()


def finish():
    global t1
    t1 = time.time()

def seconds():
    return int(t1 - t0)

def milli():
    return int((t1 - t0) * 1000)

def micro():
    return int((t1 - t0) * 1000000)

time.clock() 可以替换 time.time() 获得 CPU 时间.


1.21. posixfile 模块

(已废弃, 只用于 Unix ) posixfile 提供了一个类文件的对象( file-like object ), 实现了文件锁定的支持. 如 Example 14-27 所示. 新程序请使用 fcntl 模块代替.

1.21.0.1. Example 14-27. 使用 posixfile 模块

File: posixfile-example-1.py

import posixfile
import string

filename = "counter.txt"

try:
    # open for update
    file = posixfile.open(filename, "r+")
    counter = int(file.read(6)) + 1
except IOError:
    # create it
    file = posixfile.open(filename, "w")
    counter = 0

file.lock("w|", 6)

file.seek(0) # rewind
file.write("%06d" % counter)

file.close() # releases lock


1.22. bisect 模块

bisect 模块用于向排序后的序列插入对象.

insort(sequence, item) 将条目插入到序列中, 并且保证序列的排序. 序列可以是任意实现了 _ _getitem_ _insert 方法的序列对象. 如 Example 14-28 所示.

1.22.0.1. Example 14-28. 使用 bisect 模块向列表插入条目

File: bisect-example-1.py

import bisect

list = [10, 20, 30]

bisect.insort(list, 25)
bisect.insort(list, 15)

print list

*B*[10, 15, 20, 25, 30]*b*

bisect(sequence, item) => index 返回条目插入后的索引值, 不对序列做任何修改. Example 14-29 所示.

1.22.0.2. Example 14-29. 使用 bisect 模块获得插入点位置

File: bisect-example-2.py

import bisect

list = [10, 20, 30]

print list
print bisect.bisect(list, 25)
print bisect.bisect(list, 15)

*B*[10, 20, 30]
2
1*b*


1.23. knee 模块

knee 模块用于 Python 1.5 中导入包( package import )的实现. 当然 Python 解释器已经支持了这个, 所以这个模块几乎没有什么作用, 不过你可以看看它的代码, 明白这一切是怎么完成的.

代码请参见 Python-X.tgz\Python-2.4.4\Demo\imputil\knee.py

当然, 你可以导入该模块,如 Example 14-30 所示.

1.23.0.1. Example 14-30. 使用 knee 模块

File: knee-example-1.py

import knee

# that's all, folks!


1.24. tzparse 模块

(已废弃) tzparse 模块用于解析时区标志( time zone specification ). 导入时它会自动分析 TZ 环境变量. 如 Example 14-31 所示.

1.24.0.1. Example 14-31. 使用 tzparse 模块

File: tzparse-example-1.py

import os
if not os.environ.has_key("TZ"):
    # set it to something...
    os.environ["TZ"] = "EST+5EDT;100/2,300/2"

# importing this module will parse the TZ variable
import tzparse

print "tzparams", "=>", tzparse.tzparams
print "timezone", "=>", tzparse.timezone
print "altzone", "=>", tzparse.altzone
print "daylight", "=>", tzparse.daylight
print "tzname", "=>", tzparse.tzname

*B*tzparams => ('EST', 5, 'EDT', 100, 2, 300, 2)
timezone => 18000
altzone => 14400
daylight => 1
tzname => ('EST', 'EDT')*b*

除了这些变量之外, 该模块还提供了一些用于时间计算的函数.


1.25. regex 模块

(已废弃) regex 模块是旧版本的(1.5 前)正则表达式模块, 用法如 Example 14-32 所示. 新代码请使用 re 模块实现.

注意在 Python 1.5.2 中 regexre 模块要快. 但在新版本中 re 模块更快.

1.25.0.1. Example 14-32. 使用 regex 模块

File: regex-example-1.py

import regex

text = "Man's crisis of identity in the latter half of the 20th century"

p = regex.compile("latter") # literal
print p.match(text)
print p.search(text), repr(p.group(0))

p = regex.compile("[0-9]+") # number
print p.search(text), repr(p.group(0))

p = regex.compile("\<\w\w\>") # two-letter word
print p.search(text), repr(p.group(0))

p = regex.compile("\w+$") # word at the end
print p.search(text), repr(p.group(0))

*B*-1
32 'latter'
51 '20'
13 'of'
56 'century'*b*


1.26. regsub 模块

(已废弃) regsub 模块提供了基于正则表达式的字符串替换操作. 用法如 Example 14-33 所示. 新代码请使用 re 模块中的 replace 函数代替.

1.26.0.1. Example 14-33. 使用 regsub 模块

File: regsub-example-1.py

import regsub

text = "Well, there's spam, egg, sausage, and spam."

print regsub.sub("spam", "ham", text) # just the first
print regsub.gsub("spam", "bacon", text) # all of them

*B*Well, there's ham, egg, sausage, and spam.
Well, there's bacon, egg, sausage, and bacon.*b*


1.27. reconvert 模块

(已废弃) reconvert 提供了旧样式正则表达式( regex 模块中使用)到新样式( re 模块)的转换工具. Example 14-34 所示. 它也可以作为一个命令行工具.

1.27.0.1. Example 14-34. 使用 reconvert 模块

File: reconvert-example-1.py

import reconvert

for pattern in "abcd", "a\(b*c\)d", "\<\w+\>":
    print pattern, "=>", reconvert.convert(pattern)

*B*abcd => abcd
a\(b*c\)d => a(b*c)d
\<\w+\> => \b\w+\b*b*


1.28. regex_syntax 模块

(已废弃) regex_syntax 模块用于改变正则表达式的模式, 如 Example 14-35 所示.

1.28.0.1. Example 14-35. 使用 regex_syntax 模块

File: regex-syntax-example-1.py

import regex_syntax
import regex

def compile(pattern, syntax):
    syntax = regex.set_syntax(syntax)
    try:
        pattern = regex.compile(pattern)
    finally:
        # restore original syntax
        regex.set_syntax(syntax)
    return pattern

def compile_awk(pattern):
    return compile(pattern, regex_syntax.RE_SYNTAX_AWK)

def compile_grep(pattern):
    return compile(pattern, regex_syntax.RE_SYNTAX_GREP)

def compile_emacs(pattern):
    return compile(pattern, regex_syntax.RE_SYNTAX_EMACS)


1.29. find 模块

(已废弃, 只用于 1.5.2) find 模块用于在给定目录及其子目录中查找符合给定匹配模式的文件, Example 14-36 所示.

匹配模式的语法与 fnmatch 中相同.

1.29.0.1. Example 14-36. 使用 find 模块

File: find-example-1.py

import find

# find all JPEG files in or beneath the current directory
for file in find.find("*.jpg", "."):
    print file

*B*.\samples\sample.jpg*b*


PythonStandardLib/chpt14 (last edited 2009-12-25 07:09:31 by localhost)