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

📄 node12.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<P>有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 urls 接收的数据的 <a class="ulink" href="../lib/module-urllib2.html"  ><tt class="module">urllib2</tt></a> 以及用于发送电子邮件的 <a class="ulink" href="../lib/module-smtplib.html"  ><tt class="module">smtplib</tt></a>。<P><div class="verbatim"><pre>
&gt;&gt;&gt; import urllib2
&gt;&gt;&gt; for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
...     if 'EST' in line:      # look for Eastern Standard Time
...         print line

&lt;BR&gt;Nov. 25, 09:43:32 PM EST

&gt;&gt;&gt; import smtplib
&gt;&gt;&gt; server = smtplib.SMTP('localhost')
&gt;&gt;&gt; server.sendmail('soothsayer@tmp.org', 'jceasar@tmp.org',
"""To: jceasar@tmp.org
From: soothsayer@tmp.org

Beware the Ides of March.
""")
&gt;&gt;&gt; server.quit()</pre></div><P><H1><A NAME="SECTION0012800000000000000000"></A><A NAME="dates-and-times"></A><BR>10.8 日期和时间 Dates and Times </H1><P>The <a class="ulink" href="../lib/module-datetime.html"  ><tt class="module">datetime</tt></a> module
supplies classes for manipulating dates and times in both simple
and complex ways. While date and time arithmetic is supported, the
focus of the implementation is on efficient member extraction for
output formatting and manipulation.  The module also supports objects
that are time zone aware.<P><a class="ulink" href="../lib/module-datetime.html"  ><tt class="module">datetime</tt></a> 模块为日期和时间处理同时提供了简单和复杂的方法。支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出。该模块还支持时区处理。<P><div class="verbatim"><pre>
# dates are easily constructed and formatted
&gt;&gt;&gt; from datetime import date
&gt;&gt;&gt; now = date.today()
&gt;&gt;&gt; now
datetime.date(2003, 12, 2)
&gt;&gt;&gt; now.strftime("%m-%d-%y or %d%b %Y is a %A on the %d day of %B")
'12-02-03 or 02Dec 2003 is a Tuesday on the 02 day of December'

