示例#1
0
 def operator_imul(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
         b[i] = nb_types.int32(size-i-1)
     operator.imul(a, b)
     return a
示例#2
0
    def test_imul(self):
        import operator

        class X(object):
            def __index__(self):
                return 5

        a = list(range(3))
        raises(TypeError, operator.imul)
        raises(TypeError, operator.imul, a, None)
        raises(TypeError, operator.imul, a, [])
        assert operator.imul(a, 2) is a
        assert a == [0, 1, 2, 0, 1, 2]
        assert operator.imul(a, 1) is a
        assert a == [0, 1, 2, 0, 1, 2]
示例#3
0
    def test_imul(self):
        import operator

        class X(object):
            def __index__(self):
                return 5

        a = list(range(3))
        raises(TypeError, operator.imul)
        raises(TypeError, operator.imul, a, None)
        raises(TypeError, operator.imul, a, [])
        assert operator.imul(a, 2) is a
        assert a == [0, 1, 2, 0, 1, 2]
        assert operator.imul(a, 1) is a
        assert a == [0, 1, 2, 0, 1, 2]
示例#4
0
 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")
示例#5
0
def main():

    x,y = 5,6
    a,b = 7,8

    # Incremental Add : x += y
    iAdd = operator.iadd(5, 6)
    print("iAdd: ", iAdd)

    # Incremental Concatenate
    iConCat = operator.iconcat("Hello", "World")
    print("iConCat:", iConCat)

    # Incremental Subtraction
    iSub = operator.isub(5, 6)
    print("iSub: ", iSub)

    # Incremental Multiplication
    iMul = operator.imul(5,6)
    print("iMul:", iMul)

    # Incremental Division
    iDiv = operator.itruediv(10, 5)
    print("iDiv: ", iDiv)

    # Incremental Modulus
    iMod = operator.imod(10, 6)
    print("iMod: ", iMod)

    # Incremental Exponential
    iPow = operator.ipow(2, 4)
    print("iPow: ", iPow)
示例#6
0
def validateKey(number):
    ecx = number
    imuled = operator.imul(ecx, magicNumber)

    edx = int(hex(imuled)[:-8], 16)

    edx >>= 0x7
    eax = ecx

    eax >>= 0x1f
    edx -= eax
    eax = edx

    eax = operator.imul(eax, secondMagicNumber)
    ecx -= eax
    eax = ecx

    return eax
示例#7
0
 def test_inplace_multiplication_with_autoconvert(self, input_tuple, expected):
     self.ureg.autoconvert_offset_to_baseunit = True
     (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.imul, q1_cp, q2)
     else:
         expected = np.array([expected[0]]*2, dtype=np.float), expected[1]
         self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units)
         q1_cp = copy.copy(q1)
         self.assertQuantityAlmostEqual(op.imul(q1_cp, q2), Q_(*expected),
                                        atol=0.01)
示例#8
0
    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")
示例#9
0
    def test_inplace(self):
        import operator

        list = []
        assert operator.iadd(list, [1, 2]) is list
        assert list == [1, 2]

        list = [1, 2]
        assert operator.imul(list, 2) is list
        assert list == [1, 2, 1, 2]
示例#10
0
 def test_inplace_multiplication(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.imul, q1_cp, q2)
     else:
         expected = np.array([expected[0]] * 2, dtype=np.float), expected[1]
         self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units)
         q1_cp = copy.copy(q1)
         self.assertQuantityAlmostEqual(op.imul(q1_cp, q2),
                                        Q_(*expected),
                                        atol=0.01)
示例#11
0
    def test_inplace(self):
        import operator

        list = []
        assert operator.iadd(list, [1, 2]) is list
        assert list == [1, 2]

        list = [1, 2]
        assert operator.imul(list, 2) is list
        assert list == [1, 2, 1, 2]
