一半君的总结纸

听话只听一半君

python tips : rlcompleter & rlcompleter2 tab自动完成

突然发现tab后面自动显示所有symbol不是默认功能… 原来是公司加的啊….

用pymel的时候经常要dir(),快烦死了. 其实可以tab自动补齐的….

如果用python自带的rlcompleter

可以

>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> readline. <TAB PRESSED>
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.

我还以为这是默认效果 …
如果想每次都有这效果,可以把下面的代码放在 PYTHONSTARTUP 指定的 initialization file里

try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

还有个更强的rlcompleter2,不过得另装

pip install rlcompleter2

之后就可以..

>>> import rlcompleter2
>>> rlcompleter2.setup()
Welcome to rlcompleter2 0.98
for nice experiences hit <tab> multiple times
>>> import PyQt4
>>> PyQt4.__
*
<dic>__builtins__    : {'bytearray': <type 'bytearray'>, 'IndexError': <typ
<lis>__path__        : ['/usr/lib64/python2.7/site-packages/PyQt4'], type: 
<non>__doc__         : None, type: <type 'NoneType'>                       
<non>__package__     : None, type: <type 'NoneType'>                       
<str>__file__        : /usr/lib64/python2.7/site-packages/PyQt4/__init__.py
<str>__name__        : PyQt4, type: <type 'str'>       

多按几次tab会有奇特效果…

>>> os.path.abspath(
00 ------------------------------------------------------------------------------
01 abspath(path)
02 Return an absolute path.
03 ------------------------------------------------------------------------------
04 def abspath(path):
05     """Return an absolute path."""
06     if not isabs(path):
07         if isinstance(path, _unicode):
08             cwd = os.getcwdu()
09         else:
10             cwd = os.getcwd()
11         path = join(cwd, path)
12     return normpath(path)
13 

edit: 以上是for linux的,windows没有readline package怎么办,没关系,可以

pip install pyreadline
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: