Warning: this post is an old one please see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496812 and someone delete this recipe if possible.
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!
Note: rlcompleter module only avail in unix platform.
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 == "":
readline.insert_text('\t')
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().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
seems I accident send twice? please delete this thread if possible. thx! :)