Hone logo
Hone
Problems

Python Code Completion Suggestion Engine

Code completion is a fundamental feature in modern Integrated Development Environments (IDEs) and text editors, significantly boosting developer productivity. This challenge asks you to implement a simplified code completion suggestion engine for Python, focusing on suggesting valid attributes and methods for a given object based on its type. This is a core component of intelligent code assistance.

Problem Description

You are tasked with creating a function get_suggestions(obj, prefix) that takes a Python object obj and a string prefix as input. The function should return a list of strings representing valid attributes and methods of the object that start with the given prefix. The suggestions should be sorted alphabetically. The function should handle various object types, including built-in types like lists, dictionaries, and strings, as well as custom objects.

Key Requirements:

  • Attribute and Method Discovery: The function must be able to identify both attributes and methods of the given object.
  • Prefix Matching: Only suggestions that start with the provided prefix should be included in the result.
  • Alphabetical Sorting: The returned list of suggestions must be sorted alphabetically.
  • String Conversion: All suggestions should be strings.
  • Error Handling: The function should gracefully handle cases where the object has no attributes or methods matching the prefix. In such cases, it should return an empty list.

Expected Behavior:

The function should leverage Python's introspection capabilities (e.g., dir()) to discover attributes and methods. It should filter these based on the provided prefix and return a sorted list of strings.

Edge Cases to Consider:

  • Objects with no attributes or methods.
  • Empty prefix.
  • Prefix that matches multiple attributes/methods.
  • Objects with attributes/methods that are not strings (e.g., functions). These should be converted to strings.
  • Built-in attributes/methods that are not relevant for code completion (e.g., __class__). While filtering all irrelevant attributes is beyond the scope of this challenge, prioritize common and useful suggestions.

Examples

Example 1:

Input: obj = [1, 2, 3], prefix = "a"
Output: ['append', 'appendleft']
Explanation: The list object has methods 'append', 'appendleft', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'. Only 'append' and 'appendleft' start with "a".

Example 2:

Input: obj = {"name": "Alice", "age": 30}, prefix = "n"
Output: ['name']
Explanation: The dictionary object has attributes 'name', 'age' and methods 'clear', 'copy', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'. Only 'name' starts with "n".

Example 3:

Input: obj = "hello", prefix = "u"
Output: ['upper']
Explanation: The string object has methods 'capitalize', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'islower', 'isprintable', 'isspace', 'istitle', 'join', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper'. Only 'upper' starts with "u".

Example 4:

Input: obj = 123, prefix = "t"
Output: []
Explanation: Integer objects have limited attributes and methods. None start with "t".

Constraints

  • The input obj can be any Python object.
  • The input prefix is a string.
  • The function must return a list of strings.
  • The time complexity should be reasonable for typical object sizes (e.g., no need to optimize for extremely large objects with thousands of attributes/methods). O(n log n) where n is the number of matching attributes/methods is acceptable due to the sorting requirement.
  • The function should not raise any exceptions.

Notes

  • Use the dir() function to get a list of attributes and methods.
  • Remember to filter the results based on the prefix.
  • Sort the suggestions alphabetically.
  • Consider using list comprehensions for concise code.
  • Focus on providing a functional solution that correctly identifies and filters attributes and methods. Extensive filtering of irrelevant attributes is not required for this challenge.
Loading editor...
python