Home > database >  Is it possible to autocomplete/suggest argument values from a given list in a python method?
Is it possible to autocomplete/suggest argument values from a given list in a python method?

Time:12-15

I'm trying to make a certain method of a class more user-friendly. Since I assume that the user doesn't know what exact values he can send to the method, I'd like to autocomplete the argument value in the function call command line.

Lets say the class is:

class Friends:
    def __init__(self):
        self._friends_last_names = {
            "Rachel": "Green",
            "Ross": "Geller",
            "Chandler": "Bing"
        }

    def get_friend_last_name(self, first_name):
        last_name = self._friends_last_names.get(first_name, None)
        if last_name is None:
            print(f"Error, {first_name} not found")
            return
        return last_name

and the user initiated an obejct:

friends_obj = Friends()

According to the assumption, the user doesn't remember how to spell any of the friends' first names.

I'd like to add the option of auto-complete so when the user will call:

>>> friends_obj.get_friend_last_name(first_name="Ch ...

and then hit the TAB button, the first_name argument will auto complete to "Chandler".

another use case is:

>>> friends_obj.get_friend_last_name(first_name="R ...

and hit TAB button, and python will show the two possible friends to choose: "Ross" or "Rachel" .

I've tried to overload the class' method getitem, but since there are more methods other then that one, it is very confusing for the user to know which method will be called upon brackets "[]".

CodePudding user response:

Autocompleting is not a Python(language) feature, it's an editor feature.

If there is only limited list of names which will not change, you can use enumerator:

from enum import Enum

class FirstName(Enum):
    Ross = "Ross"  # Or any other ID, may be integer
    Rachel = "Rachel"

Then you can modify your code like that:

class Friends:
    def __init__(self):
        self._friends_last_names = {
            "Rachel": "Green",
            "Ross": "Geller",
            "Chandler": "Bing"
        }

    def get_friend_last_name(self, first_name):
        last_name = self._friends_last_names.get(first_name.value, None)
        if last_name is None:
            print(f"Error, {first_name} not found")
            return
        return last_name

And then use it like that:

friends_obj.get_friend_last_name(first_name=FirstName.Ross)

Autocomplete should work in this case. If it's console application Muhammed Rayan's answer is better.

CodePudding user response:

This can be done generally using the following method. Here possibilities is the list of all names, and entry is the part completed by the user. autofill is the output list

possibilities = ["Ross", "Robert", "John", "Davis"]
entry = "Ro"

autofill = [name for name in possibilities if name.startswith(entry)]
print(autofill) # ['Ross', 'Robert']
  • Related