readline模块定义了一系列函数用来读写Python解释器中历史命令,并提供自动补全命令功能。这个模块可以通过relcompleter模块直接调用,模块中的设置会影响解释器中的交互提示,以及内置函数raw_input()和input()提供的提示。

        readline模块定义了以下方法:
        readline.parse_and_bind(string):解析并执行命令行初始化文件。
        readline.get_line_buffer():返回当前命令行缓存的内容。
        readline.insert_text(string):插入到当前行。
        readline.read_init_file([filename]):解析一个命令行初始化文件。
        readline.read_history_file([filename]):读取历史命令文件,默认为~/.history
        readline.write_history_file([filename]):保存历史命令文件,默认为~/.history
        readline.get_history_length():获取预设的历史命令条数。负数意味着不限制条数大小。
        readline.set_history_length(length):设置要保存到历史命令文件中的命令条数,write_history_file()使用这个数字对历史命令文件进行修改。
        readline.get_current_history_length():返回当前历史文件中历史命令的条数。
        readline.get_history_item(index):获取index索引指定的历史命令。
        readline.remove_history_item(pos):删除指定位置的历史命令。
        readline.replace_history_item(pos, line) :使用给定命令替换指定位置的命令。
        readline.redisplay() :根据命令行缓存实时更新当前屏幕的显示。
        readline.set_startup_hook([function]) :设置或删除钩子函数,如果指定了函数,就将其设为钩子函数,如果没有指定或者设置为None,所有已经安装的钩子函数将被移除,钩子函数在命令行输出提示前执行。
        readline.set_pre_input_hook([function]):跟set_startup_hook()方法类似,但是钩子函数是在提示输入完之后,命令行开始读取字符串之前执行。
        readline.set_completer([function]):如果提供了函数,则用作自动完成命令函数,如果忽略或者设置为None,则移除之前设置的函数。命令自动完成函数形式如function(text,state),text为命令行中输入的字符串,state为选择的的补全命令索引。
        readline.get_completer():返回自动完成命令函数。
        readline.get_completion_type() :返回自动完成的类型。
        readline.get_begidx() :获取命令行tab自动补全范围的第一个值的索引。
        readline.get_endidx() :获取命令行tab自动补全范围的最后一个值的索引。
        readline.set_completer_delims(string) :设置自动补全命令之间的分隔符。
        readline.get_completer_delims() :获取分隔符。
        readline.set_completion_display_matches_hook([function]) :设置或者移除自动完成显示函数。
        readline.add_history(line) :添加最后一条输入的命令到历史文件中。
        示例:
        下面的例子使用readline模块从.pyhist中读取历史命令,并自动保存历史命令到这个文件中。
  1. import os
  2. histfile = os.path.join(os.environ["HOME"],".pyhist")
  3. try:
  4. readline.read_history_file(histfile)
  5. exceptIOError:
  6. pass
  7. import atexit
  8. atexit.register(readline.write_history_file, histfile)
  9. del os, histfile
        下面的例子通过继承code.InteractiveConsole来支持历史命令的读写。
  1. import code
  2. import readline
  3. import atexit
  4. import os
  5. classHistoryConsole(code.InteractiveConsole):
  6. def __init__(self, locals=None, filename="<console>",
  7. histfile=os.path.expanduser("~/.console-history")):
  8. code.InteractiveConsole.__init__(self, locals, filename)
  9. self.init_history(histfile)
  10. def init_history(self, histfile):
  11. readline.parse_and_bind("tab: complete")
  12. if hasattr(readline,"read_history_file"):
  13. try:
  14. readline.read_history_file(histfile)
  15. exceptIOError:
  16. pass
  17. atexit.register(self.save_history, histfile)
  18. def save_history(self, histfile):
  19. readline.write_history_file(histfile)

GNU自动补全模块readline解析的更多相关文章

  1. 命令自动补全模块rlcomplete

    rlcomplete定义了针对readline模块的命令自动补全函数.         当在unix平台下导入这个模块之后(前提是readline模块可用),一个Complete的实例会自动生成,并且 ...

  2. Sublime Text 3 import Anaconda 无法正常补全模块名解决办法

    Sublime Text 3 Anaconda配置 在安装Sublime Text3之后我们总会安装一些插件,比如Python的Anaconda自动补全插件.但是,装好之后发现import 时无法像别 ...

  3. (转)模块readline解析

    模块readline解析 原文:https://www.cnblogs.com/fireflow/p/4841413.html readline模块定义了一系列函数用来读写Python解释器中历史命令 ...

  4. Python自动补全

    转自:http://blog.linuxeye.com/324.html Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim ...

  5. vim编辑下Python2.0自动补全

    Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim python自动补全插件:pydiction 可以实现下面python代码 ...

  6. python命令行添加Tab键自动补全

    1.编写一个tab的自动补全脚本,名为tab.py #!/usr/bin/python # python tab complete import sys import readline import ...

  7. Windows 下python的tab自动补全

    方法一:安装一个ipython就OK啦,而且关键字还能高亮显示呢 一.打开cmd,输入pip3 install ipython联网安装 二.安装成功后,cmd里运行ipython,成功啦. 方法二:写 ...

  8. Python 自动补全(vim)

    一.vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.p ...

  9. AutoCompleteTextView自动补全文本框

    AutoCompleteTextView的作用是在输入框中输入我们想要输入的信息,就会出现其他与其相关的提示信息 下面是实例代码: MainActivity.java package com.shao ...

随机推荐

  1. DatabaseMetaData的用法(转)

    http://blog.csdn.net/sdliubo/article/details/6546889

  2. Delphi2010的RTTI增强

    Delphi编译的文件体积增大了很多.很大一部分原因是因为Delphi2010默认提供了全信息的RTTI. 每一个数据类型都有全部运行时信息.例如可以在运行时获得结构体的成员以及成员类型等. 这个功能 ...

  3. ubuntu12.04&15.04 安装lamp(12.04为主)

    ubuntu 12.04&15.04下安装lamp环境 注意:如果是ubuntu15.04下,apache2.4.10的话,直接在/etc/apache2/apache2.conf文件的后边直 ...

  4. request.getParameterValues与request.getParameter的差别

    一. 简单的对照 request.getParameter用的比較多,相对熟悉 request.getParameterValues(String   name)是获得如checkbox类(名字同样, ...

  5. struts Value Stack 值栈

    首先声明:本文是从博客园博友的文章转载过来的,感觉说的不错.在此附上地址:http://www.cnblogs.com/jerryxing/archive/2012/04/23/2467299.htm ...

  6. HDU1250:Hat's Fibonacci

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  7. CSS learnning...

    "Whenever this property changes, apply that change slowly." The property transition: width ...

  8. //相当于深拷贝一份dataArray。这样才不会改变dataArray本身的值

    //相当于深拷贝一份dataArray.这样才不会改变dataArray本身的值 NSMutableArray* commitDataArray = [NSKeyedUnarchiver unarch ...

  9. C++ 字符串指针与字符串数组

    在做面试100题中第21题时,发现char *astr="abcdefghijk\0";和char astr[]={"abcdefghijk"};有点区别,以前 ...

  10. 【转】DevExpress控件安装

    原文链接: DevExpress控件安装.汉化使用教程 - 田园里的蟋蟀 学习网址: 1.DevExpress控件中文网 2.DevExpress控件中文网使用教程 3.DevExpress控件使用经 ...