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

📄 117.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<!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 Essential Reference, Second Edition -&gt; Miscellaneous Modules</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.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="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="105.html" class="navtitle">A. The Python Library</a> &gt; <span class="nonavtitle">Miscellaneous Modules</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="116.html" title="Restricted Execution"><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=0735710910&snode=117" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="117.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="118.html" title="The Python Debugger"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A07%3A51+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105081080059255003146110</font><a href="read7.asp?bookname=0735710910&snode=117&now=5%2F28%2F2002+9%3A07%3A51+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Miscellaneous Modules</h3>
<p>The modules in this category are used for miscellaneous tasks that don抰 fit into any of the other categories.</p>


<H4><TT Class="monofont">bisect</TT></H4>
<P>The <tt clASS="monofont">bisect</Tt>  module provides support for keeping lists in sorted order. It uses a bisection algorithm to do most of its work.</p>

<pre>

<b>bisect(</b><b><i>list</i></b><b>,</b> <b><i>item</i></b> <b>[,</b> <b><I>low</i></b> <B>[,</b> <b><i>high</I></b><b>]])</b> </pRe>

<p>Returns the index of the proper insertion point for <i><TT CLass="monofont">item</tT></I>
 to be placed in <I><Tt claSS="monofont">list</TT></i>
 in order to maintain <i><tt CLASs="monofont">list</tt></i>
 in sorted order. <i><tt class="monofont">low</tt></i>
 and <i><tt clAss="monofont">high</Tt></i>
 are indices specifying a subset of the list to be considered.</p>

<Pre>

<b>insort(</b><B><i>list</i></b><B>,</B> <B><I>item</i></b> <b>[,</b> <B><I>low</I></B> <b>[,</b> <b><i>high</I></B><B>]])</B> </pre>

<p>Inserts <I><TT Class="monofont">item</tt></i>
 into <i><tt class="monofont">list</tt></i>
 in sorted order.</p>

<a NamE="4"></a>
<h4><tT clasS="monofont">cmd</tt></h4>
<P>The <TT Class="monofont">cmd</TT>  module provides a class <TT clasS="monofont">Cmd</TT>  that抯 used as a framework for building a line-oriented command interpreter. The <Tt claSS="monofont">Cmd</TT>  class is never instantiated directly, but is used as a base class for a class that actually implements the interpreter. An instance <i><tt class="monofont">c</tt></i>
 of the <tt class="monofont">Cmd</tt>  class provides the following methods:</P>

<prE>

<b><i>c</i></B><b>.cmdloop([</b><b><i>intro</I></b><b>])</b> </PRE>

<P>Prints a banner message contained in <i><tt cLASS="monofont">intro</tt></i>
 and repeatedly issues a prompt, reads a line of input, and dispatches an appropriate action. For each line of text, the first word is stripped off and used as a command name. For a command name of <tT CLAss="monofont">'foo'</tt>, an attempt is made to invoke a method <TT CLass="monofont">do_foo()</tt> with the remainder of the input line as a string argument. If a line contains only the character <tt class="monofont">'?'</tt>, a predefined method <tt clasS="monofont">do_help()</tt> is dispatched. If the command name is <Tt clAss="monofont">'!'</tt>, a method <Tt clASS="monofont">do_shell()</Tt> is invoked (if defined). An end-of-file is converted into a string <tt cLASS="monofont">'EOF'</tt> and dispatched to a command <tt CLASs="monofont">do_EOF</tt>.</p>

<P>Subclasses of <TT Class="monofont">Cmd</tt>  inherit a predefined method <tt class="monofont">do_help()</tt>. When this method is invoked with an argument <tt claSs="monofont">'bar'</tT>, it tries to invoke the method <tt cLass="monofont">help_bar()</tT>. With no arguments, <tt cLASS="monofont">do_help()</tt> lists all the available help topics by listing all commands with corresponding <tt CLASs="monofont">help_*</tt> methods, undocumented commands (commands without corresponding <tT CLAss="monofont">help_*</tt> methods), and miscellaneous topics (help methods without a corresponding command). Each of the command methods should return an integer code indicating success or failure. A negative value indicates an error and causes the interpreter to return. Otherwise, the interpreter continues to read input after each command. If the <TT CLass="monofont">readline</tt>  module has been loaded, the command interpreter will have line editing and history capabilities.</p>

<pre>

<b><i>c</i></b><b>.onecmd(</b><b><i>str</i></b><b>)</B> </prE>

<p>Interprets <i><tT clasS="monofont">str</tt></i>
 as a single line of input.</P>

<PRE>

<b><i>c</i></b><B>.emptyline()</B> </PRe>

<p>This method is called when an empty line of input is typed. It should be defined by the user. If not overridden, it repeats the last nonempty command entered.</p>

<pRE>

<B><I>c</i></b><b>.default(</b><B><I>line</I></B><b>)</b> </pre>

<p>Called when an unrecognized command is typed. By default, it prints an error message and exits.</p>

<pre>

<b><i>c</i></b><b>.precmd()</b> </pre>

<p>Method executed just before the input prompt is issued. It should be overridden by derived classes.</P>

<prE>

<b><i>c</i></B><b>.postcmd()</b> </prE>

<p>Method executed immediately after a command dispatch has finished.</p>

<pRE>

<B><I>c</i></b><b>.preloop()</b> </PRE>

