Welcome, guest | Sign In | My Account | Store | Cart

A very simple module to extend the original rlcompleter module in standard library. After invoke python interpreter and import irlcompleter, you can indent and also complete valid python identifiers and keywords using a single "tab" key!

Note1: original python rlcompleter module only avail in unix-like environment Note2: if you seek a completer with history viewer support and more features you could try Sunjoong LEE's recipe at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496822

Python, 18 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
"""Indentable rlcompleter

Extend standard rlcompleter module to let tab key can indent
and also completing valid Python identifiers and keywords."""

import readline,rlcompleter

class irlcompleter(rlcompleter.Completer):
        def complete(self, text, state):
                if text == "":
                        #you could  replace \t to 4 or 8 spaces if you prefer indent via spaces
                        return ['\t',None][state]
                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().complete)

Original rlcomplete module let python interpreter can complete valid python identifiers and keywords. But then you can't use tab key to indent, Using this module you could then press tab key to indent and also list possible keyword and indentifiers without using dir().

And if you import this module in your startup python script (which can set in $PYTHONSTARTUP environment variable), you can always get a indentable and auto-complete python!

example usage:

localhost # python Python 2.4.3 (#1, Jun 20 2006, 11:15:05) [GCC 4.1.0 (Gentoo 4.1.0-r1)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

>>> import irlcompleter
>>> for i in range(2):
...      ((tab pressed)) print i
...
0
1
>>> s  ((tab pressed twice))
set           slice         staticmethod  sum
setattr       sorted        str           super



>>> import sys
>>> sys.  ((tab pressed twice))
sys.__class__              sys.__stdin__              sys.exec_prefix            sys.path_importer_cache
sys.__delattr__            sys.__stdout__             sys.executable             sys.platform
sys.__dict__               sys.__str__                sys.exit                   sys.prefix
sys.__displayhook__        sys._getframe              sys.getcheckinterval       sys.ps1
sys.__doc__                sys.api_version            sys.getdefaultencoding     sys.ps2
sys.__excepthook__         sys.argv                   sys.getdlopenflags         sys.setcheckinterval
sys.__getattribute__       sys.builtin_module_names   sys.getfilesystemencoding  sys.setdlopenflags
sys.__hash__               sys.byteorder              sys.getrecursionlimit      sys.setprofile
sys.__init__               sys.call_tracing           sys.getrefcount            sys.setrecursionlimit
sys.__name__               sys.callstats              sys.hexversion             sys.settrace
sys.__new__                sys.copyright              sys.maxint                 sys.stderr
sys.__reduce__             sys.displayhook            sys.maxunicode             sys.stdin
sys.__reduce_ex__          sys.exc_clear              sys.meta_path              sys.stdout
sys.__repr__               sys.exc_info               sys.modules                sys.version
sys.__setattr__            sys.exc_type               sys.path                   sys.version_info
sys.__stderr__             sys.excepthook             sys.path_hooks             sys.warnoptions

3 comments

Nilton Volpato 15 years, 5 months ago  # | flag

suggestion.

This is Nice! Except that it beeps, because you are really telling rlcompleter there is no more completions.

What about changing:
readline.insert_text('\t')
return None

to:
return ['   ',None][state]

Using spaces preferrably (or tabs) if you wish.

-- Nilton
Sunjoong LEE 15 years, 5 months ago  # | flag

Completer and history viewer support indent and file_match. I had extened your recipe. My recipe's url is http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496822 .

Jian Ding Chen (author) 15 years, 5 months ago  # | flag

Thanks the suggestions. I've edited my original recipe. :)