免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1957 | 回复: 0
打印 上一主题 下一主题

一个得到类命名的方法 [复制链接]

论坛徽章:
1
15-16赛季CBA联赛之新疆
日期:2017-03-09 12:33:45
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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)
复制代码
在AUnit.GetMsg()函数调用,得到这个函数的调用类的时候,要返回的是
  1. __main__.AUnit
复制代码
当然这里假设是在__main__的模块内调用

如果是AUnit.AUnit2.GetMsg()调用,得到的是
  1. __main__.AUnit.AUnit2
复制代码
依此类推。

只好自己动手写一个
  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
复制代码
大家可以测试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP