我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用operator.__le__()。
def get_operator(op): if not op: return None if "ge" in op or ">=" in op: opr = operator.__ge__ elif "gt" in op or ">" in op: opr = operator.__gt__ elif "le" in op or "<=" in op: opr = operator.__le__ elif "lt" in op or "<" in op: opr = operator.__lt__ elif "eq" in op or "=" in op or "==" in op: opr = operator.eq elif "ne" in op or "!=" in op or "<>" in op: opr = operator.ne return opr
def __le__(self, rhs): return op_relational(self, rhs, operator.__le__)
def __le__(self, other): return self.x <= other
def __le__(self, other): return Vector([a <= b for a, b in zip(self.data, self.__cast(other))])
def test_misbehavin(self): class Misb: def __lt__(self_, other): return 0 def __gt__(self_, other): return 0 def __eq__(self_, other): return 0 def __le__(self_, other): self.fail("This shouldn't happen") def __ge__(self_, other): self.fail("This shouldn't happen") def __ne__(self_, other): self.fail("This shouldn't happen") a = Misb() b = Misb() self.assertEqual(a<b, 0) self.assertEqual(a==b, 0) self.assertEqual(a>b, 0)
def test_misbehavin(self): class Misb: def __lt__(self_, other): return 0 def __gt__(self_, other): return 0 def __eq__(self_, other): return 0 def __le__(self_, other): self.fail("This shouldn't happen") def __ge__(self_, other): self.fail("This shouldn't happen") def __ne__(self_, other): self.fail("This shouldn't happen") def __cmp__(self_, other): raise RuntimeError, "expected" a = Misb() b = Misb() self.assertEqual(a<b, 0) self.assertEqual(a==b, 0) self.assertEqual(a>b, 0) self.assertRaises(RuntimeError, cmp, a, b)
def lessThanOrEqual(typeA, typeB): if operator.__le__(typeA.number.getNum(), typeB.number.getNum()): return True else: return False
def __le__(self, other): return self.__comparator__(other, operator.__le__)
def test_le(self): self._test_incompatible_types_fail(operator.__le__) self.assertTrue(no_flags <= no_flags) self.assertTrue(no_flags <= all_flags) self.assertTrue(no_flags <= f0) self.assertTrue(no_flags <= f1) self.assertTrue(no_flags <= f2) self.assertTrue(no_flags <= f01) self.assertTrue(no_flags <= f02) self.assertTrue(no_flags <= f12) self.assertFalse(f0 <= no_flags) self.assertTrue(f0 <= all_flags) self.assertTrue(f0 <= f0) self.assertFalse(f0 <= f1) self.assertFalse(f0 <= f2) self.assertTrue(f0 <= f01) self.assertTrue(f0 <= f02) self.assertFalse(f0 <= f12) self.assertFalse(f01 <= no_flags) self.assertTrue(f01 <= all_flags) self.assertFalse(f01 <= f0) self.assertFalse(f01 <= f1) self.assertFalse(f01 <= f2) self.assertTrue(f01 <= f01) self.assertFalse(f01 <= f02) self.assertFalse(f01 <= f12) self.assertTrue(no_flags <= all_flags) self.assertTrue(all_flags <= all_flags) self.assertTrue(f0 <= all_flags) self.assertTrue(f1 <= all_flags) self.assertTrue(f2 <= all_flags) self.assertTrue(f01 <= all_flags) self.assertTrue(f02 <= all_flags) self.assertTrue(f12 <= all_flags)
def test_contains(self): self.assertNotIn(MyOtherFlags.of0, MyFlags.all_flags) self.assertNotIn(MyOtherFlags.of0, MyFlags.f0) self.assertNotIn(False, MyFlags.f0) self.assertNotIn(True, MyFlags.f0) self.assertNotIn('', MyFlags.f0) self.assertNotIn('my_string', MyFlags.f0) self.assertNotIn(4, MyFlags.f0) self.assertNotIn(5.5, MyFlags.f0) self.assertNotIn(None, MyFlags.f0) # same test cases as in case of operator.__le__(item, flags) self.assertTrue(no_flags in no_flags) self.assertTrue(no_flags in all_flags) self.assertTrue(no_flags in f0) self.assertTrue(no_flags in f1) self.assertTrue(no_flags in f2) self.assertTrue(no_flags in f01) self.assertTrue(no_flags in f02) self.assertTrue(no_flags in f12) self.assertFalse(f0 in no_flags) self.assertTrue(f0 in all_flags) self.assertTrue(f0 in f0) self.assertFalse(f0 in f1) self.assertFalse(f0 in f2) self.assertTrue(f0 in f01) self.assertTrue(f0 in f02) self.assertFalse(f0 in f12) self.assertFalse(f01 in no_flags) self.assertTrue(f01 in all_flags) self.assertFalse(f01 in f0) self.assertFalse(f01 in f1) self.assertFalse(f01 in f2) self.assertTrue(f01 in f01) self.assertFalse(f01 in f02) self.assertFalse(f01 in f12) self.assertTrue(no_flags in all_flags) self.assertTrue(all_flags in all_flags) self.assertTrue(f0 in all_flags) self.assertTrue(f1 in all_flags) self.assertTrue(f2 in all_flags) self.assertTrue(f01 in all_flags) self.assertTrue(f02 in all_flags) self.assertTrue(f12 in all_flags)