Using the snippet below, I have added tab completion to the python interpreter.
import readline
import rlcompleter
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
However, I have encountered a weird behaviour where after hitting TAB, the interpreter would give duplicates as such:
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d=dict()
>>> d.
Display all 108 possibilities? (y or n)
d.__class__( d.__class__( d.__class__(
d.__class__( d.__cmp__( d.__cmp__(
d.__contains__( d.__contains__( d.__delattr__(
d.__delattr__( d.__delattr__( d.__delitem__(
d.__delitem__( d.__doc__ d.__doc__
d.__doc__ d.__eq__( d.__eq__(
d.__format__( d.__format__( d.__format__(
d.__ge__( d.__ge__( d.__getattribute__(
d.__getattribute__( d.__getattribute__( d.__getitem__(
d.__getitem__( d.__gt__( d.__gt__(
d.__hash__ d.__hash__ d.__hash__
d.__init__( d.__init__( d.__init__(
d.__iter__( d.__iter__( d.__le__(
d.__le__( d.__len__( d.__len__(
d.__lt__( d.__lt__( d.__ne__(
d.__ne__( d.__new__( d.__new__(
d.__new__( d.__reduce__( d.__reduce__(
d.__reduce__( d.__reduce_ex__( d.__reduce_ex__(
d.__reduce_ex__( d.__repr__( d.__repr__(
d.__repr__( d.__setattr__( d.__setattr__(
d.__setattr__( d.__setitem__( d.__setitem__(
d.__sizeof__( d.__sizeof__( d.__sizeof__(
d.__str__( d.__str__( d.__str__(
d.__subclasshook__( d.__subclasshook__( d.__subclasshook__(
d.clear( d.clear( d.copy(
d.copy( d.fromkeys( d.fromkeys(
d.get( d.get( d.has_key(
d.has_key( d.items( d.items(
d.iteritems( d.iteritems( d.iterkeys(
d.iterkeys( d.itervalues( d.itervalues(
d.keys( d.keys( d.pop(
d.pop( d.popitem( d.popitem(
d.setdefault( d.setdefault( d.update(
d.update( d.values( d.values(
d.viewitems( d.viewitems( d.viewkeys(
d.viewkeys( d.viewvalues( d.viewvalues(
108 possibilities! min 2, max 4 duplicates.
Furthermore,
dict
the total number of possibilities increases to 160 (and then to 209, 258).Any pointers or debugging tips are appreciated.
I have solved this issue by adding a custom completer. It now works as expected.
import readline
import rlcompleter
c = rlcompleter.Completer()
def complete(text, state):
buffer = readline.get_line_buffer()
first = c.complete(buffer, 0)
if state == 0:
return first
current = c.complete(buffer, state)
if current != first:
return current
readline.parse_and_bind('bind ^I rl_complete')
readline.set_completer(complete)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With