當前位置: 首頁>>代碼示例>>Python>>正文


Python ast.With方法代碼示例

本文整理匯總了Python中ast.With方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.With方法的具體用法?Python ast.With怎麽用?Python ast.With使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ast的用法示例。


在下文中一共展示了ast.With方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: indent_not_multiple_of_tab_size

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def indent_not_multiple_of_tab_size(project_folder, tab_size, *args, **kwargs):
    """
        Since there are cases for which col_offset is computed incorrectly,
        this validator must be nothing more than a simple warning.

        It compliments the pep8 validator which tends to fail in cases when
        the indent is incorrect.
    """
    node_types_to_validate = (ast.For, ast.If, ast.FunctionDef, ast.With)
    for parsed_file in project_folder.get_parsed_py_files():
        lines_offsets = file_helpers.get_line_offsets(parsed_file.content)
        for node in ast.walk(parsed_file.ast_tree):
            if not ast_helpers.is_node_offset_fine(
                node,
                lines_offsets,
                node_types_to_validate,
                tab_size,
            ):
                return parsed_file.get_name_with_line(node.lineno) 
開發者ID:devmanorg,項目名稱:fiasko_bro,代碼行數:21,代碼來源:syntax.py

示例2: test_node_types

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def test_node_types(self):
        """
        Test that the typeable node types are collected
        """
        visitor = self._run_visitor(
            """\
x = 1  # assignment
for x in range(1): pass  # for loop
def f(): pass  # function definition
with a as b: pass  # with statement
"""
        )
        self.assertEqual(visitor.typeable_lines, [1, 2, 3, 4])
        self.assertIsInstance(visitor.typeable_nodes[1], ast.Assign)
        self.assertIsInstance(visitor.typeable_nodes[2], ast.For)
        self.assertIsInstance(visitor.typeable_nodes[3], ast.FunctionDef)
        self.assertIsInstance(visitor.typeable_nodes[4], ast.With) 
開發者ID:PyCQA,項目名稱:pyflakes,代碼行數:19,代碼來源:test_checker.py

示例3: _process_instr_setup_with

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def _process_instr_setup_with(instr, queue, stack, body, context):
    items = [make_withitem(queue, stack)]
    block_body = instrs_to_body(
        pop_with_body_instrs(instr, queue),
        context,
    )

    # Handle compound with statement (e.g. "with a, b").
    if len(block_body) == 1 and isinstance(block_body[0], ast.With):
        nested_with = block_body[0]
        # Merge the inner block's items with our top-level items.
        items += nested_with.items
        # Use the inner block's body as the real body.
        block_body = nested_with.body

    return body.append(
        ast.With(items=items, body=block_body)
    ) 
開發者ID:llllllllll,項目名稱:codetransformer,代碼行數:20,代碼來源:_343.py

示例4: translate_with_34

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [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

示例5: check_is_continued_with

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def check_is_continued_with(self, node):
    """Return True if the node continues a previous `with` statement.

    In python 2.x, `with` statments with many context expressions get parsed as
    a tree of With nodes. E.g, the following two syntax forms are
    indistinguishable in the ast in python 2.

    with a, b, c:
      do_something()

    with a:
      with b:
        with c:
          do_something()

    This method should return True for the `with b` and `with c` nodes.
    """ 
開發者ID:google,項目名稱:pasta,代碼行數:19,代碼來源:annotate.py

示例6: get_variables_from_node

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def get_variables_from_node(node: ast.AST) -> List[str]:
    """
    Gets the assigned names from the list of nodes.

    Can be used with any nodes that operate with ``ast.Name`` or ``ast.Tuple``
    as targets for the assignment.

    Can be used with nodes like ``ast.Assign``, ``ast.Tuple``, ``ast.For``,
    ``ast.With``, etc.
    """
    names: List[str] = []
    naive_attempt = extract_name(node)

    if naive_attempt:
        names.append(naive_attempt)
    elif isinstance(node, ast.Tuple):
        for subnode in node.elts:
            extracted_name = get_variables_from_node(subnode)
            if extracted_name:
                names.extend(extracted_name)
    return names 
開發者ID:wemake-services,項目名稱:wemake-python-styleguide,代碼行數:23,代碼來源:name_nodes.py

示例7: with_stmt_rewrite

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def with_stmt_rewrite(mark, items, body, is_async=False):
    ty = ast.AsyncWith if is_async else ast.With
    return ty(items, body, **loc @ mark) 
開發者ID:Xython,項目名稱:YAPyPy,代碼行數:5,代碼來源:helper.py

示例8: visit_AsyncWith

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def visit_AsyncWith(self, node):
        return self.visit(ast.With(**node.__dict__)) 
開發者ID:python-security,項目名稱:pyt,代碼行數:4,代碼來源:transformer.py

示例9: test_with

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [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

示例10: insert_returns

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def insert_returns(body: List[ast.AST]) -> None:
    if isinstance(body[-1], ast.Expr):
        body[-1] = ast.Return(body[-1].value)
        ast.fix_missing_locations(body[-1])
    elif isinstance(body[-1], ast.If):
        insert_returns(body[-1].body)
        insert_returns(body[-1].orelse)
    elif isinstance(body[-1], (ast.With, ast.AsyncWith)):
        insert_returns(body[-1].body) 
開發者ID:tulir,項目名稱:mautrix-python,代碼行數:11,代碼來源:manhole.py

示例11: visit_stmt

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [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

示例12: visit_With

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def visit_With(self, node):
        new_node = gast.With(
            [gast.withitem(
                self._visit(node.context_expr),
                self._visit(node.optional_vars)
            )],
            self._visit(node.body),
            None,  # type_comment
        )
        gast.copy_location(new_node, node)
        new_node.end_lineno = new_node.end_col_offset = None
        return new_node 
開發者ID:serge-sans-paille,項目名稱:gast,代碼行數:14,代碼來源:ast2.py

示例13: visit_With

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def visit_With(self, node):
            new_node = gast.With(
                self._visit(node.items),
                self._visit(node.body),
                None,  # type_comment
            )
            gast.copy_location(new_node, node)
            return new_node 
開發者ID:serge-sans-paille,項目名稱:gast,代碼行數:10,代碼來源:ast3.py

示例14: insert_returns

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def insert_returns(body):  # for +eval, thanks to nitros12 on github for the code
    # insert return stmt if the last expression is a expression statement
    if isinstance(body[-1], ast.Expr):
        body[-1] = ast.Return(body[-1].value)
        ast.fix_missing_locations(body[-1])

    # for if statements, we insert returns into the body and the orelse
    if isinstance(body[-1], ast.If):
        insert_returns(body[-1].body)
        insert_returns(body[-1].orelse)

    # for with blocks, again we insert returns into the body
    if isinstance(body[-1], ast.With):
        insert_returns(body[-1].body) 
開發者ID:BibleBot,項目名稱:BibleBot,代碼行數:16,代碼來源:utils.py

示例15: p_with_item_1

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def p_with_item_1(p):
    '''with_item : test'''
    #                 1
    p[0] = ast.With(p[1], None, [], rule=inspect.currentframe().f_code.co_name)
    inherit_lineno(p[0], p[1]) 
開發者ID:histogrammar,項目名稱:histogrammar-python,代碼行數:7,代碼來源:hgawk_grammar.py


注:本文中的ast.With方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。