def test_inplace(self): #operator = self.module class C(object): def __iadd__ (self, other): return "iadd" def __iand__ (self, other): return "iand" def __ifloordiv__(self, other): return "ifloordiv" def __ilshift__ (self, other): return "ilshift" def __imod__ (self, other): return "imod" def __imul__ (self, other): return "imul" def __ior__ (self, other): return "ior" def __ipow__ (self, other): return "ipow" def __irshift__ (self, other): return "irshift" def __isub__ (self, other): return "isub" def __itruediv__ (self, other): return "itruediv" def __ixor__ (self, other): return "ixor" def __getitem__(self, other): return 5 # so that C is a sequence c = C() self.assertEqual(operator.iadd (c, 5), "iadd") self.assertEqual(operator.iand (c, 5), "iand") self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") self.assertEqual(operator.ilshift (c, 5), "ilshift") self.assertEqual(operator.imod (c, 5), "imod") self.assertEqual(operator.imul (c, 5), "imul") self.assertEqual(operator.ior (c, 5), "ior") self.assertEqual(operator.ipow (c, 5), "ipow") self.assertEqual(operator.irshift (c, 5), "irshift") self.assertEqual(operator.isub (c, 5), "isub") self.assertEqual(operator.itruediv (c, 5), "itruediv") self.assertEqual(operator.ixor (c, 5), "ixor") self.assertEqual(operator.iconcat (c, c), "iadd")
def __isub__(self,other): if not isinstance(other, Unit): raise ValueError("Both operands must be of type Unit") elif self.unit != other.unit: raise ValueError("unit %s is not %s" % (self.unit, other.unit)) else: return Unit(operator.isub(self.value,other.value), self.unit)
def test_inplace_subtraction(self, input_tuple, expected): self.ureg.autoconvert_offset_to_baseunit = False (q1v, q1u), (q2v, q2u) = input_tuple # update input tuple with new values to have correct values on failure input_tuple = ((np.array([q1v]*2, dtype=np.float), q1u), (np.array([q2v]*2, dtype=np.float), q2u)) Q_ = self.Q_ qin1, qin2 = input_tuple q1, q2 = Q_(*qin1), Q_(*qin2) q1_cp = copy.copy(q1) if expected == 'error': self.assertRaises(OffsetUnitCalculusError, op.isub, q1_cp, q2) else: expected = np.array([expected[0]]*2, dtype=np.float), expected[1] self.assertEqual(op.isub(q1_cp, q2).units, Q_(*expected).units) q1_cp = copy.copy(q1) self.assertQuantityAlmostEqual(op.isub(q1_cp, q2), Q_(*expected), atol=0.01)
def compareHighestCards(self, self_cards, other_cards): self_set = sorted(Set([card.num_value for card in self_cards])) other_set = sorted(Set([card.num_value for card in other_cards])) index = len(self_set) - 1 while index >= 0: if self_set[index] == other_set[index]: index = operator.isub(index, 1) else: return 1 if self_set[index] > other_set[index] else -1 return 0
def isub_usecase(x, y): return operator.isub(x, y)
def __isub__(self,other): self._check_type(other) return Unit(operator.isub(self.value,other.value), self.unit)
print(li) operator.delitem(li, slice(1, 4)) print(li) print(operator.getitem(li, slice(0, 2))) s1 = "testing " s2 = "operator" print(operator.concat(s1, s2)) if (operator.contains(s1, s2)): print("Contains") else: print("It doesn't") a = 1 b = 0 print(operator.and_(a, b)) print(operator.or_(a, b)) print(operator.invert(a)) x = 10 y = 5 print(operator.iadd(x, y)) print(operator.isub(x, y)) print(operator.iconcat(s1, s2)) print(operator.imul(x, y)) print(operator.itruediv(x, y)) print(operator.imod(x, y)) print(operator.ixor(x, y)) print(operator.ipow(x, y)) print(operator.iand(x, y)) print(operator.ior(x, y)) print(operator.ilshift(x, y)) print(operator.irshift(x, y))
if math.isnan(math.nan): print("True : Not a number") else: print("its a number") if math.isinf(math.inf): print("True: infinite number") else: print("not infinite number") #------- Random related functions mylist = ['purushotham', 'chandra', 'ravi'] print(random.randint(1, 5)) print(random.random()) print(random.choice(mylist)) random.shuffle(mylist) #--- Number of seconds from EPOCH time i.e from Jan1st 1970 print(time.time()) print(date.fromtimestamp(1565335861.4913108)) #------------- operator(Inplace) related functions -------------------- print(operator.iadd(2, 3)) print(operator.isub(3, 2)) print(operator.imul(3, 2)) print(operator.itruediv(5, 3)) print(operator.imod(5, 3)) print(operator.iconcat('Purushotham', 'Reddy'))
def __isub__(self, other): return operator.isub(self._wrapped(), other)
a = 0 b = operator.iadd(a,2) print a print b #将与自身序列相加的结果赋给自身 同 += #但是不改变自身的值,返回值返回相加的结果 a = [1,2] b = [3,4] c = operator.iconcat(a,b) print c #将与自身的值相减之和的值赋给自身 同 -= #但是不改变自身的值,返回值返回相减的结果 a = 2 b = operator.isub(a,1) print a print b #将与自身的值相乘之和的值赋给自身 同 *= #但是不改变自身的值,返回值返回相乘的结果 a = 4 b = operator.imul(a,5) print a print b #将与自身的值相除之和的值赋给自身 同 /= #这个除法是精确除法,不是取整 #但是不改变自身的值,返回值返回相除的结果 a = 9 b = operator.itruediv(a,2)
import operator x = 10 y = 20 x = operator.iadd(x, y) print("The value after adding and assigning : ", end="") print(x) x = "Geeks" y = "ForGeeks" x = operator.iconcat(x, y) print("The string after concatenation : ", end="") print(x) x = operator.isub(2, 3) print("The value after subtracting and assigning : ", end="") print(x) x = operator.imul(10, 100) print("The value after multiplying and assigning : ", end="") print(x) x = operator.itruediv(100, 20) print("The value after dividing and assigning : ", end="") print(x) x = operator.imod(10, 6) print("The value after modulus and assigning : ", end="") print(x)
def test_inplace(self): class C(object): def __iadd__(self, other): return "iadd" def __iand__(self, other): return "iand" def __ifloordiv__(self, other): return "ifloordiv" def __ilshift__(self, other): return "ilshift" def __imod__(self, other): return "imod" def __imul__(self, other): return "imul" def __ior__(self, other): return "ior" def __ipow__(self, other): return "ipow" def __irshift__(self, other): return "irshift" def __isub__(self, other): return "isub" def __itruediv__(self, other): return "itruediv" def __ixor__(self, other): return "ixor" def __getitem__(self, other): return 5 # so that C is a sequence c = C() self.assertEqual(operator.iadd(c, 5), "iadd") self.assertEqual(operator.iand(c, 5), "iand") self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") self.assertEqual(operator.ilshift(c, 5), "ilshift") self.assertEqual(operator.imod(c, 5), "imod") self.assertEqual(operator.imul(c, 5), "imul") self.assertEqual(operator.ior(c, 5), "ior") self.assertEqual(operator.ipow(c, 5), "ipow") self.assertEqual(operator.irshift(c, 5), "irshift") self.assertEqual(operator.isub(c, 5), "isub") self.assertEqual(operator.itruediv(c, 5), "itruediv") self.assertEqual(operator.ixor(c, 5), "ixor") self.assertEqual(operator.iconcat(c, c), "iadd") self.assertEqual(operator.__iadd__(c, 5), "iadd") self.assertEqual(operator.__iand__(c, 5), "iand") self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv") self.assertEqual(operator.__ilshift__(c, 5), "ilshift") self.assertEqual(operator.__imod__(c, 5), "imod") self.assertEqual(operator.__imul__(c, 5), "imul") self.assertEqual(operator.__ior__(c, 5), "ior") self.assertEqual(operator.__ipow__(c, 5), "ipow") self.assertEqual(operator.__irshift__(c, 5), "irshift") self.assertEqual(operator.__isub__(c, 5), "isub") self.assertEqual(operator.__itruediv__(c, 5), "itruediv") self.assertEqual(operator.__ixor__(c, 5), "ixor") self.assertEqual(operator.__iconcat__(c, c), "iadd")