::-- JinQing [2009-01-12 06:39:25]
19.2. Persistence Options in Python
19.2. Python中的持久化选择
In this chapter, our focus is on persistent datathe kind that outlives a program that creates it. That's not true by default for objects a script constructs; things like lists, dictionaries, and even class instance objects live in your computer's memory and are lost as soon as the script ends. To make data live longer, we need to do something special. In Python programming, there are today at least six traditional ways to save information in between program executions:
本章的中心内容是数据持久化,即数据在创建它的程序关闭之后仍然存在。 脚本构造的对象默认情况下不是这样,如列表、字典,甚至类实例对象, 它们在计算机内存中,脚本一结束它们就消失了。 为了使数据寿命更长,我们需要专门处理。 Python中,目前至少有6种传统方式在程序执行之间保存信息:
Flat files
无格式文件(Flat files)
Storing text and bytes
保存文本和字节
DBM keyed files
DBM键控文件(DBM keyed files)
Keyed access to strings
按关键字访问字符串
Pickled objects
Pickle对象
Serializing Python objects to files and streams
序列化Python对象到文件和流
Shelve files
Shelve文件
Storing pickled Python objects in DBM keyed files
在DBM键控文件中保存Python pickle对象
ZODB object databases
ZODB对象数据库
Storing Python objects in persistent dictionaries
在持久化字典中保存Python对象
SQL relational databases
SQL关系数据库
Table-based systems that support queries
基于表的系统,支持查询
In some sense, Python's interfaces to network-based object transmission protocols such as SOAP, XML-RPC, and CORBA also offer persistence options, but they are beyond the scope of this chapter. Here, our interest is in techniques that allow a program to store its data directly and, usually, on the local machine. Although some database servers may operate on a physically remote machine on a network, this is largely transparent to most of the techniques we'll study here.
从某种意义上说,Python的接口中,以网络为基础的对象传输协议如SOAP、XML-RPC、和CORBA也提供了持久化的选择,但它们不在本章范围之内。在这里我们关心技术的是,允许程序直接存储其数据,并且通常是在本地计算机上。虽然有些数据库服务器可能实际运行在网络上的远程计算机,但对于我们将在这里研究的大多数技术来说,这在很大程度上是透明。
We studied Python's simple (or "flat") file interfaces in earnest in Chapter 4, and we have been using them ever since. Python provides standard access to both the stdio filesystem (through the built-in open function), as well as lower-level descriptor-based files (with the built-in os module). For simple data storage tasks, these are all that many scripts need. To save for use in a future program run, simply write data out to a newly opened file on your computer and read it back from that file later. As we've seen, for more advanced tasks, Python also supports other file-like interfaces such as pipes, fifos, and sockets.
我们早先已在第4章研究过Python的简单(或“无格式”)文件接口,并且一直在使用他们。 Python对两种文件都提供了标准的访问方法:stdio文件系统(通过内置的open函数), 以及低级的以描述符为基础的文件(通过内置的os模块)。 对于简单的数据存储任务,这些就是许多脚本所需要的全部。 为了保存数据用于将来的程序运行, 只需将数据写入到计算机上一个新建的文件, 后来再从该文件读取。 正如我们所见,对于更高级的任务, Python还支持其他类似文件的接口,如管道、fifo,和socket。
Since we've already explored flat files, I won't say more about them here. The rest of this chapter introduces the remaining topics on the preceding list. At the end, we'll also meet a GUI program for browsing the contents of things such as shelves and DBM files. Before that, though, we need to learn what manner of beast these are.
因为我们已经讲过无格式文件,所以本章不再累述。 本章的余下部分将介绍上述列表中的余下主题。 最后将会有个GUI程序,用于浏览如shelve和DBM文件这类东西的内容。 但是在这之前,我们需要了解它们是什么样的。
讨论 Discussion
参考 See Also