def goxlag(self, irc, msg, args, optlist): """[--raw] Retrieve mtgox order processing lag. If --raw option is specified only output the raw number of seconds. Otherwise, dress it up.""" try: json_data = urlopen( "https://mtgox.com/api/2/money/order/lag").read() lag = json.loads(json_data) lag_secs = lag['data']['lag_secs'] except: irc.error("Problem retrieving gox lag. Try again later.") return if dict(optlist).has_key('raw'): irc.reply("%s" % (lag_secs, )) return result = "MtGox lag is %s seconds." % (lag_secs, ) au = lag_secs / 499.004784 meandistance = { 0: "... nowhere, really", 0.0001339: "to the other side of the Earth, along the surface", 0.0024: "across the outer diameter of Saturn's rings", 0.00257: "from Earth to Moon", 0.002819: "from Jupiter to its third largest moon, Io", 0.007155: "from Jupiter to its largest moon, Ganymede", 0.00802: "from Saturn to its largest moon, Titan", 0.012567: "from Jupiter to its second largest moon, Callisto", 0.016: "one full loop along the orbit of the Moon around Earth", 0.0257: 'ten times between Earth and Moon', 0.0689: "approximately the distance covered by Voyager 1 in one week", 0.0802: "ten times between Saturn and Titan", 0.12567: "ten times between Jupiter and Callisto", 0.2540: 'between Earth and Venus at their closest approach', 0.257: 'one hundred times between Earth and Moon', 0.2988: 'approximately the distance covered by Voyager 1 in one month', 0.39: 'from the Sun to Mercury', 0.72: 'from the Sun to Venus', 1: 'from the Sun to Earth', 1.52: 'from the Sun to Mars', 2.77: 'from the Sun to Ceres (in the main asteroid belt)', 5.2: 'from the Sun to Jupiter', 9.54: 'from the Sun to Saturn', 19.18: 'from the Sun to Uranus', 30.06: 'from the Sun to Neptune', 39.44: 'from the Sun to Pluto (Kuiper belt)', 100: 'from the Sun to heliopause (out of the solar system!)' } import operator distances = meandistance.keys() diffs = map(lambda x: abs(operator.__sub__(x, au)), distances) bestdist = distances[diffs.index(min(diffs))] objectname = meandistance[bestdist] result += " During this time, light travels %s AU. You could have sent a bitcoin %s (%s AU)." % ( au, objectname, bestdist) irc.reply(result)
def goxlag(self, irc, msg, args, optlist): """[--raw] Retrieve mtgox order processing lag. If --raw option is specified only output the raw number of seconds. Otherwise, dress it up.""" try: json_data = urlopen("https://mtgox.com/api/2/money/order/lag").read() lag = json.loads(json_data) lag_secs = lag['data']['lag_secs'] except: irc.error("Problem retrieving gox lag. Try again later.") return if dict(optlist).has_key('raw'): irc.reply("%s" % (lag_secs,)) return result = "MtGox lag is %s seconds." % (lag_secs,) au = lag_secs / 499.004784 meandistance = {0: "... nowhere, really", 0.0001339: "to the other side of the Earth, along the surface", 0.0024: "across the outer diameter of Saturn's rings", 0.00257: "from Earth to Moon", 0.002819: "from Jupiter to its third largest moon, Io", 0.007155: "from Jupiter to its largest moon, Ganymede", 0.00802: "from Saturn to its largest moon, Titan", 0.012567: "from Jupiter to its second largest moon, Callisto", 0.016: "one full loop along the orbit of the Moon around Earth", 0.0257: 'ten times between Earth and Moon', 0.0689: "approximately the distance covered by Voyager 1 in one week", 0.0802: "ten times between Saturn and Titan", 0.12567: "ten times between Jupiter and Callisto", 0.2540: 'between Earth and Venus at their closest approach', 0.257: 'one hundred times between Earth and Moon', 0.2988: 'approximately the distance covered by Voyager 1 in one month', 0.39: 'from the Sun to Mercury', 0.72: 'from the Sun to Venus', 1: 'from the Sun to Earth', 1.52: 'from the Sun to Mars', 2.77: 'from the Sun to Ceres (in the main asteroid belt)', 5.2: 'from the Sun to Jupiter', 9.54: 'from the Sun to Saturn', 19.18: 'from the Sun to Uranus', 30.06: 'from the Sun to Neptune', 39.44: 'from the Sun to Pluto (Kuiper belt)', 100: 'from the Sun to heliopause (out of the solar system!)'} import operator distances = meandistance.keys() diffs = map(lambda x: abs(operator.__sub__(x, au)), distances) bestdist = distances[diffs.index(min(diffs))] objectname = meandistance[bestdist] result += " During this time, light travels %s AU. You could have sent a bitcoin %s (%s AU)." % (au, objectname, bestdist) irc.reply(result)
def test_sanity(): ''' Performs a set of simple sanity checks on most operators. ''' #__abs__ AreEqual(operator.__abs__(0), 0) AreEqual(operator.__abs__(1), 1) AreEqual(operator.__abs__(-1), 1) AreEqual(operator.__abs__(0.0), 0.0) AreEqual(operator.__abs__(1.1), 1.1) AreEqual(operator.__abs__(-1.1), 1.1) AreEqual(operator.__abs__(0L), 0L) AreEqual(operator.__abs__(1L), 1L) AreEqual(operator.__abs__(-1L), 1L) #__neg__ AreEqual(operator.__neg__(0), 0) AreEqual(operator.__neg__(1), -1) AreEqual(operator.__neg__(-1), 1) AreEqual(operator.__neg__(0.0), 0.0) AreEqual(operator.__neg__(1.1), -1.1) AreEqual(operator.__neg__(-1.1), 1.1) AreEqual(operator.__neg__(0L), 0L) AreEqual(operator.__neg__(1L), -1L) AreEqual(operator.__neg__(-1L), 1L) #__pos__ AreEqual(operator.__pos__(0), 0) AreEqual(operator.__pos__(1), 1) AreEqual(operator.__pos__(-1), -1) AreEqual(operator.__pos__(0.0), 0.0) AreEqual(operator.__pos__(1.1), 1.1) AreEqual(operator.__pos__(-1.1), -1.1) AreEqual(operator.__pos__(0L), 0L) AreEqual(operator.__pos__(1L), 1L) AreEqual(operator.__pos__(-1L), -1L) #__add__ AreEqual(operator.__add__(0, 0), 0) AreEqual(operator.__add__(1, 2), 3) AreEqual(operator.__add__(-1, 2), 1) AreEqual(operator.__add__(0.0, 0.0), 0.0) AreEqual(operator.__add__(1.1, 2.1), 3.2) AreEqual(operator.__add__(-1.1, 2.1), 1.0) AreEqual(operator.__add__(0L, 0L), 0L) AreEqual(operator.__add__(1L, 2L), 3L) AreEqual(operator.__add__(-1L, 2L), 1L) #__sub__ AreEqual(operator.__sub__(0, 0), 0) AreEqual(operator.__sub__(1, 2), -1) AreEqual(operator.__sub__(-1, 2), -3) AreEqual(operator.__sub__(0.0, 0.0), 0.0) AreEqual(operator.__sub__(1.1, 2.1), -1.0) AreEqual(operator.__sub__(-1.1, 2.1), -3.2) AreEqual(operator.__sub__(0L, 0L), 0L) AreEqual(operator.__sub__(1L, 2L), -1L) AreEqual(operator.__sub__(-1L, 2L), -3L) #__mul__ AreEqual(operator.__mul__(0, 0), 0) AreEqual(operator.__mul__(1, 2), 2) AreEqual(operator.__mul__(-1, 2), -2) AreEqual(operator.__mul__(0.0, 0.0), 0.0) AreEqual(operator.__mul__(2.0, 3.0), 6.0) AreEqual(operator.__mul__(-2.0, 3.0), -6.0) AreEqual(operator.__mul__(0L, 0L), 0L) AreEqual(operator.__mul__(1L, 2L), 2L) AreEqual(operator.__mul__(-1L, 2L), -2L) #__div__ AreEqual(operator.__div__(0, 1), 0) AreEqual(operator.__div__(4, 2), 2) AreEqual(operator.__div__(-1, 2), -1) AreEqual(operator.__div__(0.0, 1.0), 0.0) AreEqual(operator.__div__(4.0, 2.0), 2.0) AreEqual(operator.__div__(-4.0, 2.0), -2.0) AreEqual(operator.__div__(0L, 1L), 0L) AreEqual(operator.__div__(4L, 2L), 2L) AreEqual(operator.__div__(-4L, 2L), -2L) #__floordiv__ AreEqual(operator.__floordiv__(0, 1), 0) AreEqual(operator.__floordiv__(4, 2), 2) AreEqual(operator.__floordiv__(-1, 2), -1) AreEqual(operator.__floordiv__(0.0, 1.0), 0.0) AreEqual(operator.__floordiv__(4.0, 2.0), 2.0) AreEqual(operator.__floordiv__(-4.0, 2.0), -2.0) AreEqual(operator.__floordiv__(0L, 1L), 0L) AreEqual(operator.__floordiv__(4L, 2L), 2L) AreEqual(operator.__floordiv__(-4L, 2L), -2L) #__truediv__ AreEqual(operator.__truediv__(0, 1), 0) AreEqual(operator.__truediv__(4, 2), 2) AreEqual(operator.__truediv__(-1, 2), -0.5) AreEqual(operator.__truediv__(0.0, 1.0), 0.0) AreEqual(operator.__truediv__(4.0, 2.0), 2.0) AreEqual(operator.__truediv__(-1.0, 2.0), -0.5) AreEqual(operator.__truediv__(0L, 1L), 0L) AreEqual(operator.__truediv__(4L, 2L), 2L) AreEqual(operator.__truediv__(-4L, 2L), -2L) #__mod__ AreEqual(operator.__mod__(0, 1), 0) AreEqual(operator.__mod__(4, 2), 0) AreEqual(operator.__mod__(-1, 2), 1) AreEqual(operator.__mod__(0.0, 1.0), 0.0) AreEqual(operator.__mod__(4.0, 2.0), 0.0) AreEqual(operator.__mod__(-1.0, 2.0), 1.0) AreEqual(operator.__mod__(0L, 1L), 0L) AreEqual(operator.__mod__(4L, 2L), 0L) AreEqual(operator.__mod__(-4L, 2L), 0L) #__inv__ AreEqual(operator.__inv__(0), -1) AreEqual(operator.__inv__(1), -2) AreEqual(operator.__inv__(-1), 0) AreEqual(operator.__inv__(0L), -1L) AreEqual(operator.__inv__(1L), -2L) AreEqual(operator.__inv__(-1L), 0L) #__invert__ AreEqual(operator.__invert__(0), -1) AreEqual(operator.__invert__(1), -2) AreEqual(operator.__invert__(-1), 0) AreEqual(operator.__invert__(0L), -1L) AreEqual(operator.__invert__(1L), -2L) AreEqual(operator.__invert__(-1L), 0L) #__lshift__ AreEqual(operator.__lshift__(0, 1), 0) AreEqual(operator.__lshift__(1, 1), 2) AreEqual(operator.__lshift__(-1, 1), -2) AreEqual(operator.__lshift__(0L, 1), 0L) AreEqual(operator.__lshift__(1L, 1), 2L) AreEqual(operator.__lshift__(-1L, 1), -2L) #__rshift__ AreEqual(operator.__rshift__(1, 1), 0) AreEqual(operator.__rshift__(2, 1), 1) AreEqual(operator.__rshift__(-1, 1), -1) AreEqual(operator.__rshift__(1L, 1), 0L) AreEqual(operator.__rshift__(2L, 1), 1L) AreEqual(operator.__rshift__(-1L, 1), -1L) #__not__ AreEqual(operator.__not__(0), 1) AreEqual(operator.__not__(1), 0) AreEqual(operator.__not__(-1), 0) AreEqual(operator.__not__(0L), 1) AreEqual(operator.__not__(1L), 0) AreEqual(operator.__not__(-1L), 0) #__and__ AreEqual(operator.__and__(0, 0), 0) AreEqual(operator.__and__(1, 1), 1) AreEqual(operator.__and__(0, 1), 0) AreEqual(operator.__and__(1, 0), 0) #__xor__ AreEqual(operator.__xor__(0, 0), 0) AreEqual(operator.__xor__(1, 1), 0) AreEqual(operator.__xor__(0, 1), 1) AreEqual(operator.__xor__(1, 0), 1) #__or__ AreEqual(operator.__or__(0, 0), 0) AreEqual(operator.__or__(1, 1), 1) AreEqual(operator.__or__(0, 1), 1) AreEqual(operator.__or__(1, 0), 1) #__concat__ AreEqual(operator.__concat__([0], [1]), [0,1]) AreEqual(operator.__concat__([2], [1]), [2,1]) AreEqual(operator.__concat__([-1], [1]), [-1,1]) #__contains__ Assert(operator.__contains__("abc", "c")) Assert(not operator.__contains__("abc", "d")) Assert(operator.__contains__("abc", "")) Assert(not operator.__contains__("", "c")) Assert(operator.__contains__([1,2,3], 1)) Assert(not operator.__contains__([1,2,3], 4)) #__getitem__ AreEqual(operator.__getitem__("abc", 2), "c") AssertError(IndexError, operator.__getitem__, "abc", 3) AreEqual(operator.__getitem__([1,2,3], 2), 3) AssertError(IndexError, operator.__getitem__, [1,2,3], 3) #__setitem__ AssertError(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1,2,3] operator.__setitem__(t_list, 2, 4) AreEqual(t_list, [1,2,4]) AssertError(IndexError, operator.__setitem__, [1,2,3], 4, 9) #__delitem__ #UNIMPLEMENTED #AssertError(TypeError, operator.__delitem__, "abc", 2) t_list = [1,2,3] operator.__delitem__(t_list, 2) AreEqual(t_list, [1,2]) AssertError(IndexError, operator.__delitem__, [1,2,3], 4) #__repeat__ AreEqual(operator.__repeat__("abc", 2), "abcabc") AreEqual(operator.__repeat__("", 2), "") AreEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3]) #__getslice__ AreEqual(operator.__getslice__("abc", 1, 2), "b") AreEqual(operator.__getslice__("abc", 0, 3), "abc") AreEqual(operator.__getslice__("", 0, 0), "") AreEqual(operator.__getslice__([1,2,3], 1, 2), [2]) AreEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3]) AreEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1,2,3] operator.__delslice__(t_list, 1, 2) AreEqual(t_list, [1,3]) t_list = [1,2,3] operator.__delslice__(t_list, 0, 3) AreEqual(t_list, []) t_list = [1,2,3] operator.__delslice__(t_list, 0, 0) AreEqual(t_list, [1,2,3]) #__setslice__ t_list = [1,2,3] operator.__setslice__(t_list, 1, 2, [9]) AreEqual(t_list, [1,9,3]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 3, [9, 8]) AreEqual(t_list, [9, 8]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 0, [9]) AreEqual(t_list, [9,1, 2,3])
def eval(self, env): return operator.__sub__(self.left.eval(env), self.right.eval(env))
def compute_corner_response(img): ## a) apply Sobel filters # Sobel filtering function is implemented in filtering_by_yoseob.py # sobel_img_x, sobel_img_y is derivatives along x and y direction respectively. sobel_img_x, sobel_img_y = my_sobel_filtering(img) # padded with 0 value to operate corner response sobel_img_x = image_padding_2d(sobel_img_x, 2, 1) # (img, padd_width, type=1) sobel_img_y = image_padding_2d(sobel_img_y, 2, 1) # type=1 means just padd with zeros. ## b) compute second moment matrix M uni_window = np.ones((5, 5)) patch_img_x = np.zeros((5, 5)) patch_img_y = np.zeros((5, 5)) size0 = img.shape[0] size1 = img.shape[1] ## c) variables for computing corner responses R = np.zeros((size0, size1)) _k = 0.04 _max_val = 0 elapsed_ = list(range(0, size0, size0 // 20)) for x in range(size0): for y in range(size1): # i. subtract mean of each image patch _sum_x = 0. _sum_y = 0. for i in range(x, x + 5): for j in range(y, y + 5): patch_img_x[i - x][j - y] = sobel_img_x[i][j] _sum_x = operator.__add__(_sum_x, sobel_img_x[i][j]) patch_img_y[i - x][j - y] = sobel_img_y[i][j] _sum_y = operator.__add__(_sum_y, sobel_img_y[i][j]) _avg_x = operator.__truediv__(_sum_x, 25) _avg_y = operator.__truediv__(_sum_y, 25) sum_of_ix_ix = 0. sum_of_ix_iy = 0. sum_of_iy_iy = 0. for i in range(5): for j in range(5): patch_img_x[i][j] = operator.__sub__( patch_img_x[i][j], _avg_x) patch_img_y[i][j] = operator.__sub__( patch_img_y[i][j], _avg_y) sum_of_ix_ix = operator.__add__( sum_of_ix_ix, operator.__mul__(patch_img_x[i][j], patch_img_x[i][j])) sum_of_ix_iy = operator.__add__( sum_of_ix_iy, operator.__mul__(patch_img_x[i][j], patch_img_y[i][j])) sum_of_iy_iy = operator.__add__( sum_of_iy_iy, operator.__mul__(patch_img_y[i][j], patch_img_y[i][j])) # ii. get second moment matrix # since we use uniform window, just calculated the summation of ix_ix, ix_iy, iy_iy respectively (above). M = np.array([[sum_of_ix_ix, sum_of_ix_iy], [sum_of_ix_iy, sum_of_iy_iy]]) eigenvalues, _ = np.linalg.eig(M) #print(eigen_values) e1 = eigenvalues[0] e2 = eigenvalues[1] R[x][y] = e1 * e2 - _k * ((e1 + e2)**2) ## d) normalize responses # i. negative values to 0, otherwise normalize to range [0, 1] if R[x][y] < 0: R[x][y] = 0 if R[x][y] > _max_val: _max_val = R[x][y] if x in elapsed_: print('.', end='') #print("_max_val:", _max_val) #normalizer = 1. #if _max_val != 0: # normalizer = 1 / _max_val #normalizer = 1 / 255 #normalizer = 1 / np.linalg.norm(R) for x in range(size0): for y in range(size1): R[x][y] = operator.__truediv__(R[x][y], _max_val) return R
def __sub__(self, value): return operator.__sub__(self._tensor, value)
def __sub__(self, other): with self._lock: return operator.__sub__(self.__wrapped__, other)
def __sub__(x, y): if isinstance(x, callproxy): x = x.p_obj if isinstance(y, callproxy): y = y.p_obj return operator.__sub__(x, y)
def __rsub__(self, other: tp.Any) -> tp.Any: operator = lambda rhs, lhs: operator_mod.__sub__(lhs, rhs) operator.__name__ = 'r' + operator_mod.__sub__.__name__ return self._ufunc_binary_operator(operator=operator, other=other)
self.premut_lists[d][2][b][0].append(self.stim_array[d][5][1][s][e]); self.premut_lists[d][2][b][1].append(self.stim_array[d][5][1][s][e]); else: self.premut_lists[d][2][b][0].append(self.stim_array[d][5][1][s][e]); self.premut_lists[d][2][b][1].append(self.stim_array[d][5][1][s][e]); #shuffling and selecting permutation with score difference>1 for distance comparison task for k in range (b):#shuffling self.premut_lists[d][1][b] a different num of time for each block to creates diversity between blocks shuffle(self.premut_lists[d][1][b]); self.premut_temp=list(permutations(self.premut_lists[d][1][b],2)); self.premut_lists[d][1][b]=[]; shuffle(self.premut_temp); shuffle(self.premut_temp); shuffle(self.premut_temp); for p in range(len(self.premut_temp)): r0=int(self.premut_temp[p][0])+1;#row in stimuli.xls of the element stored in temp_list[p][0] = the index number (the element itself)+1; r1=int(self.premut_temp[p][1])+1; s_d=int(math.fabs(operator.__sub__(int(xsheet.cell_value(r0,5)),int(xsheet.cell_value(r1,5)))));#sufficient distance if s_d > 1:#checking if the difference in scores of the pair>1 self.premut_lists[d][1][b].append(self.premut_temp[p]); #appending lexical control in all domains. if b in range(2): l_d=int(math.fabs(operator.__sub__(len(xsheet.cell_value(r0,1)),len(xsheet.cell_value(r1,1)))));#len distance if l_d>2: self.premut_lists[d][3][0].append(self.premut_temp[p]); self.premut_lists[d][3][1].append(self.premut_temp[p]); shuffle( self.premut_lists[d][3][1]);shuffle( self.premut_lists[d][3][1]); shuffle( self.premut_lists[d][3][0]);shuffle( self.premut_lists[d][3][0]);shuffle( self.premut_lists[d][3][0]); #if d==2: print self.premut_lists[d][3][0]; #if d==2: print b, self.premut_lists[d][1][b]; #creating the semantic permutations: if b in range(2): for i in range(len(self.premut_lists[d][2][b][0])-1):#len(self.premut_lists[d][2][b][0])=len(self.premut_lists[d][2][b][1])
def test_sanity(self): """Performs a set of simple sanity checks on most operators.""" #__abs__ self.assertEqual(operator.__abs__(0), 0) self.assertEqual(operator.__abs__(1), 1) self.assertEqual(operator.__abs__(-1), 1) self.assertEqual(operator.__abs__(0.0), 0.0) self.assertEqual(operator.__abs__(1.1), 1.1) self.assertEqual(operator.__abs__(-1.1), 1.1) self.assertEqual(operator.__abs__(long(0)), long(0)) self.assertEqual(operator.__abs__(long(1)), long(1)) self.assertEqual(operator.__abs__(-long(1)), long(1)) #__neg__ self.assertEqual(operator.__neg__(0), 0) self.assertEqual(operator.__neg__(1), -1) self.assertEqual(operator.__neg__(-1), 1) self.assertEqual(operator.__neg__(0.0), 0.0) self.assertEqual(operator.__neg__(1.1), -1.1) self.assertEqual(operator.__neg__(-1.1), 1.1) self.assertEqual(operator.__neg__(long(0)), long(0)) self.assertEqual(operator.__neg__(long(1)), -long(1)) self.assertEqual(operator.__neg__(-long(1)), long(1)) #__pos__ self.assertEqual(operator.__pos__(0), 0) self.assertEqual(operator.__pos__(1), 1) self.assertEqual(operator.__pos__(-1), -1) self.assertEqual(operator.__pos__(0.0), 0.0) self.assertEqual(operator.__pos__(1.1), 1.1) self.assertEqual(operator.__pos__(-1.1), -1.1) self.assertEqual(operator.__pos__(long(0)), long(0)) self.assertEqual(operator.__pos__(long(1)), long(1)) self.assertEqual(operator.__pos__(-long(1)), -long(1)) #__add__ self.assertEqual(operator.__add__(0, 0), 0) self.assertEqual(operator.__add__(1, 2), 3) self.assertEqual(operator.__add__(-1, 2), 1) self.assertEqual(operator.__add__(0.0, 0.0), 0.0) self.assertEqual(operator.__add__(1.1, 2.1), 3.2) self.assertEqual(operator.__add__(-1.1, 2.1), 1.0) self.assertEqual(operator.__add__(long(0), long(0)), long(0)) self.assertEqual(operator.__add__(long(1), long(2)), long(3)) self.assertEqual(operator.__add__(-long(1), long(2)), long(1)) #__sub__ self.assertEqual(operator.__sub__(0, 0), 0) self.assertEqual(operator.__sub__(1, 2), -1) self.assertEqual(operator.__sub__(-1, 2), -3) self.assertEqual(operator.__sub__(0.0, 0.0), 0.0) self.assertEqual(operator.__sub__(1.1, 2.1), -1.0) self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2) self.assertEqual(operator.__sub__(long(0), long(0)), long(0)) self.assertEqual(operator.__sub__(long(1), long(2)), -long(1)) self.assertEqual(operator.__sub__(-long(1), long(2)), -long(3)) #__mul__ self.assertEqual(operator.__mul__(0, 0), 0) self.assertEqual(operator.__mul__(1, 2), 2) self.assertEqual(operator.__mul__(-1, 2), -2) self.assertEqual(operator.__mul__(0.0, 0.0), 0.0) self.assertEqual(operator.__mul__(2.0, 3.0), 6.0) self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0) self.assertEqual(operator.__mul__(long(0), long(0)), long(0)) self.assertEqual(operator.__mul__(long(1), long(2)), long(2)) self.assertEqual(operator.__mul__(-long(1), long(2)), -long(2)) #__div__ self.assertEqual(operator.__div__(0, 1), 0) self.assertEqual(operator.__div__(4, 2), 2) self.assertEqual(operator.__div__(-1, 2), -1) self.assertEqual(operator.__div__(0.0, 1.0), 0.0) self.assertEqual(operator.__div__(4.0, 2.0), 2.0) self.assertEqual(operator.__div__(-4.0, 2.0), -2.0) self.assertEqual(operator.__div__(long(0), long(1)), long(0)) self.assertEqual(operator.__div__(long(4), long(2)), long(2)) self.assertEqual(operator.__div__(-long(4), long(2)), -long(2)) #__floordiv__ self.assertEqual(operator.__floordiv__(0, 1), 0) self.assertEqual(operator.__floordiv__(4, 2), 2) self.assertEqual(operator.__floordiv__(-1, 2), -1) self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0) self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0) self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0) self.assertEqual(operator.__floordiv__(long(0), long(1)), long(0)) self.assertEqual(operator.__floordiv__(long(4), long(2)), long(2)) self.assertEqual(operator.__floordiv__(-long(4), long(2)), -long(2)) #__truediv__ self.assertEqual(operator.__truediv__(0, 1), 0) self.assertEqual(operator.__truediv__(4, 2), 2) self.assertEqual(operator.__truediv__(-1, 2), -0.5) self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0) self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0) self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5) self.assertEqual(operator.__truediv__(long(0), long(1)), long(0)) self.assertEqual(operator.__truediv__(long(4), long(2)), long(2)) self.assertEqual(operator.__truediv__(-long(4), long(2)), -long(2)) #__mod__ self.assertEqual(operator.__mod__(0, 1), 0) self.assertEqual(operator.__mod__(4, 2), 0) self.assertEqual(operator.__mod__(-1, 2), 1) self.assertEqual(operator.__mod__(0.0, 1.0), 0.0) self.assertEqual(operator.__mod__(4.0, 2.0), 0.0) self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0) self.assertEqual(operator.__mod__(long(0), long(1)), long(0)) self.assertEqual(operator.__mod__(long(4), long(2)), long(0)) self.assertEqual(operator.__mod__(-long(4), long(2)), long(0)) #__inv__ self.assertEqual(operator.__inv__(0), -1) self.assertEqual(operator.__inv__(1), -2) self.assertEqual(operator.__inv__(-1), 0) self.assertEqual(operator.__inv__(long(0)), -long(1)) self.assertEqual(operator.__inv__(long(1)), -long(2)) self.assertEqual(operator.__inv__(-long(1)), long(0)) #__invert__ self.assertEqual(operator.__invert__(0), -1) self.assertEqual(operator.__invert__(1), -2) self.assertEqual(operator.__invert__(-1), 0) self.assertEqual(operator.__invert__(long(0)), -long(1)) self.assertEqual(operator.__invert__(long(1)), -long(2)) self.assertEqual(operator.__invert__(-long(1)), long(0)) #__lshift__ self.assertEqual(operator.__lshift__(0, 1), 0) self.assertEqual(operator.__lshift__(1, 1), 2) self.assertEqual(operator.__lshift__(-1, 1), -2) self.assertEqual(operator.__lshift__(long(0), 1), long(0)) self.assertEqual(operator.__lshift__(long(1), 1), long(2)) self.assertEqual(operator.__lshift__(-long(1), 1), -long(2)) #__rshift__ self.assertEqual(operator.__rshift__(1, 1), 0) self.assertEqual(operator.__rshift__(2, 1), 1) self.assertEqual(operator.__rshift__(-1, 1), -1) self.assertEqual(operator.__rshift__(long(1), 1), long(0)) self.assertEqual(operator.__rshift__(long(2), 1), long(1)) self.assertEqual(operator.__rshift__(-long(1), 1), -long(1)) #__not__ self.assertEqual(operator.__not__(0), 1) self.assertEqual(operator.__not__(1), 0) self.assertEqual(operator.__not__(-1), 0) self.assertEqual(operator.__not__(long(0)), 1) self.assertEqual(operator.__not__(long(1)), 0) self.assertEqual(operator.__not__(-long(1)), 0) #__and__ self.assertEqual(operator.__and__(0, 0), 0) self.assertEqual(operator.__and__(1, 1), 1) self.assertEqual(operator.__and__(0, 1), 0) self.assertEqual(operator.__and__(1, 0), 0) #__xor__ self.assertEqual(operator.__xor__(0, 0), 0) self.assertEqual(operator.__xor__(1, 1), 0) self.assertEqual(operator.__xor__(0, 1), 1) self.assertEqual(operator.__xor__(1, 0), 1) #__or__ self.assertEqual(operator.__or__(0, 0), 0) self.assertEqual(operator.__or__(1, 1), 1) self.assertEqual(operator.__or__(0, 1), 1) self.assertEqual(operator.__or__(1, 0), 1) #__concat__ self.assertEqual(operator.__concat__([0], [1]), [0,1]) self.assertEqual(operator.__concat__([2], [1]), [2,1]) self.assertEqual(operator.__concat__([-1], [1]), [-1,1]) #__contains__ self.assertTrue(operator.__contains__("abc", "c")) self.assertTrue(not operator.__contains__("abc", "d")) self.assertTrue(operator.__contains__("abc", "")) self.assertTrue(not operator.__contains__("", "c")) self.assertTrue(operator.__contains__([1,2,3], 1)) self.assertTrue(not operator.__contains__([1,2,3], 4)) #__getitem__ self.assertEqual(operator.__getitem__("abc", 2), "c") self.assertRaises(IndexError, operator.__getitem__, "abc", 3) self.assertEqual(operator.__getitem__([1,2,3], 2), 3) self.assertRaises(IndexError, operator.__getitem__, [1,2,3], 3) #__setitem__ self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1,2,3] operator.__setitem__(t_list, 2, 4) self.assertEqual(t_list, [1,2,4]) self.assertRaises(IndexError, operator.__setitem__, [1,2,3], 4, 9) #__delitem__ #UNIMPLEMENTED #self.assertRaises(TypeError, operator.__delitem__, "abc", 2) t_list = [1,2,3] operator.__delitem__(t_list, 2) self.assertEqual(t_list, [1,2]) self.assertRaises(IndexError, operator.__delitem__, [1,2,3], 4) #__repeat__ self.assertEqual(operator.__repeat__("abc", 2), "abcabc") self.assertEqual(operator.__repeat__("", 2), "") self.assertEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3]) #__getslice__ self.assertEqual(operator.__getslice__("abc", 1, 2), "b") self.assertEqual(operator.__getslice__("abc", 0, 3), "abc") self.assertEqual(operator.__getslice__("", 0, 0), "") self.assertEqual(operator.__getslice__([1,2,3], 1, 2), [2]) self.assertEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3]) self.assertEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1,2,3] operator.__delslice__(t_list, 1, 2) self.assertEqual(t_list, [1,3]) t_list = [1,2,3] operator.__delslice__(t_list, 0, 3) self.assertEqual(t_list, []) t_list = [1,2,3] operator.__delslice__(t_list, 0, 0) self.assertEqual(t_list, [1,2,3]) #__setslice__ t_list = [1,2,3] operator.__setslice__(t_list, 1, 2, [9]) self.assertEqual(t_list, [1,9,3]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 3, [9, 8]) self.assertEqual(t_list, [9, 8]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 0, [9]) self.assertEqual(t_list, [9,1, 2,3])
def __rsub__(self, other): """other - self""" return __sub__(other, get_wrapped_object(self))
def __sub__(self, other): """self - other""" return __sub__(get_wrapped_object(self), get_wrapped_object(other))
def update_event(self, inp=-1): self.set_output_val(0, operator.__sub__(self.input(0), self.input(1)))
def __sub__(self, other): if isinstance(other, Number) or len(other) == 1: return Vector(map(lambda x: operator.__sub__(x, other), self)) else: return Vector(map(operator.__sub__, self, other))
def eval(self,env): return operator.__sub__(self.left.eval(env),self.right.eval(env))
def test_sanity(self): """Performs a set of simple sanity checks on most operators.""" #__abs__ self.assertEqual(operator.__abs__(0), 0) self.assertEqual(operator.__abs__(1), 1) self.assertEqual(operator.__abs__(-1), 1) self.assertEqual(operator.__abs__(0.0), 0.0) self.assertEqual(operator.__abs__(1.1), 1.1) self.assertEqual(operator.__abs__(-1.1), 1.1) self.assertEqual(operator.__abs__(big(0)), big(0)) self.assertEqual(operator.__abs__(big(1)), big(1)) self.assertEqual(operator.__abs__(-big(1)), big(1)) #__neg__ self.assertEqual(operator.__neg__(0), 0) self.assertEqual(operator.__neg__(1), -1) self.assertEqual(operator.__neg__(-1), 1) self.assertEqual(operator.__neg__(0.0), 0.0) self.assertEqual(operator.__neg__(1.1), -1.1) self.assertEqual(operator.__neg__(-1.1), 1.1) self.assertEqual(operator.__neg__(big(0)), big(0)) self.assertEqual(operator.__neg__(big(1)), -big(1)) self.assertEqual(operator.__neg__(-big(1)), big(1)) #__pos__ self.assertEqual(operator.__pos__(0), 0) self.assertEqual(operator.__pos__(1), 1) self.assertEqual(operator.__pos__(-1), -1) self.assertEqual(operator.__pos__(0.0), 0.0) self.assertEqual(operator.__pos__(1.1), 1.1) self.assertEqual(operator.__pos__(-1.1), -1.1) self.assertEqual(operator.__pos__(big(0)), big(0)) self.assertEqual(operator.__pos__(big(1)), big(1)) self.assertEqual(operator.__pos__(-big(1)), -big(1)) #__add__ self.assertEqual(operator.__add__(0, 0), 0) self.assertEqual(operator.__add__(1, 2), 3) self.assertEqual(operator.__add__(-1, 2), 1) self.assertEqual(operator.__add__(0.0, 0.0), 0.0) self.assertEqual(operator.__add__(1.1, 2.1), 3.2) self.assertEqual(operator.__add__(-1.1, 2.1), 1.0) self.assertEqual(operator.__add__(big(0), big(0)), big(0)) self.assertEqual(operator.__add__(big(1), big(2)), big(3)) self.assertEqual(operator.__add__(-big(1), big(2)), big(1)) #__sub__ self.assertEqual(operator.__sub__(0, 0), 0) self.assertEqual(operator.__sub__(1, 2), -1) self.assertEqual(operator.__sub__(-1, 2), -3) self.assertEqual(operator.__sub__(0.0, 0.0), 0.0) self.assertEqual(operator.__sub__(1.1, 2.1), -1.0) self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2) self.assertEqual(operator.__sub__(big(0), big(0)), big(0)) self.assertEqual(operator.__sub__(big(1), big(2)), -big(1)) self.assertEqual(operator.__sub__(-big(1), big(2)), -big(3)) #__mul__ self.assertEqual(operator.__mul__(0, 0), 0) self.assertEqual(operator.__mul__(1, 2), 2) self.assertEqual(operator.__mul__(-1, 2), -2) self.assertEqual(operator.__mul__(0.0, 0.0), 0.0) self.assertEqual(operator.__mul__(2.0, 3.0), 6.0) self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0) self.assertEqual(operator.__mul__(big(0), big(0)), big(0)) self.assertEqual(operator.__mul__(big(1), big(2)), big(2)) self.assertEqual(operator.__mul__(-big(1), big(2)), -big(2)) #__div__ self.assertEqual(operator.__div__(0, 1), 0) self.assertEqual(operator.__div__(4, 2), 2) self.assertEqual(operator.__div__(-1, 2), -1) self.assertEqual(operator.__div__(0.0, 1.0), 0.0) self.assertEqual(operator.__div__(4.0, 2.0), 2.0) self.assertEqual(operator.__div__(-4.0, 2.0), -2.0) self.assertEqual(operator.__div__(big(0), big(1)), big(0)) self.assertEqual(operator.__div__(big(4), big(2)), big(2)) self.assertEqual(operator.__div__(-big(4), big(2)), -big(2)) #__floordiv__ self.assertEqual(operator.__floordiv__(0, 1), 0) self.assertEqual(operator.__floordiv__(4, 2), 2) self.assertEqual(operator.__floordiv__(-1, 2), -1) self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0) self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0) self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0) self.assertEqual(operator.__floordiv__(big(0), big(1)), big(0)) self.assertEqual(operator.__floordiv__(big(4), big(2)), big(2)) self.assertEqual(operator.__floordiv__(-big(4), big(2)), -big(2)) #__truediv__ self.assertEqual(operator.__truediv__(0, 1), 0) self.assertEqual(operator.__truediv__(4, 2), 2) self.assertEqual(operator.__truediv__(-1, 2), -0.5) self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0) self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0) self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5) self.assertEqual(operator.__truediv__(big(0), big(1)), big(0)) self.assertEqual(operator.__truediv__(big(4), big(2)), big(2)) self.assertEqual(operator.__truediv__(-big(4), big(2)), -big(2)) #__mod__ self.assertEqual(operator.__mod__(0, 1), 0) self.assertEqual(operator.__mod__(4, 2), 0) self.assertEqual(operator.__mod__(-1, 2), 1) self.assertEqual(operator.__mod__(0.0, 1.0), 0.0) self.assertEqual(operator.__mod__(4.0, 2.0), 0.0) self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0) self.assertEqual(operator.__mod__(big(0), big(1)), big(0)) self.assertEqual(operator.__mod__(big(4), big(2)), big(0)) self.assertEqual(operator.__mod__(-big(4), big(2)), big(0)) #__inv__ self.assertEqual(operator.__inv__(0), -1) self.assertEqual(operator.__inv__(1), -2) self.assertEqual(operator.__inv__(-1), 0) self.assertEqual(operator.__inv__(big(0)), -big(1)) self.assertEqual(operator.__inv__(big(1)), -big(2)) self.assertEqual(operator.__inv__(-big(1)), big(0)) #__invert__ self.assertEqual(operator.__invert__(0), -1) self.assertEqual(operator.__invert__(1), -2) self.assertEqual(operator.__invert__(-1), 0) self.assertEqual(operator.__invert__(big(0)), -big(1)) self.assertEqual(operator.__invert__(big(1)), -big(2)) self.assertEqual(operator.__invert__(-big(1)), big(0)) #__lshift__ self.assertEqual(operator.__lshift__(0, 1), 0) self.assertEqual(operator.__lshift__(1, 1), 2) self.assertEqual(operator.__lshift__(-1, 1), -2) self.assertEqual(operator.__lshift__(big(0), 1), big(0)) self.assertEqual(operator.__lshift__(big(1), 1), big(2)) self.assertEqual(operator.__lshift__(-big(1), 1), -big(2)) #__rshift__ self.assertEqual(operator.__rshift__(1, 1), 0) self.assertEqual(operator.__rshift__(2, 1), 1) self.assertEqual(operator.__rshift__(-1, 1), -1) self.assertEqual(operator.__rshift__(big(1), 1), big(0)) self.assertEqual(operator.__rshift__(big(2), 1), big(1)) self.assertEqual(operator.__rshift__(-big(1), 1), -big(1)) #__not__ self.assertEqual(operator.__not__(0), 1) self.assertEqual(operator.__not__(1), 0) self.assertEqual(operator.__not__(-1), 0) self.assertEqual(operator.__not__(big(0)), 1) self.assertEqual(operator.__not__(big(1)), 0) self.assertEqual(operator.__not__(-big(1)), 0) #__and__ self.assertEqual(operator.__and__(0, 0), 0) self.assertEqual(operator.__and__(1, 1), 1) self.assertEqual(operator.__and__(0, 1), 0) self.assertEqual(operator.__and__(1, 0), 0) #__xor__ self.assertEqual(operator.__xor__(0, 0), 0) self.assertEqual(operator.__xor__(1, 1), 0) self.assertEqual(operator.__xor__(0, 1), 1) self.assertEqual(operator.__xor__(1, 0), 1) #__or__ self.assertEqual(operator.__or__(0, 0), 0) self.assertEqual(operator.__or__(1, 1), 1) self.assertEqual(operator.__or__(0, 1), 1) self.assertEqual(operator.__or__(1, 0), 1) #__concat__ self.assertEqual(operator.__concat__([0], [1]), [0, 1]) self.assertEqual(operator.__concat__([2], [1]), [2, 1]) self.assertEqual(operator.__concat__([-1], [1]), [-1, 1]) #__contains__ self.assertTrue(operator.__contains__("abc", "c")) self.assertTrue(not operator.__contains__("abc", "d")) self.assertTrue(operator.__contains__("abc", "")) self.assertTrue(not operator.__contains__("", "c")) self.assertTrue(operator.__contains__([1, 2, 3], 1)) self.assertTrue(not operator.__contains__([1, 2, 3], 4)) #__getitem__ self.assertEqual(operator.__getitem__("abc", 2), "c") self.assertRaises(IndexError, operator.__getitem__, "abc", 3) self.assertEqual(operator.__getitem__([1, 2, 3], 2), 3) self.assertRaises(IndexError, operator.__getitem__, [1, 2, 3], 3) #__setitem__ self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1, 2, 3] operator.__setitem__(t_list, 2, 4) self.assertEqual(t_list, [1, 2, 4]) self.assertRaises(IndexError, operator.__setitem__, [1, 2, 3], 4, 9) #__delitem__ #UNIMPLEMENTED #self.assertRaises(TypeError, operator.__delitem__, "abc", 2) t_list = [1, 2, 3] operator.__delitem__(t_list, 2) self.assertEqual(t_list, [1, 2]) self.assertRaises(IndexError, operator.__delitem__, [1, 2, 3], 4) #__repeat__ self.assertEqual(operator.__repeat__("abc", 2), "abcabc") self.assertEqual(operator.__repeat__("", 2), "") self.assertEqual(operator.__repeat__([1, 2, 3], 2), [1, 2, 3, 1, 2, 3]) #__getslice__ self.assertEqual(operator.__getslice__("abc", 1, 2), "b") self.assertEqual(operator.__getslice__("abc", 0, 3), "abc") self.assertEqual(operator.__getslice__("", 0, 0), "") self.assertEqual(operator.__getslice__([1, 2, 3], 1, 2), [2]) self.assertEqual(operator.__getslice__([1, 2, 3], 0, 3), [1, 2, 3]) self.assertEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1, 2, 3] operator.__delslice__(t_list, 1, 2) self.assertEqual(t_list, [1, 3]) t_list = [1, 2, 3] operator.__delslice__(t_list, 0, 3) self.assertEqual(t_list, []) t_list = [1, 2, 3] operator.__delslice__(t_list, 0, 0) self.assertEqual(t_list, [1, 2, 3]) #__setslice__ t_list = [1, 2, 3] operator.__setslice__(t_list, 1, 2, [9]) self.assertEqual(t_list, [1, 9, 3]) t_list = [1, 2, 3] operator.__setslice__(t_list, 0, 3, [9, 8]) self.assertEqual(t_list, [9, 8]) t_list = [1, 2, 3] operator.__setslice__(t_list, 0, 0, [9]) self.assertEqual(t_list, [9, 1, 2, 3])
pos = spice(lambda x, y: operator.pos(x, y), name='pos', doc=operator.pos.__doc__) #reversed pow = spice(lambda x, y: operator.pow(y, x), name='pow') __pow__ = spice(lambda x, y: operator.__pow__(y, x), name='__pow__') # reversed rshift = spice(lambda x, y: operator.rshift(y, x), name='rshift') __rshift__ = spice(lambda x, y: operator.__rshift__(y, x), name='__rshift__') # reversed sub = spice(lambda x, y: operator.sub(y, x), name='sub') __sub__ = spice(lambda x, y: operator.__sub__(y, x), name='__sub__') # reversed truediv = spice(lambda x, y: operator.truediv(y, x), name='truediv') __truediv__ = spice(lambda x, y: operator.__truediv__(y, x), name='__truediv__') xor = spice(lambda x, y: operator.xor(x, y), name='xor', doc=operator.xor.__doc__) __xor__ = spice(lambda x, y: operator.__xor__(x, y), name='__xor__', doc=operator.xor.__doc__) #################################################