我们从Python开源项目中,提取了以下25个代码示例,用于说明如何使用ast.Gt()。
def undoReverse(a): tmp = None if type(a) == ast.Lt: tmp = ast.Gt() elif type(a) == ast.LtE: tmp = ast.GtE() elif type(a) == ast.Gt: tmp = ast.Lt() elif type(a) == ast.GtE: tmp = ast.LtE() else: return a transferMetaData(a, tmp) return tmp # Applies special functions if they're included as metadata OR if they're specified by ID
def doCompare(op, left, right): """Perform the given AST comparison on the values""" top = type(op) if top == ast.Eq: return left == right elif top == ast.NotEq: return left != right elif top == ast.Lt: return left < right elif top == ast.LtE: return left <= right elif top == ast.Gt: return left > right elif top == ast.GtE: return left >= right elif top == ast.Is: return left is right elif top == ast.IsNot: return left is not right elif top == ast.In: return left in right elif top == ast.NotIn: return left not in right
def cmpop_str(op: ast.AST) -> str: if isinstance(op, ast.Eq): return '==' if isinstance(op, ast.NotEq): return '!=' if isinstance(op, ast.Lt): return '<' if isinstance(op, ast.LtE): return '<=' if isinstance(op, ast.Gt): return '>' if isinstance(op, ast.GtE): return '>=' error(loc(op), "Invalid compare operator encountered: {0}:{1}. Check supported intrisics.".format(op.lineno, op.col_offset)) return 'INVALID_CMPOP' # Parsers # ------------------------------------------------------------------------------
def compare(self): left = Expr.parse_value_expr(self.expr.left, self.context) right = Expr.parse_value_expr(self.expr.comparators[0], self.context) if isinstance(self.expr.ops[0], ast.In) and \ isinstance(right.typ, ListType): if not are_units_compatible(left.typ, right.typ.subtype) and not are_units_compatible(right.typ.subtype, left.typ): raise TypeMismatchException("Can't use IN comparison with different types!", self.expr) return self.build_in_comparator() else: if not are_units_compatible(left.typ, right.typ) and not are_units_compatible(right.typ, left.typ): raise TypeMismatchException("Can't compare values with different units!", self.expr) if len(self.expr.ops) != 1: raise StructureException("Cannot have a comparison with more than two elements", self.expr) if isinstance(self.expr.ops[0], ast.Gt): op = 'sgt' elif isinstance(self.expr.ops[0], ast.GtE): op = 'sge' elif isinstance(self.expr.ops[0], ast.LtE): op = 'sle' elif isinstance(self.expr.ops[0], ast.Lt): op = 'slt' elif isinstance(self.expr.ops[0], ast.Eq): op = 'eq' elif isinstance(self.expr.ops[0], ast.NotEq): op = 'ne' else: raise Exception("Unsupported comparison operator") if not is_numeric_type(left.typ) or not is_numeric_type(right.typ): if op not in ('eq', 'ne'): raise TypeMismatchException("Invalid type for comparison op", self.expr) ltyp, rtyp = left.typ.typ, right.typ.typ if ltyp == rtyp: return LLLnode.from_list([op, left, right], typ='bool', pos=getpos(self.expr)) elif ltyp == 'decimal' and rtyp == 'num': return LLLnode.from_list([op, left, ['mul', right, DECIMAL_DIVISOR]], typ='bool', pos=getpos(self.expr)) elif ltyp == 'num' and rtyp == 'decimal': return LLLnode.from_list([op, ['mul', left, DECIMAL_DIVISOR], right], typ='bool', pos=getpos(self.expr)) else: raise TypeMismatchException("Unsupported types for comparison: %r %r" % (ltyp, rtyp), self.expr)
def reverse(op): """Reverse the direction of the comparison for normalization purposes""" rev = not op.reversed if hasattr(op, "reversed") else True if type(op) == ast.Gt: newOp = ast.Lt() transferMetaData(op, newOp) newOp.reversed = rev return newOp elif type(op) == ast.GtE: newOp = ast.LtE() transferMetaData(op, newOp) newOp.reversed = rev return newOp else: return op # Do not change!
def test_base_classes(self): self.assertTrue(issubclass(ast.For, ast.stmt)) self.assertTrue(issubclass(ast.Name, ast.expr)) self.assertTrue(issubclass(ast.stmt, ast.AST)) self.assertTrue(issubclass(ast.expr, ast.AST)) self.assertTrue(issubclass(ast.comprehension, ast.AST)) self.assertTrue(issubclass(ast.Gt, ast.AST))
def visit_Compare(self, node): left_term = self.visit(node.left) if len(node.comparators) > 1: raise Exception("Cannot handle 'foo > bar > baz' comparison in %s" % unparse(node)) right_term = self.visit(node.comparators[0]) op = node.ops[0] if isinstance(op, ast.Eq): if self.__is_bool(left_term) and self.__is_bool(right_term): if left_term == True: return right_term elif right_term == True: return left_term elif left_term == False: return Not(right_term) elif right_term == False: return Not(left_term) return left_term == right_term elif isinstance(op, ast.Lt): return left_term < right_term elif isinstance(op, ast.LtE): return left_term <= right_term elif isinstance(op, ast.Gt): return left_term > right_term elif isinstance(op, ast.GtE): return left_term >= right_term else: raise Exception("Unhandled operators '%s' in %s" % (unparse(op), unparse(node)))
def Gt(): return functools.partial(_compare, operator.gt)
def whereeval(str_, get=None): """Evaluate a set operation string, where each Name is fetched""" if get is None: import redbiom config = redbiom.get_config() get = redbiom._requests.make_get(config) # Load is subject to indirection to simplify testing globals()['Load'] = make_Load(get) formed = ast.parse(str_, mode='eval') node_types = (ast.Compare, ast.In, ast.NotIn, ast.BoolOp, ast.And, ast.Name, ast.Or, ast.Eq, ast.Lt, ast.LtE, ast.Gt, ast.GtE, ast.NotEq, ast.Str, ast.Num, ast.Load, ast.Expression, ast.Tuple, ast.Is, ast.IsNot) for node in ast.walk(formed): if not isinstance(node, node_types): raise TypeError("Unsupported node type: %s" % ast.dump(node)) result = eval(ast.dump(formed)) # clean up global Load del Load return result
def Gt(self, key, right): c = criteria_class.instance(Const.Gt, key, right) self._push(c) return self
def __init__(self, key, right): super(Gt, self).__init__(key, right, operator.gt)
def mutate_Lt(self, node): return ast.Gt()
def mutate_GtE_to_Gt(self, node): return ast.Gt()
def pythonast(self, args, tonative=False): return ast.Compare(args[0], [ast.Gt()], [args[1]])
def pythonast(self, args, tonative=False): arg, = args justlog = ast.Call(ast.Attribute(ast.Name("$math", ast.Load()), "log", ast.Load()), [arg], [], None, None) if self.base == math.e: if tonative: return justlog else: return ast.IfExp(ast.Compare(arg, [ast.Gt()], [ast.Num(0)]), justlog, ast.Num(-inf)) else: scaled = ast.BinOp(justlog, ast.Div(), ast.Num(math.log(self.base))) if tonative: return scaled else: return ast.IfExp(ast.Compare(arg, [ast.Gt()], [ast.Num(0)]), scaled, ast.Num(-inf))
def syn_Compare(self, ctx, e): left, ops, comparators = e.left, e.ops, e.comparators for op in ops: if isinstance(op, (ast.Lt, ast.LtE, ast.Gt, ast.GtE)): raise _errors.TyError("No ordering relation on complex numbers.", e) elif isinstance(op, (ast.In, ast.NotIn)): raise _errors.TyError("Type complex does not support this operator.", op) for e_ in _util.tpl_cons(left, comparators): if hasattr(e_, 'match'): continue # already synthesized ctx.ana(e_, self) return _boolean.boolean
def areDisjoint(a, b): """Are the sets of values that satisfy these two boolean constraints disjoint?""" # The easiest way to be disjoint is to have comparisons that cover different areas if type(a) == type(b) == ast.Compare: aop = a.ops[0] bop = b.ops[0] aLeft = a.left aRight = a.comparators[0] bLeft = b.left bRight = b.comparators[0] alblComp = compareASTs(aLeft, bLeft, checkEquality=True) albrComp = compareASTs(aLeft, bRight, checkEquality=True) arblComp = compareASTs(aRight, bLeft, checkEquality=True) arbrComp = compareASTs(aRight, bRight, checkEquality=True) altype = type(aLeft) in [ast.Num, ast.Str] artype = type(aRight) in [ast.Num, ast.Str] bltype = type(bLeft) in [ast.Num, ast.Str] brtype = type(bRight) in [ast.Num, ast.Str] if (type(aop) == ast.Eq and type(bop) == ast.NotEq) or \ (type(bop) == ast.Eq and type(aop) == ast.NotEq): # x == y, x != y if (alblComp == 0 and arbrComp == 0) or (albrComp == 0 and arblComp == 0): return True elif type(aop) == type(bop) == ast.Eq: if (alblComp == 0 and arbrComp == 0) or (albrComp == 0 and arblComp == 0): return False # x = num1, x = num2 elif alblComp == 0 and artype and brtype: return True elif albrComp == 0 and artype and bltype: return True elif arblComp == 0 and altype and brtype: return True elif arbrComp == 0 and altype and bltype: return True elif (type(aop) == ast.Lt and type(bop) == ast.GtE) or \ (type(aop) == ast.Gt and type(bop) == ast.LtE) or \ (type(aop) == ast.LtE and type(bop) == ast.Gt) or \ (type(aop) == ast.GtE and type(bop) == ast.Lt) or \ (type(aop) == ast.Is and type(bop) == ast.IsNot) or \ (type(aop) == ast.IsNot and type(bop) == ast.Is) or \ (type(aop) == ast.In and type(bop) == ast.NotIn) or \ (type(aop) == ast.NotIn and type(bop) == ast.In): if alblComp == 0 and arbrComp == 0: return True elif (type(aop) == ast.Lt and type(bop) == ast.LtE) or \ (type(aop) == ast.Gt and type(bop) == ast.GtE) or \ (type(aop) == ast.LtE and type(bop) == ast.Lt) or \ (type(aop) == ast.GtE and type(bop) == ast.Gt): if albrComp == 0 and arblComp == 0: return True elif type(a) == type(b) == ast.BoolOp: return False # for now- TODO: when is this not true? elif type(a) == ast.UnaryOp and type(a.op) == ast.Not: if compareASTs(a.operand, b, checkEquality=True) == 0: return True elif type(b) == ast.UnaryOp and type(b.op) == ast.Not: if compareASTs(b.operand, a, checkEquality=True) == 0: return True return False
def negate(op): """Return the negation of the provided operator""" if op == None: return None top = type(op) neg = not op.negated if hasattr(op, "negated") else True if top == ast.And: newOp = ast.Or() elif top == ast.Or: newOp = ast.And() elif top == ast.Eq: newOp = ast.NotEq() elif top == ast.NotEq: newOp = ast.Eq() elif top == ast.Lt: newOp = ast.GtE() elif top == ast.GtE: newOp = ast.Lt() elif top == ast.Gt: newOp = ast.LtE() elif top == ast.LtE: newOp = ast.Gt() elif top == ast.Is: newOp = ast.IsNot() elif top == ast.IsNot: newOp = ast.Is() elif top == ast.In: newOp = ast.NotIn() elif top == ast.NotIn: newOp = ast.In() elif top == ast.NameConstant and op.value in [True, False]: op.value = not op.value op.negated = neg return op elif top == ast.Compare: if len(op.ops) == 1: op.ops[0] = negate(op.ops[0]) op.negated = neg return op else: values = [] allOperands = [op.left] + op.comparators for i in range(len(op.ops)): values.append(ast.Compare(allOperands[i], [negate(op.ops[i])], [allOperands[i+1]], multiCompPart=True)) newOp = ast.BoolOp(ast.Or(multiCompOp=True), values, multiComp=True) elif top == ast.UnaryOp and type(op.op) == ast.Not and \ eventualType(op.operand) == bool: # this can mess things up type-wise return op.operand else: # this is a normal value, so put a not around it newOp = ast.UnaryOp(ast.Not(addedNot=True), op) transferMetaData(op, newOp) newOp.negated = neg return newOp