00001
00002
00003 __file__ = "introspection.py"
00004 __title__ = "This is the introspection module, made to retrieve easily informations about python objects."
00005 __version__ = "0.2"
00006 __author__ = "Olivier Boudeville (olivier.boudeville@online.fr)"
00007 __project__ = "Ceylan"
00008 __creationDate__= "2001, August 23"
00009 __comments__ = "Use its own introspection capabilities to discover what this module can do !"
00010 __source__ = "Mark Pilgrim, http://diveintopython.org/, and al."
00011 __doc__ = __title__ + '\n' + __comments__
00012
00013
00014
00015
00016 import sys, string, inspect, toolbox
00017
00018
00019 def showDict(myDict, spacing=10, collapse=1):
00020 """Affiche un dictionnaire, en plaçant sur la même ligne une clé et l'objet qui lui correspond"""
00021
00022 tempList = []
00023
00024 for item in myDict.keys():
00025 tempList.append("%s %s" % (string.ljust(item,spacing), myDict[item]))
00026 print string.join(tempList, '\n')
00027
00028
00029 def showMethods(object, spacing=15, collapse=1):
00030 """Affiche les méthodes de l'objet spécifié et leur documentation __doc__"""
00031
00032
00033 methodList = []
00034 for method in dir(object):
00035 if callable(getattr(object, method)):
00036 methodList.append(method)
00037 processFunc = collapse and (lambda s: string.join(s.split(), ' ')) or (lambda s: s)
00038 tempList = []
00039 for method in methodList:
00040 tempList.append("%s %s" % (string.ljust(method,spacing),str(getattr(object, method).__doc__)))
00041 print string.join(tempList, '\n')
00042
00043
00044
00045 def showDataMembers(object, spacing=10, collapse=1):
00046 """Affiche les données membres de l'objet spécifié et leur type"""
00047
00048
00049 dataList = []
00050 for member in dir(object):
00051 if not callable(getattr(object, member)):
00052 dataList.append(member)
00053 processFunc = collapse and (lambda s: string.join(s.split(), ' ')) or (lambda s: s)
00054 tempList = []
00055 for member in dataList:
00056 tempList.append("%s %s %s" % (string.ljust(member,spacing), type(member), str(member)))
00057 if len(tempList)==0:
00058 print "No data attributes"
00059 else:
00060 print string.join(tempList, '\n')
00061
00062
00063 def showLoadedModules(spacing=10, collapse=1):
00064
00065 """Print methods and doc strings of object"""
00066
00067 tempList=[]
00068 for item in sys.modules.keys():
00069 tempList.append("%s %s" % (string.ljust(item,spacing), sys.modules[item]))
00070 print string.join(tempList, '\n')
00071
00072
00073 def showObjectSymbolTable(myObject):
00074
00075 """Display local object symbol table """
00076 showDict(vars(myObject))
00077
00078
00079 def showCurrentLocalSymbolTable(spacing=10, collapse=1):
00080
00081 """Show current local symbol table, with their type"""
00082 tempList=[]
00083
00084 for item in dir():
00085 tempList.append("%s %s" % (string.ljust(item,spacing), type(item)))
00086 print string.join(tempList, '\n')
00087
00088
00089 def inspectModule(myModule, spacing=10, collapse=1):
00090 """Affiche des informations sur un module"""
00091 print "Description du module : ", myModule.__doc__
00092 if myModule.__file__ is not None:
00093 print "Défini dans le fichier :", myModule.__file__
00094 tempList = []
00095 print "Membres du modules : "
00096 processFunc = collapse and (lambda s: string.join(s.split(), ' ')) or (lambda s: s)
00097 print "\n".join(["%s %s" % (string.ljust(item[0],spacing), processFunc(str(item[1]))) for item in inspect.getmembers(myModule)])
00098
00099 def inspectClass(myClass, spacing=10, collapse=1):
00100 """Affiche des informations sur une classe"""
00101 print "Description de la classe : ", myClass.__doc__
00102 print "Définie dans le module : ", myClass.__module__
00103
00104 def inspectMethod(myMethod, spacing=15, collapse=1):
00105 """Affiche des informations sur une méthode"""
00106 print "Nom : ", myMethod.__name__
00107 print "Description de la méthode : ", myMethod.__doc__
00108 print "Appartient à la classe : ", myMethod.im_class
00109 print "Objet-fonction contenant cette méthode : ", myMethod.im_func
00110 if myMethod.im_self is not None:
00111 print "Liée à l'instance : ", im_self
00112
00113 def inspectFunction(myFunc, spacing=10, collapse=1):
00114 """Affiche des informations sur une fonction"""
00115 print "Nom : ", myFunc.__name__
00116 print "Description de la fonction : ", myFunc.__doc__
00117 print "Arguments par défaut : ", myFunc.func_defaults
00118 print "Espace de nommage global dans lequel cette fonction est définie : ", myfunc.func_globals
00119
00120
00121
00122 if __name__ == "__main__":
00123 print __doc__