got*_*nes 21 python macos configuration interpreter tab-completion
几个月前,我写了一篇博客文章,详细介绍了如何在标准的Python交互式解释器中实现tab-completion - 这个功能我曾经认为只在IPython中可用.由于IPython unicode问题,我有时不得不切换到标准解释器,因此我发现它非常方便.
最近我在OS X中做了一些工作.令我不满的是,脚本似乎不适用于OS X的终端应用程序.我希望你们中的一些有OS X经验的人可以帮助我解决问题,这样它也可以在终端中运行.
我正在复制下面的代码
import atexit
import os.path
try:
import readline
except ImportError:
pass
else:
import rlcompleter
class IrlCompleter(rlcompleter.Completer):
"""
This class enables a "tab" insertion if there's no text for
completion.
The default "tab" is four spaces. You can initialize with '\t' as
the tab if you wish to use a genuine tab.
"""
def __init__(self, tab=' '):
self.tab = tab
rlcompleter.Completer.__init__(self)
def complete(self, text, state):
if text == '':
readline.insert_text(self.tab)
return None
else:
return rlcompleter.Completer.complete(self,text,state)
#you could change this line to bind another key instead tab.
readline.parse_and_bind('tab: complete')
readline.set_completer(IrlCompleter('\t').complete)
# Restore our command-line history, and save it when Python exits.
history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
readline.read_history_file(history_path)
atexit.register(lambda x=history_path: readline.write_history_file(x))
Run Code Online (Sandbox Code Playgroud)
请注意,我从我的博客文章中的版本稍微编辑了它,以便IrlCompleter使用真正的选项卡初始化,这似乎是终端中Tab键输出的内容.
B0r*_*0rG 56
这应该在Leopard的python下工作:
import rlcompleter
import readline
readline.parse_and_bind ("bind ^I rl_complete")
Run Code Online (Sandbox Code Playgroud)
而这个不是:
import readline, rlcompleter
readline.parse_and_bind("tab: complete")
Run Code Online (Sandbox Code Playgroud)
将其保存在〜/ .pythonrc.py中并在.bash_profile中执行
export PYTHONSTARTUP=$HOME/.pythonrc.py
Run Code Online (Sandbox Code Playgroud)
Far*_*Kon 11
这是Windows/OS X/Linux的一个完整的跨平台版本的加载选项卡完成:
#Code UUID = '9301d536-860d-11de-81c8-0023dfaa9e40'
import sys
try:
import readline
except ImportError:
try:
import pyreadline as readline
# throw open a browser if we fail both readline and pyreadline
except ImportError:
import webbrowser
webbrowser.open("http://ipython.scipy.org/moin/PyReadline/Intro#line-36")
# throw open a browser
#pass
else:
import rlcompleter
if(sys.platform == 'darwin'):
readline.parse_and_bind ("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
Run Code Online (Sandbox Code Playgroud)
来自http://www.farmckon.net/?p=181