Python rlcompleter 模块,get_class_members() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用rlcompleter.get_class_members()

项目:PyMal    作者:cysinfo    | 项目源码 | 文件源码
def main():
    banner = "PyMal - Python Interactive Shell for Malware Analysis.\nUse Object \"pm\" to access the malware analysis related functions!\nAuthor: Amit Malik\nhttp://www.securityxploded.com\n"
    pm = PyMal()
    class pymalcomplete(rlcompleter.Completer):
        # Code segment from scapy - It is neat and mature
        def attr_matches(self, text):
            m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
            if not m:
                return
            expr, attr = m.group(1, 3)
            try:
                object = eval(expr)
            except:
                object = eval(expr, session)
            words = dir(object)
            if hasattr(pm,"__class__" ):
                words = words + rlcompleter.get_class_members(pm.__class__)
            matches = []
            n = len(attr)
            for word in words:
                if word[:n] == attr:
                    matches.append("%s.%s" % (expr, word))
            return matches
    readline.set_completer(pymalcomplete().complete)
    readline.parse_and_bind("C-o: operate-and-get-next")
    readline.parse_and_bind('tab: complete')
    code.interact(banner=banner,local = locals())
项目:python-yakumo    作者:yosshy    | 项目源码 | 文件源码
def attr_matches(self, text):
        """
        Derived from rlcompleter.Completer.attr_matches()
        """
        m = self.PATTERN.match(text)
        if not m:
            return []
        expr, attr = m.group(1, 3)
        try:
            thisobject = eval(expr, self.namespace)
        except Exception:
            return []

        # get the content of the object, except __builtins__
        words = dir(thisobject)
        if "__builtins__" in words:
            words.remove("__builtins__")

        if hasattr(thisobject, '__class__'):
            words.append('__class__')
            words.extend(rlcompleter.get_class_members(thisobject.__class__))
        matches = []
        n = len(attr)
        for word in words:
            if attr == '' and word[0] == '_':
                continue
            if word[:n] == attr and hasattr(thisobject, word):
                val = getattr(thisobject, word)
                word = self._callable_postfix(val, "%s.%s" % (expr, word))
                matches.append(word)
        return matches