示例#12
0
def hash(key):
    result = 0
    prev_result = 0
    for i in range(len(key)):
        current_char = ord(key[i])
        result = operator.imul(prev_result, 0x24)
        result += prev_result
        result += current_char
        prev_result = result
        prev_result = prev_result & 0xffffffff  #keep it in 32bit range

    return prev_result
    def test_class_binary_inplace_operators(self):
        class WithLotsOfOperators(Class):
            def __iadd__(self, other):
                return (self, "iadd", other)

            def __isub__(self, other):
                return (self, "isub", other)

            def __imul__(self, other):
                return (self, "imul", other)

            def __imod__(self, other):
                return (self, "imod", other)

            def __itruediv__(self, other):
                return (self, "itruediv", other)

            def __ifloordiv__(self, other):
                return (self, "ifloordiv", other)

            def __ilshift__(self, other):
                return (self, "ilshift", other)

            def __irshift__(self, other):
                return (self, "irshift", other)

            def __ior__(self, other):
                return (self, "ior", other)

            def __iand__(self, other):
                return (self, "iand", other)

            def __ixor__(self, other):
                return (self, "ixor", other)

            def __imatmul__(self, other):
                return (self, "imatmul", other)

        c = WithLotsOfOperators()

        self.assertEqual(operator.iadd(c, 0), (c, "iadd", 0))
        self.assertEqual(operator.isub(c, 0), (c, "isub", 0))
        self.assertEqual(operator.imul(c, 0), (c, "imul", 0))
        self.assertEqual(operator.imod(c, 0), (c, "imod", 0))
        self.assertEqual(operator.itruediv(c, 0), (c, "itruediv", 0))
        self.assertEqual(operator.ifloordiv(c, 0), (c, "ifloordiv", 0))
        self.assertEqual(operator.ilshift(c, 0), (c, "ilshift", 0))
        self.assertEqual(operator.irshift(c, 0), (c, "irshift", 0))
        self.assertEqual(operator.ior(c, 0), (c, "ior", 0))
        self.assertEqual(operator.iand(c, 0), (c, "iand", 0))
        self.assertEqual(operator.ixor(c, 0), (c, "ixor", 0))
        self.assertEqual(operator.imatmul(c, 0), (c, "imatmul", 0))
示例#14
0
def problem_five():
	lcm = 2520
	lcm_li = count_divisors(lcm)
	lcm_dict = convert_list_to_dict(lcm_li)
	
	for i in range(11, 21):
		li = count_divisors(i)
		li_dict = convert_list_to_dict(li)
		for item in li_dict:
			if item in lcm_dict:
				lcm_dict[item] = max(li_dict[item], lcm_dict[item])
			else:
				lcm_dict[item] = li_dict[item]
	ans = 1
	for item in lcm_dict:
		ans = imul(ans, pow(item, lcm_dict[item]))
	print ans
示例#15
0
def problem_five():
    lcm = 2520
    lcm_li = count_divisors(lcm)
    lcm_dict = convert_list_to_dict(lcm_li)

    for i in range(11, 21):
        li = count_divisors(i)
        li_dict = convert_list_to_dict(li)
        for item in li_dict:
            if item in lcm_dict:
                lcm_dict[item] = max(li_dict[item], lcm_dict[item])
            else:
                lcm_dict[item] = li_dict[item]
    ans = 1
    for item in lcm_dict:
        ans = imul(ans, pow(item, lcm_dict[item]))
    print ans
示例#16
0
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)
print a
print b

#将与自身的值相与的值赋给自身 同 &=
#但是不改变自身的值,返回值返回相与的结果
a = 8
b = operator.iand(a, 1)
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'))
示例#18
0
b = 'Kohli'
print(operator.iconcat(a, b))

"""3. isub() :- This function is used to assign and subtract the current value. This operation does “a-=b” operation. Assigning is not performed in case of immutable
containers, such as strings, numbers and tuples."""

a = 19
b = 7
print(operator.isub(a, b))

"""4.imul() :- This function is used to assign and multiply the current value. This operation does “a*=b” operation. Assigning is not performed in case of immutable
containers, such as strings, numbers and tuples."""

a = 16
b = 6
print(operator.imul(a, b))

"""5.itruediv() :- This function is used to assign and divide the current value. This operation does “a/=b” operation. Assigning is not performed in case of immutable
containers, such as strings, numbers and tuples."""

