<s> """<STR_LIT>""" <EOL> import __builtin__ <EOL> __all__ = [ "<STR_LIT>" ] <EOL> class Completer : <EOL> def __init__ ( self , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> if namespace and not isinstance ( namespace , dict ) : <EOL> raise TypeError , '<STR_LIT>' <EOL> if namespace is None : <EOL> self . use_main_ns = <NUM_LIT:1> <EOL> else : <EOL> self . use_main_ns = <NUM_LIT:0> <EOL> self . namespace = namespace <EOL> def complete ( self , text , state ) : <EOL> """<STR_LIT>""" <EOL> if self . use_main_ns : <EOL> self . namespace = __main__ . __dict__ <EOL> if state == <NUM_LIT:0> : <EOL> if "<STR_LIT:.>" in text : <EOL> self . matches = self . attr_matches ( text ) <EOL> else : <EOL> self . matches = self . global_matches ( text ) <EOL> try : <EOL> return self . matches [ state ] <EOL> except IndexError : <EOL> return None <EOL> def _callable_postfix ( self , val , word ) : <EOL> if hasattr ( val , '<STR_LIT>' ) : <EOL> word = word + "<STR_LIT:(>" <EOL> return word <EOL> def global_matches ( self , text ) : <EOL> """<STR_LIT>""" <EOL> import keyword <EOL> matches = [ ] <EOL> n = len ( text ) <EOL> for word in keyword . kwlist : <EOL> if word [ : n ] == text : <EOL> matches . append ( word ) <EOL> for nspace in [ __builtin__ . __dict__ , self . namespace ] : <EOL> for word , val in nspace . items ( ) : <EOL> if word [ : n ] == text and word != "<STR_LIT>" : <EOL> matches . append ( self . _callable_postfix ( val , word ) ) <EOL> return matches <EOL> def attr_matches ( self , text ) : <EOL> """<STR_LIT>""" <EOL> import re <EOL> m = re . match ( r"<STR_LIT>" , text ) <EOL> if not m : <EOL> return [ ] <EOL> expr , attr = m . group ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> try : <EOL> thisobject = eval ( expr , self . namespace ) <EOL> except Exception : <EOL> return [ ] <EOL> words = dir ( thisobject ) <EOL> if "<STR_LIT>" in words : <EOL> words . remove ( "<STR_LIT>" ) <EOL> if hasattr ( thisobject , '<STR_LIT>' ) : <EOL> words . append ( '<STR_LIT>' ) <EOL> words . extend ( get_class_members ( thisobject . __class__ ) ) <EOL> matches = [ ] <EOL> n = len ( attr ) <EOL> for word in words : <EOL> if word [ : n ] == attr and hasattr ( thisobject , word ) : <EOL> val = getattr ( thisobject , word ) <EOL> word = self . _callable_postfix ( val , "<STR_LIT>" % ( expr , word ) ) <EOL> matches . append ( word ) <EOL> return matches <EOL> def get_class_members ( klass ) : <EOL> ret = dir ( klass ) <EOL> if hasattr ( klass , '<STR_LIT>' ) : <EOL> for base in klass . __bases__ : <EOL> ret = ret + get_class_members ( base ) <EOL> return ret <EOL> try : <EOL> import readline <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> readline . set_completer ( Completer ( ) . complete ) </s>
