UNIX系统使用python命令行如何使用table自动补全命令

一、我的python环境

[root@localhost ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help()

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

二、实现原理

rlcompeleter 通过补全有效的 Python 标识符和关键字定义了一个适用于 readline 模块的补全函数。

当此模块在具有可用的 readline 模块的 Unix 平台被导入, 一个 Completer 实例将被自动创建并且它的 complete() 方法将设置为 readline 的补全器.
示例:

import rlcompleter
import readline
readline.parse_and_bind("tab: complete")
readline.
readline.doc readline.get_line_buffer( readline.read_init_file(
readline.file readline.insert_text( readline.set_completer(
readline.name readline.parse_and_bind(
readline.

rlcompleter 模块是为了使用 Python 的 交互模式 而设计的。 除非 Python 是通过 -S 选项运行, 这个模块总是自动地被导入且配置 (参见 Readline configuration)。
在没有 readline 的平台, 此模块定义的 Completer 类仍然可以用于自定义行为.

Completer 对象
Completer 对象具有以下方法:

Completer.complete(text, state)
为 text 返回第 state 项补全。

如果指定的 text 不包含句点字符 (’.’),它将根据当前 main, builtins 和保留关键字(定义于 keyword 模块)所定义的名称进行补全。

如果为带有句点的名称执行调用,它将尝试尽量求值直到最后一部分为止而不产生附带影响(函数不会被求值,但它可以生成对 getattr() 的调用),并通过 dir() 函数来匹配剩余部分。 在对表达式求值期间引发的任何异常都会被捕获、静默处理并返回 None。

三、实现脚本

该功能可通过脚本自动实现,不必每次登陆都执行相同操作;

[root@localhost ~]# cat .pythonrc.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
#--------------------------------------------------
#Author:wyq
#Email:
#FileName:.pythonrc.py
#Function: 
#Version:1.0 
#Created:20210701
#--------------------------------------------------
print "import readline success!!!\nYou can use table now!!!" 
try:
    import readline
except ImportError:
    print "Module readline not found."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

四、手动执行查看结果(可自行修改脚本)

[root@localhost ~]# python .pythonrc.py 
import readline success!!!
You can use table now!!!

五、配置自动生效

echo "export PYTHONSTARTUP=~/.pythonrc.py" >> .bashrc
source .bashrc

六、验证

[root@localhost ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import readline success!!!
You can use table now!!!
>>> 

此时验证已经成功,可以使用table自动补全功能了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值