本文整理汇总了Python中contextlib2.contextmanager方法的典型用法代码示例。如果您正苦于以下问题:Python contextlib2.contextmanager方法的具体用法?Python contextlib2.contextmanager怎么用?Python contextlib2.contextmanager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contextlib2
的用法示例。
在下文中一共展示了contextlib2.contextmanager方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fail
# 需要导入模块: import contextlib2 [as 别名]
# 或者: from contextlib2 import contextmanager [as 别名]
def fail(self):
"""Mark the action as having failed.
Raises:
ActionFailedError: This exception is always raised, by this function,
but should be caught by the contextmanager.
"""
self.success = False
raise ActionFailedError()
示例2: func_span
# 需要导入模块: import contextlib2 [as 别名]
# 或者: from contextlib2 import contextmanager [as 别名]
def func_span(func, tags=None, require_active_trace=False):
"""
Creates a new local span for execution of the given `func`.
The returned span is best used as a context manager, e.g.
.. code-block:: python
with func_span('my_function'):
return my_function(...)
At this time the func should be a string name. In the future this code
can be enhanced to accept a real function and derive its qualified name.
:param func: name of the function or method
:param tags: optional tags to add to the child span
:param require_active_trace: controls what to do when there is no active
trace. If require_active_trace=True, then no span is created.
If require_active_trace=False, a new trace is started.
:return: new child span, or a dummy context manager if there is no
active/current parent span
"""
current_span = get_current_span()
if current_span is None and require_active_trace:
@contextlib2.contextmanager
def empty_ctx_mgr():
yield None
return empty_ctx_mgr()
# TODO convert func to a proper name: module:class.func
operation_name = str(func)
return utils.start_child_span(
operation_name=operation_name, parent=current_span, tags=tags)
示例3: db_span
# 需要导入模块: import contextlib2 [as 别名]
# 或者: from contextlib2 import contextmanager [as 别名]
def db_span(sql_statement,
module_name,
sql_parameters=None,
connect_params=None,
cursor_params=None):
span = current_span_func()
@contextlib2.contextmanager
def empty_ctx_mgr():
yield None
if span is None:
return empty_ctx_mgr()
if bytes is not str and isinstance(sql_statement, bytes):
sql_statement = sql_statement.decode('utf-8', errors='ignore')
statement = sql_statement.strip()
add_sql_tag = True
if sql_statement in _TRANS_TAGS:
operation = sql_statement
add_sql_tag = False
else:
space_idx = statement.find(' ')
if space_idx == -1:
operation = '' # unrecognized format of the query
else:
operation = statement[0:space_idx]
tags = {ext_tags.SPAN_KIND: ext_tags.SPAN_KIND_RPC_CLIENT}
if add_sql_tag:
tags['sql'] = statement
if sql_parameters:
tags['sql.params'] = sql_parameters
if connect_params:
tags['sql.conn'] = connect_params
if cursor_params:
tags['sql.cursor'] = cursor_params
return utils.start_child_span(
operation_name='%s:%s' % (module_name, operation),
parent=span, tags=tags
)