<P>Method executed once when <tt clASS="monofont">cmdloop()</Tt> is executed.</p>

<prE>

<B><I>c</I></b><b>.postloop()</b> </pre>

<p>Method executed when <tt class="monofont">cmdloop()</tt> is about to return.</p>

<p>The following instance variables should also be defined by a subclass of <tt ClaSs="monofont">Cmd</tt>.</P>

<p><tabLe boRDER="1" cellSPACing="0" cELLPaddiNG="1" WIdth="100%">
<colgroup span="2">
<tr>
<th vAliGn="top">
<foNt sizE="2">
<p><b>Variable</b></P>
</FONt></th>
<tH VALign="top">
<fONT Size="2">
<p><B>Description</B></P>
</Font></th>
</tr>
<tr>
<td valign="top">
<fonT siZe="2">
<p><i><Tt claSs="monofont">c</tt></I>
<TT Class="monofont">.prompt</TT></P>
</Font></tD>
<TD ValigN="top">
<FONt size="2">
<p>Prompt printed to solicit input.</p>
</font></td>
</tr>
<tr>
<td vAliGn="top">
<foNt sizE="2">
<p><i><tT CLAss="monofont">c</tt></I>
<TT Class="monofont">.identchars</TT></P>
</Font></tD>
<TD Valign="top">
<font size="2">
<p>String of characters accepted for the command prefix.</p>
</font></tD>
</tr>
<Tr>
<td ValigN="top">
<fonT SIZe="2">
<p><i><tT CLAss="monofont">c</tt></I>
<TT Class="monofont">.lastcmd</TT></P>
</Font></td>
<td valign="top">
<font sizE="2">
<p>Last nonempty command seen.</p>
</Font></Td>
</tr>
<tR>
<td vALIGn="top">
<fonT SIZe="2">
<p><i><tT CLAss="monofont">c</tt></I>
<TT Class="monofont">.intro</tt></p>
</font></td>
<td valigN="top">
<foNt siZe="2">
<p>Intro text banner; overridden using the argument to <tt ClasS="monofont">cmdloop()</TT>.</P>
</font></TD>
</TR>
<tr>
<td VALIgn="top">
<foNT SIze="2">
<p><i><tt class="monofont">c</tt></i>
<tt clasS="monofont">.doc_header</tt></P>
</fonT></td>
<td ValiGN="top">
<FOnt siZE="2">
<P>Header to issue if the help section has a section for documented commands.</P>
</font></TD>
</TR>
<tr>
<td VALIgn="top">
<font size="2">
<p><i><tt class="monofont">c</tT></i>
<tT claSs="monofont">.misc_header</tt></p>
</Font></TD>
<TD valiGN="top">
<FOnt siZE="2">
<P>Header to issue for miscellaneous help topics.</P>
</font></TD>
</TR>
<tr>
<td valign="top">
<font size="2">
<p><i><Tt cLass="monofont">c</Tt></i>
<tt ClasS="monofont">.undoc_header</TT></P>
</font></TD>
<TD valiGN="top">
<FOnt siZE="2">
<P>Header for undocumented commands.</P>
</font></td>
</tr>
<tr>
<td valign="top">
<foNt sIze="2">
<p><I><tt clAss="monofont">c</tT></I>
<TT clasS="monofont">.ruler</TT></P>
</font></TD>
<TD valiGN="top">
<FOnt size="2">
<p>Character used to draw separator lines under help message headers. If empty, no ruler line is drawn. The default is <tt class="monofont">'='</tt>.</p>
</fonT></td>
</Tr>
</coLgrouP>
</tabLE></P>


<H5>Example</h5>
<p>The following example shows how this module can be used to implement an interpreter wrapper around the callable objects of a module. It also shows the interesting feature of code being executed in a class definition.</p>

<pRE>

# cmdhelp.py 
# Builds a command interpreter that allows arbitrary Python 
# commands to be typed, but reads their doc strings to create 
# a collection of help commands. Just do an execfile(cmdhelp.py) 
# in a module to utilize this. 

import cmd, sys, traceback 

# Define the interpreter class 
class Interpreter(cmd.Cmd): 
      symbols = globals() 
      prompt = "?&gt;&gt; " 
      intro = "Interpreter for " + _ _name_ _ 

      # Find all of the callable objects and look for 
      # their doc strings   
       for n in symbols.keys(): 
             c = symbols[n] 
             if callable(c): 
                 if c._ _doc_ _: 
                      exec """ 
def help_%s(self):print %s._ _doc_ _ 
""" % (n,n) 

     # Execute an arbitrary statement 
     def default(self,l): 
         try: 
             exec self.lastcmd in globals() 
         except: 
             traceback.print_exc() 

     # Do nothing on empty line 
     def emptyline(self): 
         pass 

     def do_EOF(self,arg): 
         return -1 

# Create an instance 
interp = Interpreter() </PRe>

<p>The following code shows how this code might be used:</p>

<pRE>

Python 2.0 (#1, Feb 25 2001, 07:54:16) 
[GCC 2.95.2 19991024 (release)] on sunos5 
Type "copyright", "credits" or "license" for more information. 
&gt;&gt;&gt; from socket import * 
&gt;&gt;&gt; execfile("cmdhelp.py") 
&gt;&gt;&gt; interp.cmdloop() 
Interpreter for _ _main_ _ 
?&gt;&gt; help 
Miscellaneous help topics: 

⌨️ 快捷键说明

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