示例#1
0
 def check_exp(self, Value):
     t = Value.split("e")
     if "." in t[0]:
         t1 = str(__neg__(float(t[0])))
     else:
         t1 = str(__neg__(float(t[0])))[:-2]
     Value = str(t1 + "e" + t[1])
     return Value
示例#2
0
 def Get_Plus_Moins( self ):
     if self.action == "":
         if not self.first == "":
             if "e+" in self.first: self.first = self.check_exp( self.first )
             elif "e-" in self.first: self.first = self.check_exp( self.first )
             else: self.first = str( __neg__( float( self.first ) ) )
             self.getControl( 20 ).setLabel( self.first )
     elif not self.second == "":
         if "e+" in self.second: self.second = self.check_exp( self.second )
         elif "e-" in self.second: self.second = self.check_exp( self.second )
         else: self.second = str( __neg__( float( self.second ) ) )
         self.getControl( 20 ).setLabel( self.second )
     self.m_read_clean = False
示例#3
0
    def _align___sub__(self, a):
        cardinalities = [ self._cardinality(v) for v in a.args ]
        if max(cardinalities) == cardinalities[0]:
            return a

        adjusted = tuple(operator.__neg__(v) for v in a.args[1:]) + a.args[:1]
        return a.make_like('__add__', tuple(sorted(adjusted, key=lambda v: -self._cardinality(v))))
示例#4
0
    def _align___sub__(self, a):
        cardinalities = [ self._cardinality(v) for v in a.args ]
        if max(cardinalities) == cardinalities[0]:
            return a

        adjusted = tuple(operator.__neg__(v) for v in a.args[1:]) + a.args[:1]
        return a.make_like('__add__', tuple(sorted(adjusted, key=lambda v: -self._cardinality(v))))
示例#5
0
def _dict_to_tuple(d):
    '''Convert a dictionary to a time tuple.  Depends on key values in the
    regexp pattern!
    '''    
    # TODO: Adding a ms field to struct_time tuples is problematic 
    # since they don't have this field.  Should use datetime
    # which has a microseconds field, else no ms..  When mapping struct_time 
    # to gDateTime the last 3 fields are irrelevant, here using dummy values to make
    # everything happy.
    # 

    retval = _niltime[:]
    for k,i in ( ('Y', 0), ('M', 1), ('D', 2), ('h', 3), ('m', 4), ):
        v = d.get(k)
        if v: retval[i] = int(v)
        
    v = d.get('s')
    if v:
        msec,sec = _modf(float(v))
        retval[6],retval[5] = int(round(msec*1000)), int(sec)

    if d.get('neg', 0):
        retval[0:5] = map(lambda x: (x is not None or x) and operator.__neg__(x), retval[0:5])

    return tuple(retval)
示例#6
0
 def Set_m_moins( self ):
     if self.first == "": return
     elif not self.action == "": 
         if self.second == "": return
     if self.action == "": entry = self.first
     else: entry = self.second
     self.mrc[ "MRC" ] = str( __neg__( float( entry ) ) )
     f = open( self.tmp_file, "wb" )
     dump( self.mrc, f )
     f.close()
     self.getControl( 23 ).setLabel( LANGUAGE( 32015 ) )
     self.m_read_clean = False
示例#7
0
    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])
示例#8
0
 def __neg__(self, other):
     if isinstance(other, Number) or len(other) == 1:
         return Vector(map(lambda x: operator.__neg__(x, other), self))
     else:
         return Vector(map(operator.__neg__, self, other))
示例#9
0
 def __neg__(self):
     """Return the resulting reward by applying the unary negative operation on it."""
     reward = copy.copy(self)  # shallow copy
     reward._compute = lambda: operator.__neg__(self())
     reward.range = (-self.range[1], -self.range[0])
     return reward
示例#10
0
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])
示例#11
0
 def __neg__(self):
     return operator.__neg__(self._tensor)
示例#12
0
 def __neg__(self):
     """-self"""
     return __neg__(get_wrapped_object(self))
示例#13
0
 def update_event(self, inp=-1):
     self.set_output_val(0, operator.__neg__(self.input(0)))
示例#14
0
文件: _callproxy.py 项目: flynx/pli
	def __neg__(self):
		return operator.__neg__(self.p_obj)
示例#15
0
文件: operator.py 项目: kanales/spycy
# 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__)

#################################################

neg = spice(lambda x: operator.neg(x), name='neg', doc=operator.neg.__doc__)
__neg__ = spice(lambda x: operator.__neg__(x),
                name='__neg__',
                doc=operator.neg.__doc__)

not_ = spice(lambda x: operator.not_(x),
             name='not_',
             doc=operator.not_.__doc__)
__not__ = spice(lambda x: operator.__not__(x),
                name='__not__',
                doc=operator.not_.__doc__)

index = spice(lambda x: operator.index(x),
              name='index',
              doc=operator.index.__doc__)
__index__ = spice(lambda x: operator.__index__(x),
                  name='__index__',
示例#16
0
    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 compute(*args, **kwargs):
     return operator.__neg__(self(*args, **kwargs))
示例#18
0
 def check_exp( self, Value ):
     t = Value.split( "e" )
     if "." in t[ 0 ]: t1 = str( __neg__( float( t[ 0 ] ) ) )
     else: t1 = str( __neg__( float( t[ 0 ] ) ) )[ :-2 ]
     Value = str( t1 + "e" + t[ 1 ] )
     return Value