Python, through it's readline bindings allows for great command-line autocompletion (as described in here).
But, the completion only seems to work at the beginning of strings. If you want to match the middle or end of a string readline doesn't work.
I would like to autocomplete strings, in a command-line python program by matching what I type with any of the strings in a list of available strings.
Using terminal emulators like curses would be fine. It only has to run on linux, not Mac or Windows.
Here is an example: Say I have the following three strings in a list
['Paul Eden <[email protected]>',
'Eden Jones <[email protected]>',
'Somebody Else <[email protected]>']
I would like some code that will autocomplete the first two items in the list after I type 'Eden' and then allow me to pick one of them (all through the command-line using the keyboard).
(In Python Shell window, you can use TAB key besides the key combination of 'CTRL' and 'space' to invoke the built-in auto-completion feature.) Alternatively, you can choose the "Show Completions" in the main Edit menu to achieve the same as well.
If so, you'll need to use the input() command. The input() command allows you to require a user to enter a string or number while a program is running. The input() method replaced the old raw_input() method that existed in Python v2. Open a terminal and run the python command to access Python.
Python IDLE has basic code completion functionality. It can only autocomplete the names of functions and classes. To use autocompletion in the editor, just press the tab key after a sequence of text. The call tip will display as a popup note, reminding you how to append to a list.
To access command-line arguments from within a Python program, first import the sys package. You can then refer to the full set of command-line arguments, including the function name itself, by referring to a list named argv. In either case, argv refers to a list of command-line arguments, all stored as strings.
I'm not sure I understand the problem. You could use readline.clear_history and readline.add_history to set up the completable strings you want, then control-r to search backword in the history (just as if you were at a shell prompt). For example:
#!/usr/bin/env python
import readline
readline.clear_history()
readline.add_history('foo')
readline.add_history('bar')
while 1:
print raw_input('> ')
Alternatively, you could write your own completer version and bind the appropriate key to it. This version uses caching in case your match list is huge:
#!/usr/bin/env python
import readline
values = ['Paul Eden <[email protected]>',
'Eden Jones <[email protected]>',
'Somebody Else <[email protected]>']
completions = {}
def completer(text, state):
try:
matches = completions[text]
except KeyError:
matches = [value for value in values
if text.upper() in value.upper()]
completions[text] = matches
try:
return matches[state]
except IndexError:
return None
readline.set_completer(completer)
readline.parse_and_bind('tab: menu-complete')
while 1:
a = raw_input('> ')
print 'said:', a
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