非常教程

Python参考手册

可选操作系统 | Optional Operating System

readline

readline模块定义了许多函数以方便Python解释器完成和读取/写入历史文件。该模块可以直接使用,也可以通过rlcompleter支持在交互式提示符下完成Python标识符的模块使用。使用此模块进行的设置会影响解释程序的交互式提示raw_input()input()内置函数提供的提示的行为。

注意

底层Readline库API可以由libedit库而不是GNU readline 来实现。在MacOS X上,readline模块会在运行时检测正在使用哪个库。

配置文件libedit与GNU readline 的配置文件不同。如果以编程方式加载配置字符串,则可以检查文本“libedit” readline.__doc__以区分GNU readline和libedit。

Readline键绑定可以通过初始化文件进行配置,通常.inputrc在您的主目录中。请参阅GNU Readline手册中的Readline Init File,了解该文件的格式和允许的结构以及一般Readline库的功能。

1.初始化文件

以下函数与init文件和用户配置有关:

readline.parse_and_bind(string)

执行字符串参数中提供的init行。这调用rl_parse_and_bind()了底层库。

readline.read_init_file([filename])

执行一个readline初始化文件。默认文件名是最后使用的文件名。这调用rl_read_init_file()了底层库。

2.行缓冲区

以下功能在线路缓冲区上运行:

readline.get_line_buffer()

返回行缓冲区的当前内容(rl_line_buffer在底层库中)。

readline.insert_text(string)

将文本插入光标位置的行缓冲区中。这会调用rl_insert_text()底层库,但会忽略返回值。

readline.redisplay()

更改屏幕上显示的内容以反映行缓冲区的当前内容。这调用rl_redisplay()了底层库。

3.历史文件

以下功能在历史文件上运行:

readline.read_history_file([filename])

加载一个readline历史文件,并将其附加到历史列表中。默认文件名是~/.history。这调用read_history()了底层库。

readline.write_history_file([filename])

将历史列表保存到readline历史文件,覆盖任何现有文件。默认文件名是~/.history。这调用write_history()了底层库。

readline.get_history_length()readline.set_history_length(length)

设置或返回所需的行数以保存在历史文件中。该write_history_file()函数使用此值通过调用history_truncate_file()底层库来截断历史文件。负值意味着无限的历史文件大小。

4.历史清单

以下功能在全局历史列表上运行:

readline.clear_history()

清除当前的历史记录。这调用clear_history()了底层库。如果Python是为支持它的库的一个版本编译的,Python函数才存在。

2.4版本中的新功能。

readline.get_current_history_length()

返回历史中当前项目的数量。(这get_history_length()与之不同,它返回将写入历史文件的最大行数。)

2.3版本的新功能。

readline.get_history_item(index)

索引处返回历史项目的当前内容。项目索引是基于一个的。这调用history_get()了底层库。

2.3版本的新功能。

readline.remove_history_item(pos)

从历史记录中删除由其位置指定的历史记录项目。该职位是从零开始的。这调用remove_history()了底层库。

2.4版本中的新功能。

readline.replace_history_item(pos, line)

通过替换与位置指定的历史项目线。该职位是从零开始的。这调用replace_history_entry()了底层库。

2.4版本中的新功能。

readline.add_history(line)

追加到历史记录缓冲区,就好像它是最后一行输入一样。这调用add_history()了底层库。

5.启动挂钩

2.3版本的新功能。

readline.set_startup_hook([function])

设置或移除由rl_startup_hook基础库的回调调用的函数。如果指定了函数,它将被用作新的钩子函数;如果省略或者None已经安装的任何功能被删除。在readline打印第一个提示之前,钩子被调用时没有参数。

readline.set_pre_input_hook([function])

设置或移除由rl_pre_input_hook基础库的回调调用的函数。如果指定了函数,它将被用作新的钩子函数; 如果省略或者None已经安装的任何功能被删除。在第一个提示已被打印之后,在readline开始读取输入字符之前,钩子被调用时没有参数。这个函数只有在Python为支持它的库版本编译时才存在。

6.完成

以下功能与实现自定义字完成功能有关。这通常由Tab键操作,并且可以建议并自动完成输入的单词。默认情况下,Readline被设置为用于rlcompleter为交互式解释器完成Python标识符。如果该readline模块要与自定义完成器一起使用,则应设置一组不同的单词分隔符。

readline.set_completer([function])

设置或删除完成者功能。如果指定了函数,它将用作新的完成函数; 如果省略,或者None已经安装的完成程序功能被删除。完成者函数被调用为function(text, state),对国家012,...,直到它返回一个非字符串值。它应该返回从文本开始的下一个可能的完成。

已安装的完成程序功能由传入到基础库中的entry_func回调调用rl_completion_matches()。该文本字符串来自于第一个参数rl_attempted_completion_function的基础库的回调。

readline.get_completer()

获取完成功能,或者None如果没有设置完成功能。

2.3版本的新功能。

readline.get_completion_type()

获取正在尝试的完成类型。这会rl_completion_type以整数形式返回底层库中的变量。

2.6版本中的新功能。

readline.get_begidx()readline.get_endidx()

获取完成范围的开始或结束索引。这些索引是传递给底层库回调的开始结束参数rl_attempted_completion_function

readline.set_completer_delims(string)readline.get_completer_delims()

