本文整理匯總了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)
示例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)
示例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)
)
示例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]
示例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.
"""
示例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
示例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)
示例8: visit_AsyncWith
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import With [as 別名]
def visit_AsyncWith(self, node):
return self.visit(ast.With(**node.__dict__))
示例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")
示例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)
示例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
示例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
示例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
示例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)
示例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])