def boolean_operations(self):
if len(self.expr.values) != 2:
raise StructureException("Expected two arguments for a bool op", self.expr)
left = Expr.parse_value_expr(self.expr.values[0], self.context)
right = Expr.parse_value_expr(self.expr.values[1], self.context)
if not is_base_type(left.typ, 'bool') or not is_base_type(right.typ, 'bool'):
raise TypeMismatchException("Boolean operations can only be between booleans!", self.expr)
if isinstance(self.expr.op, ast.And):
op = 'and'
elif isinstance(self.expr.op, ast.Or):
op = 'or'
else:
raise Exception("Unsupported bool op: " + self.expr.op)
return LLLnode.from_list([op, left, right], typ='bool', pos=getpos(self.expr))
# Unary operations (only "not" supported)
python类And()的实例源码
def safe_eval_gpr(expr, conf_genes):
"""Internal function to evaluate a gene-protein rule in an
injection-safe manner (hopefully).
"""
if isinstance(expr, Expression):
return safe_eval_gpr(expr.body, conf_genes)
elif isinstance(expr, Name):
fgid = format_gid(expr.id)
if fgid not in conf_genes:
return 0
return conf_genes[fgid]
elif isinstance(expr, BoolOp):
op = expr.op
if isinstance(op, Or):
return max(safe_eval_gpr(i, conf_genes) for i in expr.values)
elif isinstance(op, And):
return min(safe_eval_gpr(i, conf_genes) for i in expr.values)
else:
raise TypeError("unsupported operation " + op.__class__.__name__)
elif expr is None:
return 0
else:
raise TypeError("unsupported operation " + repr(expr))
def simplify_multicomp(a):
if type(a) == ast.Compare and len(a.ops) > 1:
# Only do one comparator at a time. If we don't do this, things get messy!
comps = [a.left] + a.comparators
values = [ ]
# Compare each of the pairs
for i in range(len(a.ops)):
if i > 0:
# Label all nodes as middle parts so we can recognize them later
assignPropertyToAll(comps[i], "multiCompMiddle")
values.append(ast.Compare(comps[i], [a.ops[i]], [deepcopy(comps[i+1])], multiCompPart=True))
# Combine comparisons with and operators
boolOp = ast.And(multiCompOp=True)
boolopVal = ast.BoolOp(boolOp, values, multiComp=True, global_id=a.global_id)
return boolopVal
return a
def combineConditionals(a):
"""When possible, combine conditional branches"""
if not isinstance(a, ast.AST):
return a
elif type(a) == ast.If:
for i in range(len(a.body)):
a.body[i] = combineConditionals(a.body[i])
for i in range(len(a.orelse)):
a.orelse[i] = combineConditionals(a.orelse[i])
# if a: if b: x can be - if a and b: x
if (len(a.orelse) == 0) and (len(a.body) == 1) and \
(type(a.body[0]) == ast.If) and (len(a.body[0].orelse) == 0):
a.test = ast.BoolOp(ast.And(combinedConditionalOp=True), [a.test, a.body[0].test], combinedConditional=True)
a.body = a.body[0].body
# if a: x elif b: x can be - if a or b: x
elif (len(a.orelse) == 1) and \
(type(a.orelse[0]) == ast.If) and (len(a.orelse[0].orelse) == 0):
if compareASTs(a.body, a.orelse[0].body, checkEquality=True) == 0:
a.test = ast.BoolOp(ast.Or(combinedConditionalOp=True), [a.test, a.orelse[0].test], combinedConditional=True)
a.orelse = []
return a
else:
return applyToChildren(a, combineConditionals)
expr.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
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))
def visit_BoolOp(self, node):
if len(node.values) != 2:
raise Exception("Can only handle binary bool operations at this point: %s"
% unparse(node))
left_term = self.visit(node.values[0])
right_term = self.visit(node.values[1])
# Special-case for bool circuit-examples:
if is_is_int(left_term):
left_term = left_term == IntVal(1)
if is_is_int(right_term):
right_term = right_term == IntVal(1)
if isinstance(node.op, ast.And):
return And(left_term, right_term)
elif isinstance(node.op, ast.Or):
return Or(left_term, right_term)
else:
raise Exception("Unsupported bool operation %s" % unparse(node))
def visit_BoolOp(self, node):
if type(node.op) not in (ast.And, ast.Or,):
raise SyntaxError("%s is not supported" % type(node.op))
many = list()
for value in node.values:
self.visit(value)
obj = self.data.pop()
criteria = obj if isinstance(obj, Criteria) else criteria_class.instance(Const.Bool, obj)
many.append(criteria)
if isinstance(node.op, ast.And):
cls = (criteria_class.lookup(Const.And) if len(many) == 2 else criteria_class.lookup(Const.All))
else:
cls = (criteria_class.lookup(Const.Or) if len(many) == 2 else criteria_class.lookup(Const.Any))
criteria = cls(*many)
self.data.append(criteria)
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)
def translate_pat_Tuple(self, ctx, pat, scrutinee_trans):
scrutinee_trans_copy = astx.copy_node(scrutinee_trans)
elts = pat.elts
idx = self.idx
conditions = []
binding_translations = _util.odict()
for n, (elt, ty) in enumerate(zip(elts, idx.itervalues())):
elt_scrutinee_trans = astx.make_Subscript_Num_Index(
scrutinee_trans_copy,
n)
elt_condition, elt_binding_translations = ctx.translate_pat(
elt, elt_scrutinee_trans)
conditions.append(elt_condition)
binding_translations.update(elt_binding_translations)
condition = ast.BoolOp(
op=ast.And(),
values=conditions)
return (condition, binding_translations)
def translate_pat_Dict(self, ctx, pat, scrutinee_trans):
scrutinee_trans_copy = astx.copy_node(scrutinee_trans)
keys, values = pat.keys, pat.values
idx = self.idx
conditions = []
binding_translations = _util.odict()
for key, value in zip(keys, values):
label = key.label
n = _util.odict_idx_of(idx, label)
elt_scrutinee_trans = astx.make_Subscript_Num_Index(
scrutinee_trans_copy,
n)
elt_condition, elt_binding_translations = ctx.translate_pat(
value, elt_scrutinee_trans)
conditions.append(elt_condition)
binding_translations.update(elt_binding_translations)
condition = ast.BoolOp(
op=ast.And(),
values=conditions)
return (condition, binding_translations)
def translate_pat_Call_constructor(self, ctx, pat, scrutinee_trans):
lbl = pat.func.id
tag_loc = ast.Subscript(
value=scrutinee_trans,
slice=ast.Index(value=ast.Num(n=0)))
lbl_condition = ast.Compare(
left=tag_loc,
ops=[ast.Eq()],
comparators=[ast.Str(s=lbl)])
arg = pat.args[0]
arg_scrutinee = ast.Subscript(
value=scrutinee_trans,
slice=ast.Index(value=ast.Num(n=1)))
arg_condition, binding_translations = ctx.translate_pat(arg, arg_scrutinee)
condition = ast.BoolOp(
op=ast.And(),
values=[lbl_condition, arg_condition])
return condition, binding_translations
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def visit_If(self, node):
node = self.generic_visit(node)
if (node.orelse
and len(node.orelse) == 1
and isinstance(node.orelse[0], ast.Pass)
):
node.orelse = []
if (len(node.body) == 1
and isinstance(node.body[0], ast.Pass)
):
if node.orelse:
node_test = ast.UnaryOp(op=ast.Not(), operand=node.test)
if (len(node.orelse) == 1
and isinstance(node.orelse[0], ast.If)
):
node_test = ast.BoolOp\
( op = ast.And()
, values = [node_test, node.orelse[0].test]
)
node.test = ast.copy_location(node_test, node.orelse[0].test)
node.body = node.orelse[0].body
node.orelse = node.orelse[0].orelse
else:
node.test = ast.copy_location(node_test, node.test)
node.body = node.orelse
node.orelse = []
else:
node = None
return node
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
break
return result