本文整理汇总了Python中inspect.py方法的典型用法代码示例。如果您正苦于以下问题:Python inspect.py方法的具体用法?Python inspect.py怎么用?Python inspect.py使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inspect
的用法示例。
在下文中一共展示了inspect.py方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_log_level
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import py [as 别名]
def set_log_level(level):
"""Sets the log level that should be used by all AgentLogger instances.
This method is thread-safe.
@param level: The level, in the logging units by the logging package, such as logging.INFO, logging.DEBUG, etc.
You can also use one of the Scalyr debug levels, such as DEBUG_LEVEL_0, DEBUG_LEVEL_1, etc.
@type level: int
"""
__log_manager__.set_log_level(level)
#
# _srcfile is used when walking the stack to check when we've got the first
# caller stack frame. This is copied from the logging/__init__.py
#
示例2: getsourcefile
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import py [as 别名]
def getsourcefile(pyObject):
"""Return the filename that can be used to locate an object's source.
Return None if no way can be identified to get the source.
"""
filename = getfile(pyObject)
if filename == "<stdin>":
return filename
if string.lower(filename[-4:]) in ('.pyc', '.pyo'):
filename = filename[:-4] + '.py'
for suffix, mode, _ in imp.get_suffixes():
if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix:
# Looks like a binary file. We want to only return a text file.
return None
if filename not in pathExistsOnDiskCache_:
pathExistsOnDiskCache_[filename] = os.path.exists(filename)
if pathExistsOnDiskCache_[filename]:
return filename
# only return a non-existent filename if the module has a PEP 302 loader
if hasattr(getmodule(pyObject, filename), '__loader__'):
return filename
# or it is in the linecache
if filename in linecache.cache:
return filename
示例3: alternateCurrentFrame
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import py [as 别名]
def alternateCurrentFrame():
# noinspection PyProtectedMember
return sys._getframe(3)
# We set this variable to True after close_handlers() has been called (this happens when termination
# handler function is called when shutting down the agent.
# This way we can avoid "IOError: [Errno 0] Error" errors which may appear in stdout on agent
# shutdown when using agent_main.py stop command which sends SIGTERM signal multiple times.
# Those logs appeared if we try to log a message inside SIGTERM handler after all the log
# handlers have already been closed.
示例4: tbsource
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import py [as 别名]
def tbsource(tb, context=6):
"""Get source from a traceback object.
A tuple of two things is returned: a list of lines of context from
the source code, and the index of the current line within that list.
The optional second argument specifies the number of lines of context
to return, which are centered around the current line.
.. Note ::
This is adapted from inspect.py in the python 2.4 standard library,
since a bug in the 2.3 version of inspect prevents it from correctly
locating source lines in a traceback frame.
"""
lineno = tb.tb_lineno
frame = tb.tb_frame
if context > 0:
start = lineno - 1 - context//2
log.debug("lineno: %s start: %s", lineno, start)
try:
lines, dummy = inspect.findsource(frame)
except IOError:
lines, index = [''], 0
else:
all_lines = lines
start = max(start, 1)
start = max(0, min(start, len(lines) - context))
lines = lines[start:start+context]
index = lineno - 1 - start
# python 2.5 compat: if previous line ends in a continuation,
# decrement start by 1 to match 2.4 behavior
if sys.version_info >= (2, 5) and index > 0:
while lines[index-1].strip().endswith('\\'):
start -= 1
lines = all_lines[start:start+context]
else:
lines, index = [''], 0
log.debug("tbsource lines '''%s''' around index %s", lines, index)
return (lines, index)
示例5: create_getattr
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import py [as 别名]
def create_getattr(module) -> typing.Callable:
"""
Create __getattr__ of uninstantiated module.
Will return values from `uninstantiated` network if exist, if not
will check it's `instantiated` network (if it exists), otherwise
return `NoAttributeError`.
Can be considered proxy passing user calls `module` after instantiation.
Parameters
----------
module : str
Name of variable where instantiated module will be saved. Usually equal to
global variable `MODULE`
Returns
-------
Callable
__getattr__ function
"""
def __getattr__(self, name) -> str:
if name == module:
return super(type(self), self).__getattr__(name)
return getattr(getattr(self, module), name)
return __getattr__
# FIXED FOR PyTorch 1.4.0, 1.2.0 should work fine as well although it may throw warnings
# For warning regarding inability to find source code
# https://github.com/pytorch/pytorch/blob/master/torch/_utils_internal.py#L44
# getsourcefile
# https://github.com/python/cpython/blob/master/Lib/inspect.py#L692
# getfile
# https://github.com/python/cpython/blob/master/Lib/inspect.py#L654
# Simulate self.__module__.__file__ variable appropriately
# getsourcelines
# https://github.com/python/cpython/blob/master/Lib/inspect.py#L958