本文整理匯總了Python中ast.Eq方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Eq方法的具體用法?Python ast.Eq怎麽用?Python ast.Eq使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Eq方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_MutateAST_visit_compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def test_MutateAST_visit_compare(idx, mut_op, lineno, compare_file, compare_expected_locs):
"""Test mutation of the == to != in the compare op."""
tree = Genome(compare_file).ast
# apply the mutation to the original tree copy
testing_tree = deepcopy(tree)
mutated_tree = MutateAST(target_idx=compare_expected_locs[idx], mutation=mut_op).visit(
testing_tree
)
# revisit in read-only mode to gather the locations of the new nodes
mast = MutateAST(readonly=True)
mast.visit(mutated_tree)
assert len(mast.locs) == 3
# check that the lineno marked for mutation is changed, otherwise original ops should
# still be present without modification
for loc in mast.locs:
if loc.lineno == lineno and loc.col_offset == 11:
assert loc.op_type == mut_op
else:
assert loc.op_type in {ast.Eq, ast.Is, ast.In} # based on compare_file fixture
示例2: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def visit_Compare(self, node):
node.obj = bool()
for n in [node.left] + node.comparators:
self.visit(n)
op, arg = node.ops[0], node.comparators[0]
# # node.expr.target = self.getObj(arg)
# # arg.target = self.getObj(node.expr)
# detect specialized case for the test
if isinstance(op, ast.Eq) and isinstance(node.left, ast.Name):
# check wether it can be a case
val = arg.obj
if isinstance(val, bool):
val = int(val) # cast bool to int first
if isinstance(val, (EnumItemType, int)):
node.case = (node.left, val)
# check whether it can be part of an edge check
n = node.left.id
if n in self.tree.sigdict:
sig = self.tree.sigdict[n]
v = self.getValue(arg)
if v is not None:
if v == 0:
node.edge = sig.negedge
elif v == 1:
node.edge = sig.posedge
示例3: comp_op_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def comp_op_rewrite(op: t.Union[Tokenizer, t.List[Tokenizer]]):
"""
('<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not')
"""
if isinstance(op, list):
op = tuple(map(lambda it: it.value, op))
else:
op = op.value
return {
'<': ast.Lt,
'>': ast.Gt,
'==': ast.Eq,
'>=': ast.GtE,
'<=': ast.LtE,
'<>': lambda: raise_exp(NotImplemented),
'!=': ast.NotEq,
'in': ast.In,
('is', ): ast.Is,
('is', 'not'): ast.IsNot,
('not', 'in'): ast.NotIn,
}[op]()
示例4: verify_ast
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def verify_ast(expr, variables_and_values):
"""Verifies that |expr| is of the form
expr ::= expr ( "or" | "and" ) expr
| identifier "==" ( string | int )
Also collects the variable identifiers and string/int values in the dict
|variables_and_values|, in the form {'var': set([val1, val2, ...]), ...}.
"""
assert isinstance(expr, (ast.BoolOp, ast.Compare))
if isinstance(expr, ast.BoolOp):
assert isinstance(expr.op, (ast.And, ast.Or))
for subexpr in expr.values:
verify_ast(subexpr, variables_and_values)
else:
assert isinstance(expr.left.ctx, ast.Load)
assert len(expr.ops) == 1
assert isinstance(expr.ops[0], ast.Eq)
var_values = variables_and_values.setdefault(expr.left.id, set())
rhs = expr.comparators[0]
assert isinstance(rhs, (ast.Str, ast.Num))
var_values.add(rhs.n if isinstance(rhs, ast.Num) else rhs.s)
示例5: cmpop
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def cmpop(self, op):
if isinstance(op, ast.Eq):
return Operator.eq
elif isinstance(op, ast.NotEq):
return Operator.ne
elif isinstance(op, ast.Lt):
return Operator.lt
elif isinstance(op, ast.LtE):
return Operator.le
elif isinstance(op, ast.Gt):
return Operator.gt
elif isinstance(op, ast.GtE):
return Operator.ge
else:
raise Exception('Unexpected CmpOp (%s)'%(op.__class__))
#Parses a function call (AST Call)
示例6: gen_compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def gen_compare(self, condition, yes_block, no_block):
# print(dir(c), c.ops, c.comparators)
# TODO: chained operators! ( 'a < b < c < d' )
assert len(condition.ops) == len(condition.comparators)
assert len(condition.ops) == 1
op_map = {
ast.Gt: ">",
ast.GtE: ">=",
ast.Lt: "<",
ast.LtE: "<=",
ast.Eq: "==",
ast.NotEq: "!=",
}
a = self.gen_expr(condition.left)
op = op_map[type(condition.ops[0])]
b = self.gen_expr(condition.comparators[0])
if a.ty is not b.ty:
self.error(condition, "Type mismatch, types must be the same.")
self.emit(ir.CJump(a, op, b, yes_block, no_block))
示例7: visit_Assert
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def visit_Assert(self, node):
if is_binary_comparison(node):
if is_comparison_type(node, ast.Eq):
return make_call_node(node, assert_equal.__name__)
elif is_comparison_type(node, ast.NotEq):
return make_call_node(node, assert_not_equal.__name__)
elif is_comparison_type(node, ast.In):
return make_call_node(node, assert_in.__name__)
elif is_comparison_type(node, ast.NotIn):
return make_call_node(node, assert_not_in.__name__)
elif is_comparison_type(node, ast.Is):
return make_call_node(node, assert_is.__name__)
elif is_comparison_type(node, ast.IsNot):
return make_call_node(node, assert_is_not.__name__)
elif is_comparison_type(node, ast.Lt):
return make_call_node(node, assert_less_than.__name__)
elif is_comparison_type(node, ast.LtE):
return make_call_node(node, assert_less_than_equal_to.__name__)
elif is_comparison_type(node, ast.Gt):
return make_call_node(node, assert_greater_than.__name__)
elif is_comparison_type(node, ast.GtE):
return make_call_node(node, assert_greater_than_equal_to.__name__)
return node
示例8: _check_compares
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def _check_compares(self, node: ast.Compare) -> None:
is_all_equals = all(isinstance(op, ast.Eq) for op in node.ops)
is_all_notequals = all(isinstance(op, ast.NotEq) for op in node.ops)
can_be_longer = is_all_notequals or is_all_equals
threshold = constants.MAX_COMPARES
if can_be_longer:
threshold += 1
if len(node.ops) > threshold:
self.add_violation(
complexity.TooLongCompareViolation(
node,
text=str(len(node.ops)),
baseline=threshold,
),
)
示例9: compare_expected_locs
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def compare_expected_locs():
"""The compare expected locations based on the fixture"""
# Py 3.7
if sys.version_info < (3, 8):
return [
LocIndex(ast_class="Compare", lineno=2, col_offset=11, op_type=ast.Eq),
LocIndex(ast_class="CompareIs", lineno=5, col_offset=11, op_type=ast.Is),
LocIndex(ast_class="CompareIn", lineno=8, col_offset=11, op_type=ast.In),
]
# Py 3.8
return [
LocIndex(
ast_class="Compare",
lineno=2,
col_offset=11,
op_type=ast.Eq,
end_lineno=2,
end_col_offset=17,
),
LocIndex(
ast_class="CompareIs",
lineno=5,
col_offset=11,
op_type=ast.Is,
end_lineno=5,
end_col_offset=17,
),
LocIndex(
ast_class="CompareIn",
lineno=8,
col_offset=11,
op_type=ast.In,
end_lineno=8,
end_col_offset=17,
),
]
####################################################################################################
# TRANSFORMERS: IF FIXTURES
####################################################################################################
示例10: _has_eq_compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def _has_eq_compare(node):
return len(node.ops) == 1 and isinstance(node.ops[0], ast.Eq)
示例11: _is_compared_with_zero
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def _is_compared_with_zero(node):
return (
isinstance(node.ops[0], (ast.Gt, ast.Eq))
and isinstance(node.comparators[0], ast.Num)
and node.comparators[0].n == 0
)
示例12: visit_Assign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def visit_Assign(self, node, **kwargs):
cmpr = ast.Compare(ops=[ast.Eq()], left=node.targets[0],
comparators=[node.value])
return self.visit(cmpr)
示例13: translate_In
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def translate_In(self, op):
return ast.Eq() if isinstance(op, ast.In) else op
示例14: compare_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [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
示例15: _eq
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Eq [as 別名]
def _eq(test: ast.Compare, n: int) -> bool:
return (
isinstance(test.ops[0], ast.Eq) and
isinstance(test.comparators[0], ast.Num) and
test.comparators[0].n == n
)