本文整理汇总了Python中ast.BoolOp方法的典型用法代码示例。如果您正苦于以下问题:Python ast.BoolOp方法的具体用法?Python ast.BoolOp怎么用?Python ast.BoolOp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.BoolOp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_BoolOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def visit_BoolOp(self, node: ast.BoolOp) -> ast.AST:
"""Boolean operations, AND/OR."""
self.generic_visit(node)
log_header = f"visit_BoolOp: {self.src_file}:"
node_span = NodeSpan(node)
idx = LocIndex(
ast_class="BoolOp",
lineno=node_span.lineno,
col_offset=node_span.col_offset,
op_type=type(node.op),
end_lineno=node_span.end_lineno,
end_col_offset=node_span.end_col_offset,
)
self.locs.add(idx)
if idx == self.target_idx and self.mutation and not self.readonly:
LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
return ast.copy_location(ast.BoolOp(op=self.mutation(), values=node.values), node)
LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
return node
示例2: visit_Compare
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def visit_Compare(self, node, **kwargs):
ops = node.ops
comps = node.comparators
# base case: we have something like a CMP b
if len(comps) == 1:
op = self.translate_In(ops[0])
binop = ast.BinOp(op=op, left=node.left, right=comps[0])
return self.visit(binop)
# recursive case: we have a chained comparison, a CMP b CMP c, etc.
left = node.left
values = []
for op, comp in zip(ops, comps):
new_node = self.visit(ast.Compare(comparators=[comp], left=left,
ops=[self.translate_In(op)]))
left = comp
values.append(new_node)
return self.visit(ast.BoolOp(op=ast.And(), values=values))
示例3: test_afterSubMult
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def test_afterSubMult(self):
'Tests after SubToMult pre-processing'
tests = [("1 + 2 - 3", ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2),
ast.BinOp(ast.Num(-1),
ast.Mult(),
ast.Num(3))])),
("1 + 2 - 3 + 4", ast.BoolOp(ast.Add(),
[ast.Num(1),
ast.Num(2),
ast.BinOp(ast.Num(-1),
ast.Mult(),
ast.Num(3)),
ast.Num(4)])),
("(1 + 2) - (3 + 4)",
ast.BoolOp(ast.Add(),
[ast.Num(1), ast.Num(2),
ast.BinOp(ast.Num(-1), ast.Mult(),
ast.BinOp(ast.Num(3), ast.Add(),
ast.Num(4)))]))]
for teststring, ref_ast in tests:
test_ast = ast.parse(teststring, mode="eval").body
test_ast = pre_processing.all_preprocessings(test_ast)
test_ast = Flattening(ast.Add).visit(test_ast)
self.assertTrue(Comparator().visit(test_ast, ref_ast))
示例4: test_onBoolOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def test_onBoolOp(self):
'Tests on BoolOp'
expr_a = ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2), ast.Num(3)])
expr_b = ast.BoolOp(ast.Add(), [ast.Num(3), ast.Num(2), ast.Num(1)])
self.assertTrue(asttools.Comparator().visit(expr_a, expr_b))
expr_a = ast.BoolOp(ast.Add, [ast.Num(1), ast.BoolOp(ast.Mult(),
[ast.Num(5),
ast.Num(6)]),
ast.Num(4)])
expr_b = ast.BoolOp(ast.Add, [ast.BoolOp(ast.Mult(), [ast.Num(6),
ast.Num(5)]),
ast.Num(4),
ast.Num(1)])
self.assertTrue(asttools.Comparator().visit(expr_a, expr_b))
示例5: __init__
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def __init__(self, patt_ast, target_ast, rep_ast, nbits=0):
'Pattern ast should have as root: BinOp, BoolOp, UnaryOp or Call'
if isinstance(patt_ast, ast.Module):
self.patt_ast = patt_ast.body[0].value
elif isinstance(patt_ast, ast.Expression):
self.patt_ast = patt_ast.body
else:
self.patt_ast = patt_ast
if isinstance(rep_ast, ast.Module):
self.rep_ast = deepcopy(rep_ast.body[0].value)
elif isinstance(rep_ast, ast.Expression):
self.rep_ast = deepcopy(rep_ast.body)
else:
self.rep_ast = deepcopy(rep_ast)
if not nbits:
getsize = asttools.GetSize()
getsize.visit(target_ast)
if getsize.result:
self.nbits = getsize.result
# default bitsize is 8
else:
self.nbits = 8
else:
self.nbits = nbits
示例6: visit_BoolOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def visit_BoolOp(self, node):
'A custom BoolOp can be used in flattened AST'
if type(node.op) not in (ast.Add, ast.Mult,
ast.BitXor, ast.BitAnd, ast.BitOr):
return self.generic_visit(node)
# get constant parts of node:
list_cste = [child for child in node.values
if isinstance(child, ast.Num)]
if len(list_cste) < 2:
return self.generic_visit(node)
rest_values = [n for n in node.values if n not in list_cste]
fake_node = Unflattening().visit(ast.BoolOp(node.op, list_cste))
fake_node = ast.Expression(fake_node)
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
rest_values.append(new_node)
return ast.BoolOp(node.op, rest_values)
示例7: normalize_compare
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def normalize_compare(node):
"""Rewrites a compare expression to a `and` expression
1 < 2 < 3 > 0
1 < 2 and 2 < 3 and 3 > 0"""
and_values = []
left = node.left
for (op, val) in zip(node.ops, node.comparators):
comp = ast.Compare(ops=[op],
left=left,
comparators=[val],
lineno=node.lineno,
col_offset=node.col_offset)
and_values.append(comp)
left = val
return ast.BoolOp(op=ast.And(),
values=and_values,
lineno=node.lineno,
col_offset=node.col_offset)
示例8: merge_exitcases
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def merge_exitcases(exit1, exit2):
"""
Merge the exitcases of two Links.
Args:
exit1: The exitcase of a Link object.
exit2: Another exitcase to merge with exit1.
Returns:
The merged exitcases.
"""
if exit1:
if exit2:
return ast.BoolOp(ast.And(), values=[exit1, exit2])
return exit1
return exit2
示例9: verify_ast
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [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)
示例10: boolop
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def boolop(self, block, node, var=None):
#Get the type of operation
if isinstance(node.op, ast.And):
op = Operator.bool_and
elif isinstance(node.op, ast.Or):
op = Operator.bool_or
else:
raise Exception('Unexpected BoolOp (%s)'%(node.op.__class__))
#Chain operations together
left = self.expression(block, node.values[0])
for node_value in node.values[1:]:
right = self.expression(block, node_value)
operation = BinaryOperation(left, op, right)
if node_value == node.values[-1] and var is not None:
#The last operation in the chain should be assigned to this variable
temp_var = var
else:
#Create a temporary variable to store the intermediate result
temp_var = self.add_variable(None, is_mask=True)
assignment = Assignment(temp_var, operation)
block.add(assignment)
left = temp_var
return temp_var
#Parses an array element (AST Subscript)
示例11: eval_expr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def eval_expr(expr):
""" Eval and expression inside a #define using a suppart of python grammar """
def _eval(node):
if isinstance(node, ast.Num):
return node.n
elif isinstance(node, ast.BinOp):
return OPERATORS[type(node.op)](_eval(node.left), _eval(node.right))
elif isinstance(node, ast.UnaryOp):
return OPERATORS[type(node.op)](_eval(node.operand))
elif isinstance(node, ast.BoolOp):
values = [_eval(x) for x in node.values]
return OPERATORS[type(node.op)](**values)
else:
raise TypeError(node)
return _eval(ast.parse(expr, mode='eval').body)
示例12: normalize_boolop
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def normalize_boolop(expr):
"""
Normalize a boolop by folding together nested And/Or exprs.
"""
optype = expr.op
newvalues = []
for subexpr in expr.values:
if not isinstance(subexpr, ast.BoolOp):
newvalues.append(subexpr)
elif type(subexpr.op) != type(optype):
newvalues.append(normalize_boolop(subexpr))
else:
# Normalize subexpression, then inline its values into the
# top-level subexpr.
newvalues.extend(normalize_boolop(subexpr).values)
return ast.BoolOp(op=optype, values=newvalues)
示例13: p_logic_or_expr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def p_logic_or_expr(t):
'''logic_or_expr : logic_or_expr OR logic_and_expr
| logic_and_expr'''
if len(t) == 4:
if isinstance(t[1], ast.BoolOp) and isinstance(t[1].op, ast.Or):
t[0] = t[1]
t[0].values.append(t[3])
else:
or_ast = ast.Or()
or_ast.lineno = t.lineno(2)
or_ast.col_offset = -1 # XXX
t[0] = ast.BoolOp(or_ast, [t[1], t[3]])
t[0].lineno = t.lineno(2)
t[0].col_offset = -1 # XXX
else:
t[0] = t[1]
示例14: p_logic_and_expr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def p_logic_and_expr(t):
'''logic_and_expr : logic_and_expr AND logic_expr
| logic_expr'''
if len(t) == 4:
if isinstance(t[1], ast.BoolOp) and isinstance(t[1].op, ast.And):
t[0] = t[1]
t[0].values.append(t[3])
else:
and_ast = ast.And()
and_ast.lineno = t.lineno(2)
and_ast.col_offset = -1 # XXX
t[0] = ast.BoolOp(and_ast, [t[1], t[3]])
t[0].lineno = t.lineno(2)
t[0].col_offset = -1 # XXX
else:
t[0] = t[1]
示例15: evaluate_node
# 需要导入模块: import ast [as 别名]
# 或者: from ast import BoolOp [as 别名]
def evaluate_node(self, node):
if isinstance(node, ast.BinOp):
return self.operators[type(node.op)](self.evaluate_node(node.left),
self.evaluate_node(node.right))
elif isinstance(node, ast.UnaryOp):
return self.operators[type(node.op)](self.evaluate_node(node.operand))
elif isinstance(node, ast.Compare):
to_string = isinstance(node.comparators[0], ast.Str)
return self.operators[type(node.ops[0])](self.evaluate_attribute_node(node.left, to_string),
self.evaluate_node(node.comparators[0]))
elif isinstance(node, ast.BoolOp):
func = all if isinstance(node.op, ast.And) else any
return func(self.evaluate_node(value) for value in node.values)
elif isinstance(node, ast.Str):
return node.s
elif isinstance(node, ast.Attribute):
return self.evaluate_attribute_node(node)
elif isinstance(node, ast.Num):
return node.n
else:
logger.error("Error during parsing")