當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。