# dates support calendar arithmetic
&gt;&gt;&gt; birthday = date(1964, 7, 31)
&gt;&gt;&gt; age = now - birthday
&gt;&gt;&gt; age.days
14368</pre></div><P><H1><A NAME="SECTION0012900000000000000000"></A><A NAME="data-compression"></A><BR>10.9 数据压缩 Data Compression </H1><P>Common data archiving and compression formats are directly supported
by modules including:
<a class="ulink" href="../lib/module-zlib.html"  ><tt class="module">zlib</tt></a>,
<a class="ulink" href="../lib/module-gzip.html"  ><tt class="module">gzip</tt></a>,
<a class="ulink" href="../lib/module-bz2.html"  ><tt class="module">bz2</tt></a>,
<a class="ulink" href="../lib/module-zipfile.html"  ><tt class="module">zipfile</tt></a>, and
<a class="ulink" href="../lib/module-tarfile.html"  ><tt class="module">tarfile</tt></a>.<P>以下模块直接支持通用的数据打包和压缩格式:<P><a class="ulink" href="../lib/module-zlib.html"  ><tt class="module">zlib</tt></a>,
<a class="ulink" href="../lib/module-gzip.html"  ><tt class="module">gzip</tt></a>,
<a class="ulink" href="../lib/module-bz2.html"  ><tt class="module">bz2</tt></a>,
<a class="ulink" href="../lib/module-zipfile.html"  ><tt class="module">zipfile</tt></a>, 以及
<a class="ulink" href="../lib/module-tarfile.html"  ><tt class="module">tarfile</tt></a><P><div class="verbatim"><pre>
&gt;&gt;&gt; import zlib
&gt;&gt;&gt; s = 'witch which has which witches wrist watch'
&gt;&gt;&gt; len(s)
41
&gt;&gt;&gt; t = zlib.compress(s)
&gt;&gt;&gt; len(t)
37
&gt;&gt;&gt; zlib.decompress(t)
'witch which has which witches wrist watch'
&gt;&gt;&gt; zlib.crc32(t)
-1438085031</pre></div><P><H1><A NAME="SECTION00121000000000000000000"></A><A NAME="performance-measurement"></A><BR>10.10 性能度量 Performance Measurement </H1><P>Some Python users develop a deep interest in knowing the relative
performance between different approaches to the same problem.
Python provides a measurement tool that answers those questions
immediately.<P>有些用户对了解解决同一问题的不同方法之间的性能差异很感兴趣。Python 提供了一个度量工具,为这些问题提供了直接答案。<P>For example, it may be tempting to use the tuple packing and unpacking
feature instead of the traditional approach to swapping arguments.
The <a class="ulink" href="../lib/module-timeit.html"  ><tt class="module">timeit</tt></a> module
quickly demonstrates that the traditional approach is faster:<P>例如,使用元组封装和拆封来交换元素看起来要比使用传统的方法要诱人的多。<a class="ulink" href="../lib/module-timeit.html"  ><tt class="module">timeit</tt></a>
证明了传统的方法更快一些。<P><div class="verbatim"><pre>
&gt;&gt;&gt; from timeit import Timer
&gt;&gt;&gt; Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.60864915603680925
&gt;&gt;&gt; Timer('a,b = b,a', 'a=1; b=2').timeit()
0.8625194857439773</pre></div><P>In contrast to <tt class="module">timeit</tt>'s fine level of granularity, the
<a class="ulink" href="../lib/module-profile.html"  ><tt class="module">profile</tt></a> and <tt class="module">pstats</tt>
modules provide tools for identifying time critical sections in larger
blocks of code.<P>相对于 <tt class="module">timeit</tt> 的细粒度,<a class="ulink" href="../lib/module-profile.html"  ><tt class="module">profile</tt></a> 和 <tt class="module">pstats</tt> 模块提供了针对更大代码块的时间度量工具。<P><H1><A NAME="SECTION00121100000000000000000"></A><A NAME="quality-control"></A><BR>10.11 质量控制 Quality Control </H1><P>One approach for developing high quality software is to write tests for
each function as it is developed and to run those tests frequently during
the development process.<P>开发高质量软件的方法之一是为每一个函数开发测试代码,并且在开发过程中经常进行测试。<P>The <a class="ulink" href="../lib/module-doctest.html"  ><tt class="module">doctest</tt></a> module provides
a tool for scanning a module and validating tests embedded in a program's
docstrings.  Test construction is as simple as cutting-and-pasting a
typical call along with its results into the docstring.  This improves
the documentation by providing the user with an example and it allows the
doctest module to make sure the code remains true to the documentation:<P><a class="ulink" href="../lib/module-doctest.html"  ><tt class="module">doctest</tt></a> 模块提供了一个工具,扫描模块并根据程序中内嵌的文档字符串执行测试。测试构造如同简单的将它的输出结果剪切并粘贴到文档字符串中。通过用户提供的例子,它发展了文档,允许 doctest 模块确认代码的结果是否与文档一致。<P><div class="verbatim"><pre>
def average(values):
    """Computes the arithmetic mean of a list of numbers.

    &gt;&gt;&gt; print average([20, 30, 70])
    40.0
    """
    return sum(values, 0.0) / len(values)

import doctest
doctest.testmod()   # automatically validate the embedded tests</pre></div><P>The <a class="ulink" href="../lib/module-unittest.html"  ><tt class="module">unittest</tt></a> module is not
as effortless as the <tt class="module">doctest</tt> module, but it allows a more
comprehensive set of tests to be maintained in a separate file:<P><a class="ulink" href="../lib/module-unittest.html"  ><tt class="module">unittest</tt></a> 模块不像 <tt class="module">doctest</tt> 模块那么容易使用,不过它可以在一个独立的文件里提供一个更全面的测试集。<P><div class="verbatim"><pre>
import unittest

class TestStatisticalFunctions(unittest.TestCase):

    def test_average(self):
        self.assertEqual(average([20, 30, 70]), 40.0)
        self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
        self.assertRaises(ZeroDivisionError, average, [])
        self.assertRaises(TypeError, average, 20, 30, 70)