a = 64
b = 8
print(operator.itruediv(a, b))

"""6.imod()- This function is used to assign and return remainder. This operation does “a%=b” operation. Assigning is not performed in case of immutable containers,
such as strings, numbers and tuples."""

a = 3
b = 2
print(operator.imod(a, b))
示例#19
0
 def __imul__(self, other):
     return operator.imul(self._wrapped(), other)
示例#20
0
#
x = operator.iadd(2, 3)
print("the value after adding and assigning : ", end="")
print(x)

y = "geeks"
z = "forgeeks"
y = operator.iconcat(y, z)
print("the string after concatenation is : ", end="")
print(y)

x = operator.isub(2, 3)
print("the value after subtracting and assiging : ", end="")
print(x)

x = operator.imul(2, 3)
print("the value after multiplying and assiging : ", end="")
print(x)

x = operator.itruediv(2, 3)
print("the value after dividing and assign : ", end="")
print(x)

x = operator.ixor(10, 5)
print("the value after xoring and assigning : ", end="")
print(x)

x = operator.ipow(5, 4)
print("the value after exponentiating and assigning : ", end="")
print(x)
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))
示例#22
0
 def update_event(self, inp=-1):
     self.set_output_val(0, operator.imul(self.input(0), self.input(1)))
示例#23
0
 def imul_usecase(x, y):
     return operator.imul(x, y)
示例#24
0
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)
print a
print b

#将与自身的值相与的值赋给自身 同 &=
#但是不改变自身的值,返回值返回相与的结果
a = 8
b = operator.iand(a,1)
示例#25
0
 def imul_usecase(x, y):
     return operator.imul(x, y)
