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


Python ast.withitem方法代码示例

本文整理汇总了Python中ast.withitem方法的典型用法代码示例。如果您正苦于以下问题:Python ast.withitem方法的具体用法?Python ast.withitem怎么用?Python ast.withitem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ast的用法示例。


在下文中一共展示了ast.withitem方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle_with

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def handle_with(expr) -> Optional[Token]:
    token_info = dict(line=expr.lineno, col=expr.col_offset)
    for item in expr.items:
        if isinstance(item, ast.withitem):
            item = item.context_expr
        else:
            item = item[0]
        if _is_pathlib_write(item):
            return Token(value='Path.open', **token_info)
        if not isinstance(item, TOKENS.CALL):
            continue
        name = get_name(item.func)
        if name == 'open':
            if _is_open_to_write(item):
                return Token(value='open', **token_info)
    return None 
开发者ID:life4,项目名称:deal,代码行数:18,代码来源:prints.py

示例2: make_withitem

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def make_withitem(queue, stack):
    """
    Make an ast.withitem node.
    """
    context_expr = make_expr(stack)
    # This is a POP_TOP for just "with <expr>:".
    # This is a STORE_NAME(name) for "with <expr> as <name>:".
    as_instr = queue.popleft()
    if isinstance(as_instr, (instrs.STORE_FAST,
                             instrs.STORE_NAME,
                             instrs.STORE_DEREF,
                             instrs.STORE_GLOBAL)):
        return ast.withitem(
            context_expr=context_expr,
            optional_vars=make_assign_target(as_instr, queue, stack),
        )
    elif isinstance(as_instr, instrs.POP_TOP):
        return ast.withitem(context_expr=context_expr, optional_vars=None)
    else:
        raise DecompilationError(
            "Don't know how to make withitem from %s" % as_instr,
        ) 
开发者ID:llllllllll,项目名称:codetransformer,代码行数:24,代码来源:_343.py

示例3: translate_with_34

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def translate_with_34(self, exp):
        keyword_with, items, *body = exp
        pre = []
        items_py = []
        for item in items:
            item_pre, item_value = self.translate(item[0], False)
            pre.extend(item_pre)
            var = item[1]
            items_py.append(ast.withitem(context_expr=item_value,
                                         optional_vars=ast.Name(id=var.name,
                                                                ctx=ast.Store(),
                                                                lineno=var.lineno,
                                                                col_offset=0),
                                         lineno=var.lineno,
                                         col_offset=0))

        body_py = self._translate_sequence(body, True)
        pre.append(ast.With(items=items_py,
                            body=body_py,
                            lineno=keyword_with.lineno,
                            col_offset=0))
        return pre, self.translate(NONE_SYM, False)[1] 
开发者ID:i2y,项目名称:mochi,代码行数:24,代码来源:translation.py

示例4: visit_withitem

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def visit_withitem(self, node: ast.withitem) -> None:
        """
        Visits ``with`` and ``async with`` declarations.

        Raises:
            BlockAndLocalOverlapViolation

        """
        if node.optional_vars:
            parent = cast(AnyWith, get_parent(node))
            names = defs.extract_names(node.optional_vars)
            self._scope(parent, names, is_local=False)
            self._outer_scope(parent, names)
        self.generic_visit(node)

    # Locals: 
开发者ID:wemake-services,项目名称:wemake-python-styleguide,代码行数:18,代码来源:blocks.py

示例5: test_with

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def test_with(self):
        p = ast.Pass()
        self.stmt(ast.With([], [p]), "empty items on With")
        i = ast.withitem(ast.Num(3), None)
        self.stmt(ast.With([i], []), "empty body on With")
        i = ast.withitem(ast.Name("x", ast.Store()), None)
        self.stmt(ast.With([i], [p]), "must have Load context")
        i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
        self.stmt(ast.With([i], [p]), "must have Store context") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:11,代码来源:test_ast.py

示例6: visit_stmt

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def visit_stmt(self, node):
        # type: (ast.stmt) -> ast.With
        """
        Every statement in the original code becomes:

        with _treetrace_hidden_with_stmt(_tree_index):
            <statement>

        where the _treetrace_hidden_with_stmt function is the the corresponding method with the
        TreeTracerBase and traced_file arguments already filled in (see _trace_methods_dict)
        """
        context_expr = self._create_simple_marker_call(
            super(_NodeVisitor, self).generic_visit(node),
            TreeTracerBase._treetrace_hidden_with_stmt)

        if PY3:
            wrapped = ast.With(
                items=[ast.withitem(context_expr=context_expr)],
                body=[node],
            )
        else:
            wrapped = ast.With(
                context_expr=context_expr,
                body=[node],
            )
        ast.copy_location(wrapped, node)
        ast.fix_missing_locations(wrapped)
        return wrapped 
开发者ID:alexmojaki,项目名称:executing,代码行数:30,代码来源:tracer.py

示例7: _check_open_call_context

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def _check_open_call_context(self, node: ast.Call) -> None:
        function_name = functions.given_function_called(node, {'open'})
        if not function_name:
            return

        if isinstance(nodes.get_parent(node), ast.withitem):
            # We do not care about `with` or `async with` - both are fine.
            return

        self.add_violation(OpenWithoutContextManagerViolation(node)) 
开发者ID:wemake-services,项目名称:wemake-python-styleguide,代码行数:12,代码来源:functions.py

示例8: visit_withitem

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def visit_withitem(self, node: ast.withitem) -> None:
        """
        Checks that we cannot create explicit unused context variables.

        Raises:
            UnusedVariableIsDefinedViolation

        """
        if node.optional_vars:
            self._check_assign_unused(
                cast(ast.AST, nodes.get_parent(node)),
                name_nodes.get_variables_from_node(node.optional_vars),
                is_local=True,
            )
        self.generic_visit(node) 
开发者ID:wemake-services,项目名称:wemake-python-styleguide,代码行数:17,代码来源:naming.py

示例9: visit_withitem

# 需要导入模块: import ast [as 别名]
# 或者: from ast import withitem [as 别名]
def visit_withitem(self, node: ast.withitem) -> None:
        """
        Checks that all variables inside context managers defined correctly.

        Raises:
            ContextManagerVariableDefinitionViolation

        """
        self._check_variable_definitions(node)
        self.generic_visit(node) 
开发者ID:wemake-services,项目名称:wemake-python-styleguide,代码行数:12,代码来源:keywords.py


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