设置或获取单词分隔符以完成。这些决定了要考虑完成的单词的开始(完成范围)。这些函数访问rl_completer_word_break_characters底层库中的变量。

readline.set_completion_display_matches_hook([function])

设置或删除完成显示功能。如果指定了功能,它将用作新的完成显示功能; 如果省略,或者None已经安装的任何完成显示功能被删除。这会设置或清除rl_completion_display_matches_hook底层库中的回调。完成显示功能被称为function(substitution, [matches], longest_match_length)每次匹配需要显示一次。

2.6版本中的新功能。

7.例子

以下示例演示如何使用readline模块的历史读取和写入功能自动加载并保存.pyhist从用户主目录命名的历史文件。下面的代码通常会在用户PYTHONSTARTUP文件的交互式会话期间自动执行。

import os
import readline
histfile = os.path.join(os.path.expanduser("~"), ".pyhist")
try:
    readline.read_history_file(histfile)
    # default history len is -1 (infinite), which may grow unruly
    readline.set_history_length(1000)
except IOError:
    pass
import atexit
atexit.register(readline.write_history_file, histfile)
del os, histfile

以下示例将该code.InteractiveConsole类扩展为支持历史保存/恢复。

import code
import readline
import atexit
import os

class HistoryConsole(code.InteractiveConsole):
    def __init__(self, locals=None, filename="<console>",
                 histfile=os.path.expanduser("~/.console-history")):
        code.InteractiveConsole.__init__(self, locals, filename)
        self.init_history(histfile)

    def init_history(self, histfile):
        readline.parse_and_bind("tab: complete")
        if hasattr(readline, "read_history_file"):
            try:
                readline.read_history_file(histfile)
            except IOError:
                pass
            atexit.register(self.save_history, histfile)

    def save_history(self, histfile):
        readline.set_history_length(1000)
        readline.write_history_file(histfile)

可选操作系统 | Optional Operating System相关

Python

Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人 Guido van Rossum 于1989年发明,第一个公开发行版发行于1991年。 Python 是纯粹的自由软件, 源代码和解释器 CPython 遵循 GPL 协议。Python 语法简洁清晰,特色之一是强制用空白符( white space )作为语句缩进。

主页 https://www.python.org/
源码 https://github.com/python/cpython
版本 2.7
发布版本 2.7.13

Python目录

1.内置常量 | Built-in Constants
2.内置例外 | Built-in Exceptions
3.内置函数 | Built-in Functions
4.内置类型 | Built-in Types
5.编译器 | Compiler
6.加密 | Cryptography
7.数据压缩 | Data Compression
8.数据持久性 | Data Persistence
9.数据类型 | Data Types
10.调试和分析 | Debugging & Profiling
11.开发工具 | Development Tools
12.文件和目录访问 | File & Directory Access
13.文件格式 | File Formats
14.构架 | Frameworks
15.输入 | Importing
16.输入/输出 | Input/ouput
17.国际化 | Internationalization
18.网络 | Internet
19.网络数据 | Internet Data
20.翻译 | Interpreters
21.语言 | Language
22.记录 | Logging
23.Mac OS
24.MS Windows
25.多媒体 | Multimedia
26.联网 | Networking
27.数字与数学 | Numeric & Mathematical
28.操作系统 | Operating System
29.可选操作系统 | Optional Operating System
30.限制执行 | Restricted Execution
31.运行 | Runtime
32.SGI IRIX
33.软件包装与分销 | Software Packaging & Distribution
34.字符串 | String
35.结构化标记 | Structured Markup
36.Tk
37.Unix
38.Python 简介
39.Python pass 语句
40.Python 循环嵌套
41.Python 运算符
42.Python log10() 函数
43.Python log() 函数
44.Python floor() 函数
45.Python fabs() 函数
46.Python exp() 函数
47.Python cmp() 函数
48.Python ceil() 函数
49.Python abs() 函数
50.Python Number(数字)
51.Python pow() 函数
52.Python modf() 函数
53.Python min() 函数
54.Python max() 函数
55.Python asin() 函数
56.Python acos() 函数
57.Python uniform() 函数
58.Python shuffle() 函数
59.Python seed() 函数
60.Python random() 函数
61.Python randrange() 函数
62.Python choice() 函数
63.Python sqrt() 函数
64.Python round() 函数
65.Python radians() 函数
66.Python degrees() 函数
67.Python tan() 函数
68.Python sin() 函数
69.Python hypot() 函数
70.Python cos() 函数
71.Python atan2() 函数
72.Python atan() 函数
73.Python 元组
74.Python 列表(List)
75.Python 字符串
76.Python 字典(Dictionary)
77.Python 日期和时间
78.Python 函数
79.Python 模块
80.Python capitalize()方法
81.Python center()方法
82.Python count() 方法
83.Python expandtabs()方法
84.Python endswith()方法
85.Python encode()方法
86.Python decode()方法
87.Python find()方法
88.Python index()方法
89.Python 异常处理
90.Python isspace()方法
91.Python isnumeric()方法
92.Python islower()方法
93.Python isdigit()方法
94.Python isalpha()方法
95.Python isalnum()方法
96.Python isupper()方法
97.Python istitle()方法
98.Python min()方法
99.Python max()方法
100.Python maketrans()方法