当前位置: 首页>>代码示例>>Python>>正文


Python contextlib2.contextmanager方法代码示例

本文整理汇总了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() 
开发者ID:google,项目名称:openhtf,代码行数:11,代码来源:console_output.py

示例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) 
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:36,代码来源:local_span.py

示例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
    ) 
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:45,代码来源:_dbapi2.py


注:本文中的contextlib2.contextmanager方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。