Skip to content

Instantly share code, notes, and snippets.

@jairajsahgal
Created November 9, 2021 17:59
Show Gist options
  • Save jairajsahgal/161fcb411544c7a407678f26ff8c1313 to your computer and use it in GitHub Desktop.
Save jairajsahgal/161fcb411544c7a407678f26ff8c1313 to your computer and use it in GitHub Desktop.
This python program gives you auto complete suggestions of words just like command line.
import readline
import requests
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
text = text.lower()
if state == 0: # on first trigger, build possible matches
if text: # cache matches (entries that start with entered text)
self.matches = [s for s in self.options
if s and s.startswith(text)]
else: # no text entered, all matches possible
self.matches = self.options[:]
# return match indexed by state
try:
return self.matches[state]
except IndexError:
return None
url = 'https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt'
page = requests.get(url)
l=page.text.split()
print(len(l))
completer = MyCompleter(l)
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
input = input("Input: ")
print("You entered", input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment