本文整理汇总了Python中inspect.getmodule方法的典型用法代码示例。如果您正苦于以下问题:Python inspect.getmodule方法的具体用法?Python inspect.getmodule怎么用?Python inspect.getmodule使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inspect
的用法示例。
在下文中一共展示了inspect.getmodule方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_method
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def handle_method(method, method_name, class_name):
method_errors = []
# Skip out-of-library inherited methods
module = inspect.getmodule(method)
if module is not None:
if not module.__name__.startswith('pylearn2'):
return method_errors
docstring = inspect.getdoc(method)
if docstring is None:
method_errors.append((class_name, method_name,
'**missing** method-level docstring'))
else:
method_errors = [
(class_name, method_name, e) for e in
NumpyFunctionDocString(docstring, method).get_errors()
]
return method_errors
示例2: handle_class
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def handle_class(val, class_name):
cls_errors = []
docstring = inspect.getdoc(val)
if docstring is None:
cls_errors.append((class_name,
'**missing** class-level docstring'))
else:
cls_errors = [
(e,) for e in
NumpyClassDocString(docstring, class_name, val).get_errors()
]
# Get public methods and parse their docstrings
methods = dict(((name, func) for name, func in inspect.getmembers(val)
if not name.startswith('_') and callable(func) and
type(func) is not type))
for m_name, method in six.iteritems(methods):
# skip error check if the method was inherited
# from a parent class (which means it wasn't
# defined in this source file)
if inspect.getmodule(method) is not None:
continue
cls_errors.extend(handle_method(method, m_name, class_name))
return cls_errors
示例3: validate_error_func
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def validate_error_func(self):
if self.error_func:
if isinstance(self.error_func,types.FunctionType):
ismethod = 0
elif isinstance(self.error_func, types.MethodType):
ismethod = 1
else:
self.log.error("'p_error' defined, but is not a function or method")
self.error = 1
return
eline = func_code(self.error_func).co_firstlineno
efile = func_code(self.error_func).co_filename
module = inspect.getmodule(self.error_func)
self.modules[module] = 1
argcount = func_code(self.error_func).co_argcount - ismethod
if argcount != 1:
self.log.error("%s:%d: p_error() requires 1 argument",efile,eline)
self.error = 1
# Get the tokens map
示例4: get_pfunctions
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def get_pfunctions(self):
p_functions = []
for name, item in self.pdict.items():
if not name.startswith('p_'): continue
if name == 'p_error': continue
if isinstance(item,(types.FunctionType,types.MethodType)):
line = func_code(item).co_firstlineno
module = inspect.getmodule(item)
p_functions.append((line,module,name,item.__doc__))
# Sort all of the actions by line number
p_functions.sort()
self.pfuncs = p_functions
# Validate all of the p_functions
示例5: get_extended_info
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def get_extended_info(error_message, prefix):
# noinspection PyProtectedMember
caller_stack_frame = sys._getframe(2)
caller = caller_stack_frame.f_code.co_name
line = caller_stack_frame.f_lineno
module = inspect.getmodule(caller_stack_frame)
error_code = get_error_constant_name(module.__dict__, error_message, prefix)
if error_code is None:
error_code = get_error_constant_name(caller_stack_frame.f_globals, error_message, prefix)
result = {
"Caller": caller,
"Module": module.__name__,
"Line": line
}
if error_code is not None:
result["Code"] = error_code
return result
示例6: tap
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def tap(fn):
"""Decorate a function/method to call any associated taps that have been registered in the ``TapManager``.
Functional behavior is not changed.
"""
fn_id = inspect.getmodule(fn).__name__ + "." + fn.__qualname__
@wraps(fn)
def tapped(*args, **kwargs):
tap = TapManager.get(fn_id)
if tap is not None:
tap.pre(fn_id, args, kwargs)
result = fn(*args, **kwargs)
if tap is not None:
tap.post(fn_id, args, kwargs, result)
return result
return tapped
示例7: render_doc
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
renderer=None):
"""Render text documentation, given an object or a path to an object."""
if renderer is None:
renderer = text
object, name = resolve(thing, forceload)
desc = describe(object)
module = inspect.getmodule(object)
if name and '.' in name:
desc += ' in ' + name[:name.rfind('.')]
elif module and module is not object:
desc += ' in module ' + module.__name__
if not (inspect.ismodule(object) or
inspect.isclass(object) or
inspect.isroutine(object) or
inspect.isgetsetdescriptor(object) or
inspect.ismemberdescriptor(object) or
isinstance(object, property)):
# If the passed object is a piece of data or an instance,
# document its available methods instead of its value.
object = type(object)
desc += ' object'
return title % desc + '\n\n' + renderer.document(object, name)
示例8: nice_classname
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def nice_classname(obj):
"""Returns a nice name for class object or class instance.
>>> nice_classname(Exception()) # doctest: +ELLIPSIS
'...Exception'
>>> nice_classname(Exception) # doctest: +ELLIPSIS
'...Exception'
"""
if inspect.isclass(obj):
cls_name = obj.__name__
else:
cls_name = obj.__class__.__name__
mod = inspect.getmodule(obj)
if mod:
name = mod.__name__
# jython
if name.startswith('org.python.core.'):
name = name[len('org.python.core.'):]
return "%s.%s" % (name, cls_name)
else:
return cls_name
示例9: validate_error_func
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def validate_error_func(self):
if self.error_func:
if isinstance(self.error_func, types.FunctionType):
ismethod = 0
elif isinstance(self.error_func, types.MethodType):
ismethod = 1
else:
self.log.error("'p_error' defined, but is not a function or method")
self.error = True
return
eline = self.error_func.__code__.co_firstlineno
efile = self.error_func.__code__.co_filename
module = inspect.getmodule(self.error_func)
self.modules.add(module)
argcount = self.error_func.__code__.co_argcount - ismethod
if argcount != 1:
self.log.error('%s:%d: p_error() requires 1 argument', efile, eline)
self.error = True
# Get the tokens map
示例10: get_pfunctions
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def get_pfunctions(self):
p_functions = []
for name, item in self.pdict.items():
if not name.startswith('p_') or name == 'p_error':
continue
if isinstance(item, (types.FunctionType, types.MethodType)):
line = getattr(item, 'co_firstlineno', item.__code__.co_firstlineno)
module = inspect.getmodule(item)
p_functions.append((line, module, name, item.__doc__))
# Sort all of the actions by line number; make sure to stringify
# modules to make them sortable, since `line` may not uniquely sort all
# p functions
p_functions.sort(key=lambda p_function: (
p_function[0],
str(p_function[1]),
p_function[2],
p_function[3]))
self.pfuncs = p_functions
# Validate all of the p_functions
示例11: get_module_things
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def get_module_things(module):
module_name = module.__name__
for func_name, func in get_module_functions(module):
if inspect.getmodule(func) != module:
continue
yield (module_name + "." + func_name, func)
for class_name, klass in get_module_classes(module):
if inspect.getmodule(klass) != module:
continue
yield (module_name + "." + class_name, klass)
for method_name, method in get_class_methods(klass):
if method_name not in klass.__dict__:
continue
yield (module_name + "." + class_name + ":" + method_name, method)
示例12: _from_module
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def _from_module(self, module, object):
"""
Return true if the given object is defined in the given
module.
"""
if module is None:
return True
elif inspect.isfunction(object):
return module.__dict__ is object.func_globals
elif inspect.isclass(object):
return module.__name__ == object.__module__
elif inspect.getmodule(object) is not None:
return module is inspect.getmodule(object)
elif hasattr(object, '__module__'):
return module.__name__ == object.__module__
elif isinstance(object, property):
return True # [XX] no way not be sure.
else:
raise ValueError("object must be a class or function")
示例13: inject_dtype
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def inject_dtype(dtype):
def inner(func):
@wraps(func)
def call(*tensors, **kw):
kw['dtype'] = np.dtype(dtype)
ret = func(*tensors, **kw)
if ret is NotImplemented:
reverse_func = getattr(inspect.getmodule(func), 'r{0}'.format(func.__name__), None)
if reverse_func is not None:
ret = reverse_func(*tensors[::-1], **kw)
if ret is NotImplemented:
raise TypeError(
"unsupported operand type(s) for {0}: '{1}' and '{2}".format(
func.__name__, *[type(t) for t in tensors]))
return ret
return call
return inner
示例14: get_calling_module_name
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def get_calling_module_name(depth=3):
"""
Get the name of the 'calling' module
Args:
depth (int): How many levels to look 'up' from the current module. Remember that the enclosing 'helpers' \
module is counted as 1.
Returns:
str: The calling module name
"""
assert isinstance(depth, int), u'depth should be an int'
frame = inspect.stack()[depth]
LOG.debug(u'Got calling frame %r', frame)
module = inspect.getmodule(frame[0])
if module:
return module.__name__
示例15: render_doc
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import getmodule [as 别名]
def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
"""Render text documentation, given an object or a path to an object."""
object, name = resolve(thing, forceload)
desc = describe(object)
module = inspect.getmodule(object)
if name and '.' in name:
desc += ' in ' + name[:name.rfind('.')]
elif module and module is not object:
desc += ' in module ' + module.__name__
if type(object) is _OLD_INSTANCE_TYPE:
# If the passed object is an instance of an old-style class,
# document its available methods instead of its value.
object = object.__class__
elif not (inspect.ismodule(object) or
inspect.isclass(object) or
inspect.isroutine(object) or
inspect.isgetsetdescriptor(object) or
inspect.ismemberdescriptor(object) or
isinstance(object, property)):
# If the passed object is a piece of data or an instance,
# document its available methods instead of its value.
object = type(object)
desc += ' object'
return title % desc + '\n\n' + text.document(object, name)