def get_object_completions(instance, prefix, types=None, instance_types=None):
word_toks = []<EOL>toks = prefix.split('<STR_LIT:.>')<EOL>while len(toks) > <NUM_LIT:1>:<EOL><INDENT>attr = toks[<NUM_LIT:0>]<EOL>toks = toks[<NUM_LIT:1>:]<EOL>word_toks.append(attr)<EOL>try:<EOL><INDENT>instance = getattr(instance, attr)<EOL><DEDENT>except AttributeError:<EOL><INDENT>return []<EOL><DEDENT>if instance_types and not isinstance(instance, instance_types):<EOL><INDENT>return []<EOL><DEDENT><DEDENT>prefix = toks[-<NUM_LIT:1>]<EOL>words = []<EOL>attrs = dir(instance)<EOL>try:<EOL><INDENT>for attr in instance:<EOL><INDENT>if isinstance(attr, basestring):<EOL><INDENT>attrs.append(attr)<EOL><DEDENT><DEDENT><DEDENT>except TypeError:<EOL><INDENT>pass<EOL><DEDENT>for attr in attrs:<EOL><INDENT>if attr.startswith(prefix) and not attr.startswith('<STR_LIT:_>')and not hasattr(instance.__class__, attr):<EOL><INDENT>value = getattr(instance, attr)<EOL>if types and not isinstance(value, types):<EOL><INDENT>continue<EOL><DEDENT>if not callable(value):<EOL><INDENT>words.append(attr)<EOL><DEDENT><DEDENT><DEDENT>qual_words = ['<STR_LIT:.>'.join(word_toks + [x]) for x in words]<EOL>if len(words) == <NUM_LIT:1> and value is not None and(instance_types is None or isinstance(value, instance_types)):<EOL><INDENT>qual_word = qual_words[<NUM_LIT:0>]<EOL>words = get_object_completions(value, '<STR_LIT>', types)<EOL>for word in words:<EOL><INDENT>qual_words.append("<STR_LIT>" % (qual_word, word))<EOL><DEDENT><DEDENT>return qual_words<EOL>
Get completion strings based on an object's attributes/keys.

    Completion also works on dynamic attributes (eg implemented via __getattr__)
    if they are iterable.

    Args:
        instance (object): Object to introspect.
        prefix (str): Prefix to match, can be dot-separated to access nested
            attributes.
        types (tuple): Attribute types to match, any if None.
        instance_types (tuple): Class types to recurse into when a dotted
            prefix is given, any if None.

    Returns:
        List of strings.