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

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2010 | 回復(fù): 0
打印 上一主題 下一主題

一個得到類命名的方法 [復(fù)制鏈接]

論壇徽章:
1
15-16賽季CBA聯(lián)賽之新疆
日期:2017-03-09 12:33:45
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-04-26 13:31 |只看該作者 |倒序瀏覽
想得到一個類全名,但找了很多的方法,都沒有辦法直接得到,如
  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)用,得到這個函數(shù)的調(diào)用類的時候,要返回的是
  1. __main__.AUnit
復(fù)制代碼
當然這里假設(shè)是在__main__的模塊內(nèi)調(diào)用

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

只好自己動手寫一個
  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ù)制代碼
大家可以測試。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP