欢迎您来到GIS动力

加入收藏 免费注册 用户登陆 帮助中心
首页 新闻动态 技术专栏 银杏树下 学习考研 软件下载 求职招聘 许愿瓶 节日祝福 用户中心 精彩推荐 资源搜索 地图
专栏导航: AO开发 | SO开发 | ArcGIS桌面 | 超图桌面 | 开发语言 | 数据库 | WebGIS | 银杏文学 | 研究生考题 | FreeMap 谈天说地
   您现在位于: 首页技术专栏开发语言 → 正文
Python教程之接下来?
07-12-14 00:00:00 作者:半块点心 出处:本站原创
10. What Now?
Reading this  tutorial has probably reinforced your interest in using Python -- you should be eager to apply Python to solve yourreal-world  problems. Now what should you do?
You should read, or at least page through, the Python Library Reference, which gives  complete (though terse) reference material about types, functions, and modules that can save you a lot of time when writing  Python programs. The standard Python distribution includes a lot of code in both C and Python; there are modules to read Unix  mailboxes, retrieve documents via HTTP, generate random numbers, parse command-line options, write CGI programs, compress  data, and a lot more; skimming through the Library Reference will give you an idea of what's available.
The major Python Web  site is http://www.python.org/; it contains code, documentation, and pointers to Python-related pages around the Web. This  Web site is mirrored in various places around the world, such as Europe, Japan, and Australia; a mirror may be faster than  the main site, depending on your geographical location. A more informal site is http://starship.python.net/, which contains a  bunch of Python-related personal home pages; many people have downloadable software there. Many more user-created Python  modules can be found in a third-party repository at http://www.vex.net/parnassus.
For Python-related questions and problem  reports, you can post to the newsgroup comp.lang.python, or send them to the mailing list at python-list@python.org. The  newsgroup and mailing list are gatewayed, so messages posted to one will automatically be forwarded to the other. There are  around 120 postings a day (with peaks up to several hundred), asking (and answering) questions, suggesting new features, and  announcing new modules. Before posting, be sure to check the list of Frequently Asked Questions (alsocalled the FAQ), at  http://www.python.org/doc/FAQ.html, or look for it in the Misc/ directory of the Python source distribution. Mailing list  archives are available at http://www.python.org/pipermail/. The FAQ answers many of the questions that come up again and  again, and may already contain the solution for your problem.

A. Interactive Input Editing and History Substitution
Some  versions of the Python interpreter support editing of the current input line and history substitution, similar to facilities  found in the Korn shell and the GNU Bash shell. This is implemented using the GNU Readline library, which supports Emacs- styleand vi-style editing. This library has its own documentation which I won't duplicate here; however, the basics are  easily explained. The interactive editing and history described here are optionally available in the Unix and CygWin versions  of the interpreter.
This chapter does not document the editing facilities of Mark Hammond's PythonWin package or the Tk- based environment, IDLE, distributed with Python. The command line history recall which operates within DOS boxes on NT and  some other DOS and Windows flavors is yet another beast.
A.1 Line Editing
If supported, input line editing is active  whenever the interpreter prints a primary or secondary prompt. The current line can be edited using the conventional Emacs  control characters. The most important of these are: C-A (Control-A) moves the cursor to the beginning of the line, C-E to  the end, C-B moves it one position to the left, C-F to the right. Backspace erases the character to the left of the cursor,  C-D the character to its right. C-K kills (erases) the rest of the line to the right of the cursor, C-Y yanks back the last  killed string. C-underscore undoes the last change you made; it can be repeated for cumulative effect.
A.2 History  Substitution
History substitution works as follows. All non-empty input lines issued are saved in a history buffer, and when  a new prompt is given you are positioned on a new line at the bottom of this buffer. C-P moves one line up (back) in the  history buffer, C-N moves one down. Any line in the history buffer can be edited; an asterisk appears in front of the prompt  to mark a line as modified. Pressing the Return key passes the current line to the interpreter. C-R starts an incremental  reverse search; C-S starts a forward search.
A.3 Key Bindings
The key bindings and some other parameters of the Readline  library can be customized by placing commands in an initialization file called ~/.inputrc. Key bindings have the form
key- name: function-name
or
"string": function-name
and options can be set with
set option-name value
For example:
# I prefer  vi-style editing:
set editing-mode vi
# Edit using a single line:
set horizontal-scroll-mode On
# Rebind some keys:
Meta-h:  backward-kill-word
"\C-u": universal-argument
"\C-x\C-r": re-read-init-file
Note that the default binding for Tab in Python  is to insert a Tab character instead of Readline's default filename completion function. If you insist, you can override this  by putting
Tab: complete
in your ~/.inputrc. (Of course, this makes it harder to type indented continuation lines.)
Automatic completion of variable and module names is optionally available. To enable it in the interpreter's interactive  mode, add the following to your startup file:A.1
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
This  binds the Tab key to the completion function, so hitting the Tab key twice suggests completions; it looks at Python statement  names, the current local variables, and the available module names. For dotted expressions such as string.a, it will  evaluatethe the expression up to the final "."and then suggest completions from the attributes of the resulting object. Note  that this may execute application-defined code if an object with a __getattr__() method is part of the expression.
A more  capable startup file might look like this example. Note that this deletes the names it creates once they are no longer  needed; this is done since the startup file is executed in the same namespace as the interactive commands, and removing the  names avoids creating side effects in the interactive environments. You may find it convenient to keep some of the imported  modules, such as os, which turn out to be needed in most sessions with the interpreter.
# Add auto-completion and a stored  history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to  the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment  variable to point
# to it: "export PYTHONSTARTUP=/max/home/itamar/.pystartup"in bash.
#
# Note that PYTHONSTARTUP does *not*  expand "~", so you have to put in the
# full path to your home directory.
import atexit
import os
import readline
import  rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
A.4 Commentary
This facility  is an enormous step forward compared to earlier versions of the interpreter; however, some wishes are left: It would be nice  if the proper indentation were suggested on continuation lines (the parser knows if an indent token is required next). The  completion mechanism might use the interpreter's symbol table. A command to check (or even suggest) matching parentheses,  quotes, etc., would also be useful.
发布人:admin
推荐给好友:发送给好友
上篇新闻:
下篇新闻:
相关评论
发表我的评论
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  •   相关文章  
    Python教程之类
    Python教程之错误和异常
    Python教程之输入和输出
    Python教程之模块
    Python教程之数据结构
    Python教程之流程控制
    Python教程之初步认识 Python 

    关于我们 友情链接 ┋ 与我在线 ┋ 管理 ┋ TOP
    网站当前版本:GisPower CMS V3.0
    『GIS 动力』- http://www.gispower.org/
    联系我们:webmaster#gispower.org
    Copyright (c) 2003-2007 GisPOwer.Org. All Rights Reserved.

                   滇ICP备05006901号