unittest.main() # Calling from the command line invokes all tests</pre></div><P><H1><A NAME="SECTION00121200000000000000000"></A><A NAME="batteries-included"></A><BR>10.12 Batteries Included</H1><P>Python has a ``batteries included'' philosophy.  This is best seen
through the sophisticated and robust capabilities of its larger
packages. For example:<P>Python 体现了“batteries included”哲学。Python 可以通过更大的包的来得到应付各种复杂情况的强大能力,从这一点我们可以看出该思想的应用。例如:<P>* The <a class="ulink" href="../lib/module-xmlrpclib.html"  ><tt class="module">xmlrpclib</tt></a> and
<a class="ulink" href="../lib/module-SimpleXMLRPCServer.html"  ><tt class="module">SimpleXMLRPCServer</tt></a>
modules make implementing remote procedure calls into an almost trivial
task.  Despite the names, no direct knowledge or handling of XML is needed.<P>* <a class="ulink" href="../lib/module-xmlrpclib.html"  ><tt class="module">xmlrpclib</tt></a> 和
<a class="ulink" href="../lib/module-SimpleXMLRPCServer.html"  ><tt class="module">SimpleXMLRPCServer</tt></a>
模块实现了在琐碎的任务中调用远程过程。尽管有这样的名字,其实用户不需要直接处理
XML ,也不需要这方面的知识。<P>* The <a class="ulink" href="../lib/module-email.html"  ><tt class="module">email</tt></a>
package is a library for managing email messages,
including MIME and other RFC 2822-based message documents.  Unlike
<tt class="module">smtplib</tt> and <tt class="module">poplib</tt> which actually send and receive
messages, the email package has a complete toolset for building or
decoding complex message structures (including attachments)
and for implementing internet encoding and header protocols.<P>* <a class="ulink" href="../lib/module-email.html"  ><tt class="module">email</tt></a> 包是一个邮件消息管理库,可以处理 MIME 或其它基于 RFC 2822 的消息文档。不同于实际发送和接收消息的 <tt class="module">smtplib</tt> 和 <tt class="module">poplib</tt> 模块,email 包有一个用于构建或解析复杂消息结构(包括附件)以及实现互联网编码和头协议的完整工具集。<P>* The <a class="ulink" href="../lib/module-xml.dom.html"  ><tt class="module">xml.dom</tt></a> and
<a class="ulink" href="../lib/module-xml.sax.html"  ><tt class="module">xml.sax</tt></a> packages provide
robust support for parsing this popular data interchange format.  Likewise,
the <tt class="module">csv</tt> module supports direct reads and writes in a common
database format.  Together, these modules and packages greatly simplify
data interchange between python applications and other tools.<P>* <a class="ulink" href="../lib/module-xml.dom.html"  ><tt class="module">xml.dom</tt></a> 和 <a class="ulink" href="../lib/module-xml.sax.html"  ><tt class="module">xml.sax</tt></a> 包为流行的信息交换格式提供了强大的支持。同样, <tt class="module">csv</tt> 模块支持在通用数据库格式中直接读写。综合起来,这些模块和包大大简化了
Python 应用程序和其它工具之间的数据交换。<P>* Internationalization is supported by a number of modules including
<a class="ulink" href="../lib/module-gettext.html"  ><tt class="module">gettext</tt></a>,
<a class="ulink" href="../lib/module-locale.html"  ><tt class="module">locale</tt></a>, and the
<a class="ulink" href="../lib/module-codecs.html"  ><tt class="module">codecs</tt></a> package.<P>* 国际化由 <a class="ulink" href="../lib/module-gettext.html"  ><tt class="module">gettext</tt></a>,
<a class="ulink" href="../lib/module-locale.html"  ><tt class="module">locale</tt></a>和
<a class="ulink" href="../lib/module-codecs.html"  ><tt class="module">codecs</tt></a> 包支持。<P><DIV CLASS="navigation"><div class='online-navigation'><p></p><hr /><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="9. 类 Classes"  href="node11.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="11. What Now?"  href="node13.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node11.html">9. 类 Classes</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node13.html">11. What Now?</A></div></div><hr /></DIV><!--End of Navigation Panel--><ADDRESS>译者:刘鑫(march.liu AT gmail DOT com) 由:limodou转(limodou AT gmail DOT com)<br>See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

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