本文整理汇总了Python中code.co_name方法的典型用法代码示例。如果您正苦于以下问题:Python code.co_name方法的具体用法?Python code.co_name怎么用?Python code.co_name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类code
的用法示例。
在下文中一共展示了code.co_name方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_generic_cmd
# 需要导入模块: import code [as 别名]
# 或者: from code import co_name [as 别名]
def get_generic_cmd(obj, ui, cliclass=GenericCLI, aliases=None, gbl=None):
"""get a GenericCLI (or other) command set wrapping any class instance
object. The wrapped objects public methods have CLI command counterparts
automatically created."""
import new
from methodholder import MethodHolder
cmd = cliclass(ui, aliases)
if gbl is None:
gbl = globals()
hashfilter = {}
for name in _get_methodnames(obj):
if hasattr(cmd, name):
continue # don't override already defined methods
# all this mess does is introspect the object methods and map it to a CLI
# object method of the same name, with a docstring showing the attributes
# and their default values, and the actual code mirroring the
# _generic_call method in the GenericCLI class.
else:
obj_meth = getattr(obj, name)
if id(obj_meth.im_func) in hashfilter: # filter out aliases
continue
else:
hashfilter[id(obj_meth.im_func)] = True
mh = MethodHolder(obj_meth)
doc = "%s *\n%s" % (mh, obj_meth.__doc__ or "")
code = cliclass._generic_call.func_code
nc = new.code(code.co_argcount, code.co_nlocals, code.co_stacksize,
code.co_flags, code.co_code,
(doc,)+code.co_consts[1:], # replace docstring
code.co_names, code.co_varnames, code.co_filename,
code.co_name, code.co_firstlineno, code.co_lnotab)
f = new.function(nc, gbl, name)
m = new.instancemethod(f, cmd, cliclass)
setattr(cmd, name, m)
cmd._setup(obj, "Object:%s> " % (obj.__class__.__name__,))
return cmd
示例2: get_generic_cmd
# 需要导入模块: import code [as 别名]
# 或者: from code import co_name [as 别名]
def get_generic_cmd(obj, ui, cliclass=GenericCLI, aliases=None, gbl=None):
"""get a GenericCLI (or other) command set wrapping any class instance
object. The wrapped objects public methods have CLI command counterparts
automatically created."""
import new
from pycopia.methodholder import MethodHolder
cmd = cliclass(ui, aliases)
if gbl is None:
gbl = globals()
hashfilter = {}
for name in _get_methodnames(obj):
if hasattr(cmd, name):
continue # don't override already defined methods
# all this mess does is introspect the object methods and map it to a CLI
# object method of the same name, with a docstring showing the attributes
# and their default values, and the actual code mirroring the
# _generic_call method in the GenericCLI class.
else:
obj_meth = getattr(obj, name)
if id(obj_meth.__func__) in hashfilter: # filter out aliases
continue
else:
hashfilter[id(obj_meth.__func__)] = True
mh = MethodHolder(obj_meth)
doc = "%s *\n%s" % (mh, obj_meth.__doc__ or "")
code = cliclass._generic_call.func_code
nc = new.code(code.co_argcount, code.co_nlocals, code.co_stacksize,
code.co_flags, code.co_code,
(doc,)+code.co_consts[1:], # replace docstring
code.co_names, code.co_varnames, code.co_filename,
code.co_name, code.co_firstlineno, code.co_lnotab)
f = new.function(nc, gbl, name)
m = new.instancemethod(f, cmd, cliclass)
setattr(cmd, name, m)
cmd._setup(obj, "Object:%s> " % (obj.__class__.__name__,))
return cmd
示例3: do_whatis
# 需要导入模块: import code [as 别名]
# 或者: from code import co_name [as 别名]
def do_whatis(self, arg):
"""whatis arg
Print the type of the argument.
"""
try:
value = self._getval(arg)
except:
# _getval() already printed the error
return
code = None
# Is it a function?
try:
code = value.__code__
except Exception:
pass
if code:
self.message('Function %s' % code.co_name)
return
# Is it an instance method?
try:
code = value.__func__.__code__
except Exception:
pass
if code:
self.message('Method %s' % code.co_name)
return
# Is it a class?
if value.__class__ is type:
self.message('Class %s.%s' % (value.__module__, value.__qualname__))
return
# None of the above...
self.message(type(value))
示例4: do_whatis
# 需要导入模块: import code [as 别名]
# 或者: from code import co_name [as 别名]
def do_whatis(self, arg):
"""whatis arg
Print the type of the argument.
"""
try:
value = self._getval(arg)
except:
# _getval() already printed the error
return
code = None
# Is it a function?
try:
code = value.__code__
except Exception:
pass
if code:
self.message('Function %s' % code.co_name)
return
# Is it an instance method?
try:
code = value.__func__.__code__
except Exception:
pass
if code:
self.message('Method %s' % code.co_name)
return
# Is it a class?
if value.__class__ is type:
self.message('Class %s.%s' % (value.__module__, value.__name__))
return
# None of the above...
self.message(type(value))