why this error?

python.jiang python.jiang at 163.com
Mon Dec 17 00:45:34 EST 2007


thanks all first. but i had one class bellow to get object info what user had inputed when run application. because the problem that i had showed yestoday, i must write the code so hard to understand.
can any friend tell me one way to solve this problem?
thanks!!
 
list:

import inspect
 
vtStd,vtIron=range(2)
versionType=vtStd

moduleName=None
objectName=None
 
def _functionFilter(name):  #because the problem, i must write this function
 exec "import %s"%(moduleName,)
 a=getattr(eval(objectName),name)
 return callable(a)
 
def _classFilter(name):  #because the problem, i must write this function
 exec "import %s"%(moduleName,)
 a=getattr(eval(objectName),name)
 return inspect.isclass(a)
 
def _varFilter(name):  #because the problem, i must write this function
 exec "import %s"%(moduleName,)
 a=getattr(eval(objectName),name)
 return not(callable(a) or inspect.isclass(a))
 
class moduleInfo(object):
 def __init__(self):
  self.dirs=None
  self.moduleName=None
  self.objectName=None
  self.classes=None
  self.functions=None
  self.variants=None
  self.objectType=None
 
 def readContent(self,mname,name):
  global moduleName,objectName
  exec "import %s"%(mname)  #because this line, can't write code like "listA not in listB"
  self.dirs=dir(eval(name))
  self.objectType=type(eval(name))
  self.dirs.sort()
  self.dirs=tuple(self.dirs)
  self.moduleName=moduleName=mname
  self.objectName=objectName=name

  self.classes=filter(_classFilter,self.dirs)
  self.functions=filter(_functionFilter,self.dirs)
  self.variants=filter(_varFilter,self.dirs)
#because the problem, i can't write code like bellow:
#  self.variants=filter(lambda a: (a not in self.classes) and (a not in self.functions),self.dirs)

  return self.dirs
 
 def readHelp(self,mname,objName,memName):
  global moduleName,objectName
  exec "import %s"%(mname)
  s="%s.%s"%(objName,memName)
  shelp=str(eval("type(%s)\n"%(s,)))
  shelp+=str(eval("\n%s.__doc__"%(s,)))
  return shelp
 
if __name__=="__main__":
 mi=moduleInfo()
 a=mi.readContent("sys","sys")
 print "dir:",a
 print "class:",mi.classes
 print "fun:",mi.functions
 print "var:",mi.variants
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071217/a3e26d0e/attachment.html>


More information about the Python-list mailing list