Ejemplo n.º 1
0
    def test_irepeat(self):
        import operator

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

        a = range(3)
        raises(TypeError, operator.irepeat)
        raises(TypeError, operator.irepeat, a, None)
        raises(TypeError, operator.irepeat, a, [])
        raises(TypeError, operator.irepeat, a, X())
        raises(TypeError, operator.irepeat, 6, 7)
        assert operator.irepeat(a, 2L) is a
        assert a == [0, 1, 2, 0, 1, 2]
        assert operator.irepeat(a, 1) is a
        assert a == [0, 1, 2, 0, 1, 2]
Ejemplo n.º 2
0
 def test_inplace(self):
     class C(object):
         def __iadd__     (self, other): return "iadd"
         def __iand__     (self, other): return "iand"
         def __idiv__     (self, other): return "idiv"
         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 __imatmul__  (self, other): return "imatmul"
         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.idiv     (c, 5), "idiv")
     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.imatmul  (c, 5), "imatmul")
     self.assertEqual(operator.ior      (c, 5), "ior")
     self.assertEqual(operator.ipow     (c, 5), "ipow")
     self.assertEqual(operator.irshift  (c, 5), "irshift")
     self.assertEqual(operator.isub     (c, 5), "isub")
     self.assertEqual(operator.itruediv (c, 5), "itruediv")
     self.assertEqual(operator.ixor     (c, 5), "ixor")
     self.assertEqual(operator.iconcat  (c, c), "iadd")
     self.assertEqual(operator.irepeat  (c, 5), "imul")
     self.assertEqual(operator.__iadd__     (c, 5), "iadd")
     self.assertEqual(operator.__iand__     (c, 5), "iand")
     self.assertEqual(operator.__idiv__     (c, 5), "idiv")
     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.__imatmul__  (c, 5), "imatmul")
     self.assertEqual(operator.__ior__      (c, 5), "ior")
     self.assertEqual(operator.__ipow__     (c, 5), "ipow")
     self.assertEqual(operator.__irshift__  (c, 5), "irshift")
     self.assertEqual(operator.__isub__     (c, 5), "isub")
     self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
     self.assertEqual(operator.__ixor__     (c, 5), "ixor")
     self.assertEqual(operator.__iconcat__  (c, c), "iadd")
     self.assertEqual(operator.__irepeat__  (c, 5), "imul")
Ejemplo n.º 3
0
 def test_inplace(self):
     class C(object):
         def __iadd__     (self, other): return "iadd"
         def __iand__     (self, other): return "iand"
         def __idiv__     (self, other): return "idiv"
         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.idiv     (c, 5), "idiv")
     self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
     self.assertEqual(operator.ilshift  (c, 5), "ilshift")
     self.assertEqual(operator.imod     (c, 5), "imod")
     self.assertEqual(operator.imul     (c, 5), "imul")
     self.assertEqual(operator.ior      (c, 5), "ior")
     self.assertEqual(operator.ipow     (c, 5), "ipow")
     self.assertEqual(operator.irshift  (c, 5), "irshift")
     self.assertEqual(operator.isub     (c, 5), "isub")
     self.assertEqual(operator.itruediv (c, 5), "itruediv")
     self.assertEqual(operator.ixor     (c, 5), "ixor")
     self.assertEqual(operator.iconcat  (c, c), "iadd")
     self.assertEqual(operator.irepeat  (c, 5), "imul")
     self.assertEqual(operator.__iadd__     (c, 5), "iadd")
     self.assertEqual(operator.__iand__     (c, 5), "iand")
     self.assertEqual(operator.__idiv__     (c, 5), "idiv")
     self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
     self.assertEqual(operator.__ilshift__  (c, 5), "ilshift")
     self.assertEqual(operator.__imod__     (c, 5), "imod")
     self.assertEqual(operator.__imul__     (c, 5), "imul")
     self.assertEqual(operator.__ior__      (c, 5), "ior")
     self.assertEqual(operator.__ipow__     (c, 5), "ipow")
     self.assertEqual(operator.__irshift__  (c, 5), "irshift")
     self.assertEqual(operator.__isub__     (c, 5), "isub")
     self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
     self.assertEqual(operator.__ixor__     (c, 5), "ixor")
     self.assertEqual(operator.__iconcat__  (c, c), "iadd")
     self.assertEqual(operator.__irepeat__  (c, 5), "imul")
Ejemplo n.º 4
0
    def test_irepeat(self):
        import operator

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

        a = range(3)
        raises(TypeError, operator.irepeat)
        raises(TypeError, operator.irepeat, a, None)
        raises(TypeError, operator.irepeat, a, [])
        raises(TypeError, operator.irepeat, a, X())
        raises(TypeError, operator.irepeat, 6, 7)
        assert operator.irepeat(a, 2L) is a
        assert a == [0, 1, 2, 0, 1, 2]
        assert operator.irepeat(a, 1) is a
        assert a == [0, 1, 2, 0, 1, 2]
Ejemplo n.º 5
0
print operator.getslice(a, 0, 2)

#设定字典的值 同 a[b]=c
a = {0: "zero", 1: "one", 2: "two"}
operator.setitem(a, 3, "three")
print a

#设定序列的片段 同 a[b:c]=d
a = [1, 2, 3, 4, 5]
operator.setslice(a, 1, 2, ["two"])
print a

#序列翻倍 同序列的 *=
a = [1, 2, 3]
print operator.repeat(a, 5)
print operator.irepeat(a, 5)

#判断值相等 同 =
print operator.eq(1, "1")
print operator.eq("a", "a")

#判断值不等 同 !=
print operator.ne(1, "1")
print operator.ne("a", "a")

#判断地址相等 同 is
print operator.is_(1, "1")
print operator.eq("a", "a")

#判断地址不相等 同 is not
print operator.is_(1, "1")
Ejemplo n.º 6
0
print operator.getslice(a,0,2)

#设定字典的值 同 a[b]=c
a = {0:"zero",1:"one",2:"two"}
operator.setitem(a,3,"three")
print a

#设定序列的片段 同 a[b:c]=d
a = [1,2,3,4,5]
operator.setslice(a,1,2,["two"])
print a

#序列翻倍 同序列的 *=
a = [1,2,3]
print operator.repeat(a,5)
print operator.irepeat(a,5)

#判断值相等 同 =
print operator.eq(1,"1")
print operator.eq("a","a")

#判断值不等 同 !=
print operator.ne(1,"1")
print operator.ne("a","a")

#判断地址相等 同 is
print operator.is_(1,"1")
print operator.eq("a","a")

#判断地址不相等 同 is not
print operator.is_(1,"1")