本文整理匯總了Python中ast.Set方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Set方法的具體用法?Python ast.Set怎麽用?Python ast.Set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Set方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: number_literal_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def number_literal_expression_ast_to_ir2(ast_node: ast.Num,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
positive: bool):
n = ast_node.n
if isinstance(n, float):
raise CompilationError(compilation_context, ast_node, 'Floating-point values are not supported.')
if isinstance(n, complex):
raise CompilationError(compilation_context, ast_node, 'Complex values are not supported.')
assert isinstance(n, int)
if not positive:
n = -n
if n <= -2**63:
raise CompilationError(compilation_context, ast_node,
'int value out of bounds: values lower than -2^63+1 are not supported.')
if n >= 2**63:
raise CompilationError(compilation_context, ast_node,
'int value out of bounds: values greater than 2^63-1 are not supported.')
return ir2.IntLiteral(value=n)
示例2: not_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def not_expression_ast_to_ir2(ast_node: ast.UnaryOp,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
assert isinstance(ast_node.op, ast.Not)
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'The "not" operator is not allowed in match patterns')
expr = expression_ast_to_ir2(ast_node.operand,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if expr.expr_type != ir2.BoolType():
raise CompilationError(compilation_context, ast_node.operand,
'The "not" operator is only supported for booleans, but this value has type %s.' % str(expr.expr_type))
return ir2.NotExpr(expr=expr)
示例3: unary_minus_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def unary_minus_expression_ast_to_ir2(ast_node: ast.UnaryOp,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
assert isinstance(ast_node.op, ast.USub)
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'The "-" operator is not allowed in match patterns')
expr = expression_ast_to_ir2(ast_node.operand,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if expr.expr_type != ir2.IntType():
raise CompilationError(compilation_context, ast_node.operand,
'The "-" operator is only supported for ints, but this value has type %s.' % str(expr.expr_type))
return ir2.IntUnaryMinusExpr(expr=expr)
示例4: _extract_single_type_expr_arg
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def _extract_single_type_expr_arg(ast_node: ast.Call,
called_fun_name: str,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
if ast_node.keywords:
raise CompilationError(compilation_context, ast_node.keywords[0].value,
'Keyword arguments are not supported in %s()' % called_fun_name)
if len(ast_node.args) != 1:
raise CompilationError(compilation_context, ast_node, '%s() takes 1 argument. Got: %s' % (called_fun_name, len(ast_node.args)))
[arg] = ast_node.args
arg_ir = expression_ast_to_ir2(arg,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if arg_ir.expr_type != ir2.TypeType():
raise CompilationError(compilation_context, arg, 'The argument passed to %s() should have type Type, but was: %s' % (called_fun_name, str(arg_ir.expr_type)))
return arg_ir
示例5: _make_function
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def _make_function(instr, queue, stack, body, context):
"""
Set a make_function_context, then push onto the stack.
"""
assert stack, "Empty stack before MAKE_FUNCTION."
prev = stack[-1]
expect(prev, instrs.LOAD_CONST, "before MAKE_FUNCTION")
stack.append(instr)
if is_lambda_name(prev.arg):
return
return context.update(
make_function_context=MakeFunctionContext(
closure=isinstance(instr, instrs.MAKE_CLOSURE),
)
)
示例6: _resolve_constant
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def _resolve_constant(node):
if isinstance(node, str_types):
return node.s
elif isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.List):
return [_resolve_constant(e) for e in node.elts]
elif isinstance(node, ast.Set):
return set(_resolve_constant(e) for e in node.elts)
elif isinstance(node, ast.Tuple):
return tuple(_resolve_constant(e) for e in node.elts)
elif isinstance(node, ast.Dict):
res = {}
for k, v in zip(node.keys, node.values):
res[_resolve_constant(k)] = _resolve_constant(v)
return res
else:
raise NotConstant()
# This parses .py files for simple variable assignment
# I'm making an assumption that the file will not contain any complicated code,
# or features that would be version-dependent. Since Horizon aims for
# compatibility with common systems, this should be a fair assumption.
示例7: _almost_swapped
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def _almost_swapped(self, assigns: Sequence[ast.Assign]) -> None:
previous_var: Set[Optional[str]] = set()
for assign in assigns:
current_var = {
first(name_nodes.flat_variable_names([assign])),
first(name_nodes.get_variables_from_node(assign.value)),
}
if not all(map(bool, current_var)):
previous_var.clear()
continue
if current_var == previous_var:
self.add_violation(AlmostSwappedViolation(assign))
if len(previous_var & current_var) == 1:
current_var ^= previous_var
previous_var = current_var
示例8: compare_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def compare_ast_to_ir2(ast_node: ast.Compare,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
if len(ast_node.ops) != 1 or len(ast_node.comparators) != 1:
raise CompilationError(compilation_context, ast_node, 'Comparison not supported.') # pragma: no cover
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'Comparisons are not allowed in match patterns')
lhs = ast_node.left
op = ast_node.ops[0]
rhs = ast_node.comparators[0]
if isinstance(op, ast.Eq):
return eq_ast_to_ir2(lhs, rhs, compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
elif isinstance(op, ast.NotEq):
return not_eq_ast_to_ir2(lhs, rhs, compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
elif isinstance(op, ast.In):
return in_ast_to_ir2(lhs, rhs, compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
elif isinstance(op, ast.Lt):
return int_comparison_ast_to_ir2(lhs, rhs, '<', compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
elif isinstance(op, ast.LtE):
return int_comparison_ast_to_ir2(lhs, rhs, '<=', compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
elif isinstance(op, ast.Gt):
return int_comparison_ast_to_ir2(lhs, rhs, '>', compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
elif isinstance(op, ast.GtE):
return int_comparison_ast_to_ir2(lhs, rhs, '>=', compilation_context, in_match_pattern, check_var_reference, current_stmt_line)
else:
raise CompilationError(compilation_context, ast_node, 'Comparison not supported.') # pragma: no cover
示例9: attribute_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def attribute_expression_ast_to_ir2(ast_node: ast.Attribute,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'Attribute access is not allowed in match patterns')
value_expr = expression_ast_to_ir2(ast_node.value,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if isinstance(value_expr.expr_type, ir2.TypeType):
return ir2.AttributeAccessExpr(expr=value_expr,
attribute_name=ast_node.attr,
expr_type=ir2.TypeType())
elif isinstance(value_expr.expr_type, ir2.CustomType):
for arg in value_expr.expr_type.arg_types:
if arg.name == ast_node.attr:
return ir2.AttributeAccessExpr(expr=value_expr,
attribute_name=ast_node.attr,
expr_type=arg.expr_type)
else:
lookup_result = compilation_context.get_type_symbol_definition(value_expr.expr_type.name)
assert lookup_result
raise CompilationError(compilation_context, ast_node.value,
'Values of type "%s" don\'t have the attribute "%s". The available attributes for this type are: {"%s"}.' % (
str(value_expr.expr_type), ast_node.attr, '", "'.join(sorted(arg.name
for arg in value_expr.expr_type.arg_types))),
notes=[(lookup_result.ast_node, '%s was defined here.' % str(value_expr.expr_type))])
else:
raise CompilationError(compilation_context, ast_node.value,
'Attribute access is not supported for values of type %s.' % str(value_expr.expr_type))
示例10: and_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def and_expression_ast_to_ir2(ast_node: ast.BoolOp,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
assert isinstance(ast_node.op, ast.And)
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'The "and" operator is not allowed in match patterns')
if not compilation_context.current_function_name:
raise CompilationError(compilation_context, ast_node,
'The "and" operator is only supported in functions, not at toplevel.')
assert len(ast_node.values) >= 2
exprs = []
for expr_ast_node in ast_node.values:
expr = expression_ast_to_ir2(expr_ast_node,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if expr.expr_type != ir2.BoolType():
raise CompilationError(compilation_context, expr_ast_node,
'The "and" operator is only supported for booleans, but this value has type %s.' % str(expr.expr_type))
exprs.append(expr)
final_expr = exprs[-1]
for expr in reversed(exprs[:-1]):
final_expr = ir2.AndExpr(lhs=expr, rhs=final_expr)
return final_expr
示例11: int_binary_op_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def int_binary_op_expression_ast_to_ir2(ast_node: ast.BinOp,
op: str,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
lhs = expression_ast_to_ir2(ast_node.left,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
rhs = expression_ast_to_ir2(ast_node.right,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'The "%s" operator is not allowed in match patterns' % op)
if lhs.expr_type != ir2.IntType():
raise CompilationError(compilation_context, ast_node.left,
'The "%s" operator is only supported for ints, but this value has type %s.' % (op, str(lhs.expr_type)))
if rhs.expr_type != ir2.IntType():
raise CompilationError(compilation_context, ast_node.right,
'The "%s" operator is only supported for ints, but this value has type %s.' % (op, str(rhs.expr_type)))
return ir2.IntBinaryOpExpr(lhs=lhs, rhs=rhs, op=op)
示例12: add_expression_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def add_expression_ast_to_ir2(ast_node: ast.BinOp,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
lhs = expression_ast_to_ir2(ast_node.left,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
rhs = expression_ast_to_ir2(ast_node.right,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if in_match_pattern:
raise CompilationError(compilation_context, ast_node,
'The "+" operator is not allowed in match patterns')
if not isinstance(lhs.expr_type, (ir2.IntType, ir2.ListType)):
raise CompilationError(compilation_context, ast_node.left,
'The "+" operator is only supported for ints and lists, but this value has type %s.' % str(lhs.expr_type))
if not isinstance(rhs.expr_type, (ir2.IntType, ir2.ListType)):
raise CompilationError(compilation_context, ast_node.right,
'The "+" operator is only supported for ints and lists, but this value has type %s.' % str(rhs.expr_type))
if lhs.expr_type != rhs.expr_type:
raise CompilationError(compilation_context, ast_node.left,
'Type mismatch: the LHS of "+" has type %s but the RHS has type %s.' % (str(lhs.expr_type), str(rhs.expr_type)))
if lhs.expr_type == ir2.IntType():
return ir2.IntBinaryOpExpr(lhs=lhs, rhs=rhs, op='+')
else:
return ir2.ListConcatExpr(lhs=lhs, rhs=rhs)
示例13: name_constant_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def name_constant_ast_to_ir2(ast_node: ast.NameConstant,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str]):
if isinstance(ast_node.value, bool):
return ir2.BoolLiteral(value=ast_node.value)
else:
raise CompilationError(compilation_context, ast_node, 'NameConstant not supported: ' + str(ast_node.value)) # pragma: no cover
示例14: type_pointer_expr_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def type_pointer_expr_ast_to_ir2(ast_node: ast.Call,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
return ir2.PointerTypeExpr(_extract_single_type_expr_arg(ast_node,
'Type.pointer',
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line))
示例15: type_reference_expr_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Set [as 別名]
def type_reference_expr_ast_to_ir2(ast_node: ast.Call,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
return ir2.ReferenceTypeExpr(_extract_single_type_expr_arg(ast_node,
'Type.reference',
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line))