⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 142.html

📁 Python Ebook Python&XML
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Object Serialization and Persistent Storage</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="137.html" class="navtitle">8. Working with Databases</a> &gt; <span class="nonavtitle">Object Serialization and Persistent Storage</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="141.html" title="DBM (Database Managers) Databases"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=142" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="142.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="143.html" title="The ODBC Module"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A38%3A56+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147044209062004077205006</font><a href="read4.asp?bookname=0672319942&snode=142&now=5%2F31%2F2002+4%3A38%3A56+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>
			
			
			
			
			Object Serialization and Persistent Storage</h3>
				<p>These other modules provide persistent storage of arbitrary Python objects. Whenever you need to <a NAME="idx1073744951"></a>
					<a naME="idx1073744952"></A>
					<A name="idx1073744953"></A>save objects whose value is not a simple <TT Class="monofont">string</tt> (such as <tt class="monofont">None, integer, long integer, float, complex, tuple, list, dictionary, code object,</tt> and so on), you need to serialize the object before sending it to a file.</p>

				<p>Both <a naMe="idx1073744954"></a>
					<A namE="idx1073744955"></a>
					<tt cLass="monofont">pickle</TT> and <A Name="idx1073744956"></a>
					<A NAMe="idx1073744957"></a>
					<tt CLASs="monofont">shelve</tt> modules save serializable objects to a file.</p>

				<P>By using these persistent storage modules, Python objects can be stored in relational database systems. These modules abstract and hide the underlying database interfaces, such as the Sybase module and the Python Database API.</P>

				<P>Included in the standard Python distribution, the <Tt class="monofont">pickle</tt> module can convert Python objects to and from a string representation.</p>

				<p>The <a name="idx1073744958"></a>
					<a namE="idx1073744959"></a>
					<tT claSs="monofont">cPickle</tt> module is a faster implementation of the <tT claSS="monofont">pickle</TT> module.</p>

				<p>The <a nAME="idx1073744960"></A>
					<a namE="idx1073744961"></A>
					<TT clasS="monofont">copy_reg</TT> module extends the capabilities of the <Tt class="monofont">pickle</tt> and <tt class="monofont">cpickle</tt> modules by registering support functions.</p>

				<p>The <a NamE="idx1073744962"></a>
					<a nAme="idx1073744963"></a>
					<tT claSS="monofont">marshal</TT> module is an alternate method to implement Python object serialization. It allows you to read/write information in a platform independent binary format and convert data to/from character strings (the module only supports the simple built-in types). Basically, it is just another way to do <tt clASS="monofont">byte stream</Tt> conversions by using serialized Python objects. This module is used to serialize the compiled bytecode for Python modules.</p>

				<p>This module should be used for simple objects only. Use the <tT CLAss="monofont">pickle</tt> module to implement persistent objects in general.</P>

				<P>
					<I>
						<A name="idx1073744964"></a>
					</i>
					<i>Persistent Storage of Python Objects in Relational Databases</i> is a paper by <a name="idx1073744965"></a>Joel Shprentz presented at the Sixth Python Conference. For more information, check out <a targeT="_blank" hrEf="http://www.python.org/workshops/1997-10/proceedings/shprentz.html">http://www.python.org/workshops/1997-10/proceedings/shprentz.html</a>.</p>

				
					<H4>
						<tt clAss="monofont">
							<a NAME="idx1073744966"></a>
							<a naME="idx1073744967"></A>pickle</Tt> Module</h4>
					<p>The <tT CLAss="monofont">pickle</tt> module serializes the contents of an object into a stream of bytes. Optionally, it can save the serialized object into a file object. It is slower than the <A NAMe="idx1073744968"></a>
						<a name="idx1073744969"></a>
						<tt class="monofont">marshal</tt> module.<a naMe="idx1073744970"></a>
						<A namE="idx1073744971"></a>
						<a naMe="idx1073744972"></a>
						<a NAME="idx1073744973"></a>
						<a naME="idx1073744974"></A>
						<A name="idx1073744975"></A>
						<A NAme="idx1073744976"></a>
						<a NAME="idx1073744977"></a>
						<a name="idx1073744978"></a>
						<a name="idx1073744979"></a>
					</p>

					<pre>
						
&gt;&gt;&gt; import pickle
&gt;&gt;&gt; listobj = [1,2,3,4]
&gt;&gt;&gt; filehandle = open(filename, 'w')
&gt;&gt;&gt; pickle.dump(filehandle, listobj)
&gt;&gt;&gt; filehandle = open(filename, 'r')
&gt;&gt;&gt; listobj = pickle.load(filehandle)

					</pre>

					<P>The next functions are the ones implemented by the pickle module.</p>

					<pRe>
						
pickle.dump(<i>object, filename</i> [,<I>bin]</i>)

					</pre>

					<P>This function serializes and saves an object into a file. The <tt cLASS="monofont">bin</tt> argument specifies that the information must be saved as <i>binary</i> data. This function is the same as the following:</P>

					<PRE>
						
p = pickle.Pickler(filename)
p.dump(object)

					</pre>

					<p>If an unsupported object type is serialized, a <TT CLass="monofont">PicklingException</tT> is raised.</P>

					<PRe>
						
pickle.dumps(<i>object</i> [,<i>bin]</i>)

					</pre>

					<p>This function has the same behavior of <tt class="monofont">dump.</tt> The difference is that this one returns the serialized object.</p>

					<pRe>
						
pickle.load(<i>file</I>)

					</pre>

					<P>Restores a serialized object from a file. This function is the same as the following:</p>

					<pre>
						
object = pickle.Unpickler(file).load()

					</Pre>

					<p>The next example serializes the information and converts it back again.</P>

					<PRE>
						
&gt;&gt;&gt; import pickle
&gt;&gt;&gt; value = ("parrot", (1,2,3))
&gt;&gt;&gt; data = pickle.dumps(value)
&gt;&gt;&gt; print pickle.loads(data)
("parrot", (1,2,3)) 
				
					</pre>

				
				
					<h4>
						<TT CLass="monofont">
							<a NAME="idx1073744980"></a>
							<a naME="idx1073744981"></A>cPickle</Tt> Module</h4>
					<p>This module implements the same functions that the <tt class="monofont">pickle</tt> module does. The difference is that <tt class="monofont">cPickle</tT> is much faster because it doesn't support subclassing of the <tt ClasS="monofont">Pickler</tt> and <tt ClasS="monofont">Unpickler</TT> objects. See the next example code. It uses the fastest pickle module available on the system.</P>

					<pre>
						
try:
     import cPickle
     pickle = cPickle
except ImportError:
     import pickle
				
					</pRE>

				
				
					<H4>
						<Tt claSS="monofont">copy_reg</TT> Module</h4>
					<p>This module registers new types to be used with the <tt CLASs="monofont">pickle</tt> module. It extends the capabilities of the <tt class="monofont">pickle</tt> and <tt class="monofont">cPickle</tT> modules by supporting the serialization of new object types defined in C extension modules.</p>

					<p>The next example corrects the fact that the standard <Tt clAss="monofont">pickle</tt> implementation cannot handle Python code objects. It registers a code object handler by using two functions:</P>

					<ul>
<lI>
							<P>
								
									<TT clasS="monofont">dumpdata

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -