亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 一個(gè)得到類命名的方法 [打印本頁]

作者: jeppeter    時(shí)間: 2013-04-26 13:31
標(biāo)題: 一個(gè)得到類命名的方法
想得到一個(gè)類全名,但找了很多的方法,都沒有辦法直接得到,如
  1. class AUnit(object):
  2.         def GetMsg(self):
  3.                 return clsname.GetCallerClassName(1)
  4.         class AUnit2(object):
  5.                 def GetMsg(self):
  6.                         return clsname.GetCallerClassName(1)
  7.                 class AUnit3(object):
  8.                         def GetMsg(self):
  9.                                 return clsname.GetCallerClassName(1)
復(fù)制代碼
在AUnit.GetMsg()函數(shù)調(diào)用,得到這個(gè)函數(shù)的調(diào)用類的時(shí)候,要返回的是
  1. __main__.AUnit
復(fù)制代碼
當(dāng)然這里假設(shè)是在__main__的模塊內(nèi)調(diào)用

如果是AUnit.AUnit2.GetMsg()調(diào)用,得到的是
  1. __main__.AUnit.AUnit2
復(fù)制代碼
依此類推。

只好自己動(dòng)手寫一個(gè)
  1. #! python
  2. import inspect
  3. import types
  4. import logging
  5. def FuncMethodSearh(obj):
  6.         if inspect.ismethod(obj):
  7.                 return True
  8.         if inspect.isfunction(obj):
  9.                 return True
  10.         if inspect.isclass(obj):
  11.                 return True
  12.         return False


  13. def __IsCodeInClass(clsobj,codeobj):
  14.         mns = inspect.getmembers(clsobj,predicate=FuncMethodSearh)
  15.         ret = 0
  16.         for m in mns:
  17.                 #logging.info('%s %s'%(repr(m[1]),repr(codeobj)))
  18.                 if type(m[1]) is types.MethodType or type(m[1]) is types.FunctionType or\
  19.                   type(m[1]) is types.GeneratorType:
  20.                         fobj = m[1].func_code
  21.                         if fobj.co_name == codeobj.co_name  and \
  22.                         fobj.co_firstlineno == codeobj.co_firstlineno:
  23.                                 ret = 1
  24.                                 break
  25.         return ret

  26. def __GetCodeObjClass(clsobj,codeobj,calllevel=0):
  27.         if calllevel > 300:
  28.                 raise
  29.         ret = __IsCodeInClass(clsobj,codeobj)
  30.         if ret > 0:
  31.                 return clsobj.__name__
  32.         mns = inspect.getmembers(clsobj,predicate=inspect.isclass)
  33.         for m in mns:
  34.                 # we do not recursive for the two  types
  35.                 if m[0] != '__class__' and m[0] != '__base__':
  36.                         #logging.info('%s %s'%(repr(m[1]),repr(codeobj)))
  37.                         n = __GetCodeObjClass(m[1],codeobj,calllevel + 1)
  38.                         if n:
  39.                                 return clsobj.__name__+'.'+n
  40.         return None

  41. def GetCallerClassFullName(modobj,codeobj):
  42.         '''
  43.                 @modobj : module object area to search for code object
  44.                 @codeobj : code object will find its class belong to

  45.                 @return : it codeobj can not detect the name return module name
  46.                               if codeobj is the function not belong to any of the class
  47.                               return the module name
  48.                               if codeobj is the method or function of class ,return whole class name including module name
  49.         '''
  50.         mns = inspect.getmembers(modobj,predicate=FuncMethodSearh)
  51.         ret = 0
  52.         for m in mns:
  53.                 if type(m[1]) is types.MethodType or type(m[1]) is types.FunctionType or\
  54.                   type(m[1]) is types.GeneratorType:
  55.                         fobj = m[1].func_code
  56.                         if fobj.co_name == codeobj.co_name  and \
  57.                         fobj.co_firstlineno == codeobj.co_firstlineno:
  58.                                 ret = 1
  59.                                 break
  60.         if ret > 0:
  61.                 return modobj.__name__

  62.         mns = inspect.getmembers(modobj,predicate=inspect.isclass)
  63.         for m in mns:
  64.                 n = __GetCodeObjClass(m[1],codeobj)
  65.                 if n:
  66.                         return modobj.__name__ +'.'+n
  67.         return modobj.__name__
  68.        
  69. def GetCallerClassName(level=2):
  70.         mn = ''
  71.         stks = inspect.stack()
  72.         if len(stks) > level:
  73.                 frm = stks[level]
  74.                 mm = inspect.getmodule(frm[0])
  75.                 fc = frm[0].f_code
  76.                 mn = GetCallerClassFullName(mm,fc)
  77.         return mn
復(fù)制代碼
大家可以測(cè)試。




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2