示例#26
0
    - x += y is equivalent to x = operator.iadd(x, y) 
    - Assigning is not performed in case of immutable containers, 
      such as strings, numbers and tuples.'''
      
import operator
# using iadd() to add and assign value 
x = 2;y = 3
x = operator.iadd(x,y)
print("Addition",x)

x = 2;y = 3
x = operator.isub(x,y)
print("Subtraction", x)

x = 2;y = 3
x = operator.imul(x,y)
print("Multiply",x)

x = 10;y = 5
x = operator.itruediv(x,y)
print("Divide",x)

x = 10; y = 6
x = operator.imod(x,y)
print("Mod",x)


# initializing another values
y = 'geeks'
z = 'forgeeks'
# using iconcat() to concat the sequences
示例#27
0
			def interp(ins):
				nonlocal stack, block_stack, ip, env

				if ins.opname == 'LOAD_CONST': push(ins.argval)
				elif ins.opname == 'LOAD_NAME': push(env[ins.argval]) # TODO: use locals or global
				elif ins.opname == 'STORE_NAME': env[ins.argval] = pop()
				elif ins.opname == 'DELETE_NAME': del env[ins.argval]
				elif ins.opname == 'LOAD_GLOBAL': push(env[ins.argval]) # TODO: global env
				elif ins.opname == 'LOAD_FAST': push(localenv[ins.argval])
				elif ins.opname == 'STORE_FAST': localenv[ins.argval] = pop()
				elif ins.opname == 'LOAD_ATTR': push(getattr(pop(), ins.argval))
				elif ins.opname == 'STORE_ATTR': setattr(pop(), ins.argval, pop())
				elif ins.opname == 'CALL_FUNCTION':
					# TODO: handle more than just positional arguments
					argc = ins.argval
					positional = argc & 0xFF
					args = [pop() for _ in range(positional)]
					args.reverse()
					log("args:", args)
					f = pop()
					push(f(*args))
				elif ins.opname == 'MAKE_FUNCTION':
					argc = ins.argval
					positional = argc & 0xFF
					name = pop()
					code = pop()
					default_args = [pop() for _ in range(positional)]
					log("make function:", name, positional, code)
					push(Function(name, positional, code, interpret))
				elif ins.opname == 'POP_TOP': pop()
				elif ins.opname == 'DUP_TOP': push(stack[-1])
				elif ins.opname == 'RETURN_VALUE': raise Return(pop())
				elif ins.opname == 'COMPARE_OP':
					opname = ins.argrepr
					rhs = pop()
					lhs = pop()
					push({'<': operator.lt, '>': operator.gt,
						  '==': operator.eq, '!=': operator.ne,
						  '<=': operator.le, '>=': operator.ge}[opname](lhs, rhs))
				elif ins.opname == 'UNARY_NOT': push(not pop())
				elif ins.opname == 'INPLACE_MULTIPLY': rhs = pop(); push(operator.imul(pop(), rhs))
				elif ins.opname == 'INPLACE_SUBTRACT': rhs = pop(); push(operator.isub(pop(), rhs))
				elif ins.opname == 'INPLACE_ADD': rhs = pop(); push(operator.iadd(pop(), rhs))
				elif ins.opname == 'BINARY_ADD': push(pop() + pop())
				elif ins.opname == 'BINARY_SUBTRACT': rhs = pop(); push(pop() - rhs)
				elif ins.opname == 'BINARY_MULTIPLY': rhs = pop(); push(pop() * rhs)
				elif ins.opname == 'BINARY_MODULO': rhs = pop(); push(pop() % rhs)
				elif ins.opname == 'BINARY_TRUE_DIVIDE': rhs = pop(); push(pop() / rhs)
				elif ins.opname == 'BINARY_OR': rhs = pop(); push(pop() or rhs)
				elif ins.opname == 'BINARY_SUBSCR': i = pop(); push(pop()[i])
				elif ins.opname == 'STORE_SUBSCR': i = pop(); lhs = pop(); lhs[i] = pop()
				elif ins.opname == 'STORE_MAP': k = pop(); v = pop(); stack[-1][k] = v
				elif ins.opname == 'UNPACK_SEQUENCE': stack.extend([x for x in reversed(pop())])
				elif ins.opname == 'LIST_APPEND': v = pop(); stack[-ins.argval].append(v)
				elif ins.opname == 'MAP_ADD': k = pop(); d = stack[-ins.argval-1]; d[k] = pop()
				elif ins.opname == 'BUILD_MAP': push({})
				elif ins.opname == 'BUILD_TUPLE':
					push(tuple(reversed([pop() for _ in range(ins.argval)])))
				elif ins.opname == 'BUILD_LIST':
					push(list(reversed([pop() for _ in range(ins.argval)])))
				elif ins.opname == 'BUILD_SLICE':
					argc = ins.argval
					if argc == 2: # x[i:]
						i = pop(); push(slice(pop(), i))
					elif argc == 3: # x[i:j]
						j = pop(); i = pop(); push(slice(pop(), i, j))
				elif ins.opname == 'SETUP_LOOP':
					# (start, end) indices
					block_stack.append((ip, indices[ins.argval]))
				elif ins.opname == 'POP_BLOCK': block_stack.pop()
				elif ins.opname == 'JUMP_ABSOLUTE':
					log("jmp to {0} ({1})".format(ins.argval, indices[ins.argval]))
					ip = indices[ins.argval]
				elif ins.opname == 'JUMP_FORWARD':
					log("jmp forward to {0} ({1})".format(ins.argval, indices[ins.argval]))
					ip = indices[ins.argval]
				elif ins.opname == 'POP_JUMP_IF_FALSE':
					log("jmpf to {0} ({1})".format(ins.argval, indices[ins.argval]))
					if not pop(): ip = indices[ins.argval]
				elif ins.opname == 'POP_JUMP_IF_TRUE':
					log("jmpt to {0} ({1})".format(ins.argval, indices[ins.argval]))
					if pop(): ip = indices[ins.argval]
				elif ins.opname == 'GET_ITER': push(iter(pop()))
				elif ins.opname == 'FOR_ITER':
					iterator = stack[-1]
					try: push(next(iterator))
					except StopIteration:
						pop()
						ip = indices[ins.argval]
				else:
					raise NotImplementedError("instruction: " + repr(ins))
示例#28
0
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)