def test_wchar_ptr(self): s = u"abcdefghijklmnopqrstuvwxyz\0" dll.my_wcsdup.restype = POINTER(c_wchar) dll.my_wcsdup.argtypes = POINTER(c_wchar), dll.my_free.restype = None res = dll.my_wcsdup(s) assert res[:len(s)] == s import operator with pytest.raises(TypeError): operator.setslice(res, 0, 5, u"abcde") dll.my_free(res) if sizeof(c_wchar) == sizeof(c_short): dll.my_wcsdup.restype = POINTER(c_short) elif sizeof(c_wchar) == sizeof(c_int): dll.my_wcsdup.restype = POINTER(c_int) elif sizeof(c_wchar) == sizeof(c_long): dll.my_wcsdup.restype = POINTER(c_long) else: return res = dll.my_wcsdup(s) assert res[:len(s) - 1] == range(ord("a"), ord("z") + 1) dll.my_free(res)
def test_setslice(self): a = range(4) self.failUnlessRaises(TypeError, operator.setslice, a) self.failUnlessRaises(TypeError, operator.setslice, a, None, None, None) self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None) self.assert_(a == [0, 2, 1, 3]) operator.setslice(a, 0, test_support.MAX_Py_ssize_t, []) self.assert_(a == [])
def test_setslice(self): a = range(4) self.assertRaises(TypeError, operator.setslice, a) self.assertRaises(TypeError, operator.setslice, a, None, None, None) self.assertTrue(operator.setslice(a, 1, 3, [2, 1]) is None) self.assertTrue(a == [0, 2, 1, 3]) operator.setslice(a, 0, test_support.MAX_Py_ssize_t, []) self.assertTrue(a == [])
def test_setslice(self): a = list(range(4)) self.assertRaises(TypeError, operator.setslice, a) self.assertRaises(TypeError, operator.setslice, a, None, None, None) self.assertTrue(operator.setslice(a, 1, 3, [2, 1]) is None) self.assertTrue(a == [0, 2, 1, 3]) operator.setslice(a, 0, support.MAX_Py_ssize_t, []) self.assertTrue(a == [])
def __setitem__ (self,key,value): if isinstance(key,slice): if hasattr(value,'__getitem__'): try: operator.setitem(self._datap,key,value) # works for 2.6+ except TypeError: operator.setslice(self._datap,key.start,key.stop, value) # deprecated in 2.6, works in 2.5 #self._datap.__setitem__(key,value) else: raise TypeError("Value must be a sequence") else: self._datap[key] = self.dtype(value)
def __setitem__(self, key, value): if isinstance(key, slice): if hasattr(value, '__getitem__'): try: operator.setitem(self._datap, key, value) # works for 2.6+ except TypeError: operator.setslice(self._datap, key.start, key.stop, value) # deprecated in 2.6, works in 2.5 #self._datap.__setitem__(key,value) else: raise TypeError("Value must be a sequence") else: self._datap[key] = self.dtype(value)
def test_setslice(self): a = range(4) self.failUnlessRaises(TypeError, operator.setslice, a) self.failUnlessRaises(TypeError, operator.setslice, a, None, None, None) self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None) self.assert_(a == [0, 2, 1, 3])
def test_char_ptr(self): s = "abcdefghijklmnopqrstuvwxyz" dll.my_strdup.restype = POINTER(c_char) dll.my_free.restype = None res = dll.my_strdup(s) assert res[:len(s)] == s import operator with pytest.raises(TypeError): operator.setslice(res, 0, 5, u"abcde") dll.my_free(res) dll.my_strdup.restype = POINTER(c_byte) res = dll.my_strdup(s) assert res[:len(s)] == range(ord("a"), ord("z") + 1) dll.my_free(res)
def test_setslice_cint(self): a = (c_int * 100)(*xrange(1100, 1200)) b = range(1100, 1200) a[32:47] = range(32, 47) assert a[32:47] == range(32, 47) from operator import setslice # TypeError: int expected instead of str instance with pytest.raises(TypeError): setslice(a, 0, 5, "abcde") # TypeError: int expected instead of str instance with pytest.raises(TypeError): setslice(a, 0, 5, ["a", "b", "c", "d", "e"]) # TypeError: int expected instead of float instance with pytest.raises(TypeError): setslice(a, 0, 5, [1, 2, 3, 4, 3.14]) # ValueError: Can only assign sequence of same size with pytest.raises(ValueError): setslice(a, 0, 5, range(32))
def setslice(a, b, c, v): return operator.setslice(a, b, c, v)
# True # False # 4 # 0 # 1 c = [1, 'a', None, 10.01, {'key':'value'}] # 通过下标操作序列 print operator.getitem(c, 4) print operator.getslice(c, 1, 4) operator.setitem(c, 2, 9999) print c operator.setslice(c, 3,-1, [10,20,30,40,50]) print c operator.delitem(c, 0) print c operator.delslice(c, 2, 7) print c # output # {'key': 'value'} # ['a', None, 10.01] # [1, 'a', 9999, 10.01, {'key': 'value'}] # [1, 'a', 9999, 10, 20, 30, 40, 50, {'key': 'value'}] # ['a', 9999, 10, 20, 30, 40, 50, {'key': 'value'}] # ['a', 9999, {'key': 'value'}]
#取得字典的值 同 a[b] a = {0:"zero",1:"one",2:"two"} print operator.getitem(a,0) #取得序列的片段 同 a[b:c] a = [1,2,3,4,5] 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")
print(" is ", "abc" is "abc") print(" is_", op.is_("abc","abc")) print(" is not ", "abc" is not "abcd") print(" is_", op.is_not("abc","abcd")) print(" in ", "a" in "abc") print(" __contains__", "abc".__contains__('a')) print(" contains", op.contains("abc",'a')) print(" in ", "a" not in "abc") print(" __contains__", not("abc".__contains__('a'))) print(" contains", op.not_(op.contains("abc",'a'))) print(" __getslice__ :", [0,1,2,3].__getslice__(0,2)) print(" getslice :", op.getslice([0,1,2,3],0,2)) l=[0,1,2,3] print(" __setslice__ :", l.__setslice__(0,2,[99,99]),l) print(" __delslice__ :", l.__delslice__(0,2),l) l=[0,1,2,3] print(" setslice :", op.setslice(l,0,2,[99,99]),l) print(" delslice :", op.delslice(l,0,2),l) print(" __getitem__ : ", [0,1,2,3].__getitem__(slice(0,2))) print(" getitem : ", op.getitem([0,1,2,3],slice(0,2))) l=[0,1,2,3] print(" __setitem__ :", l.__setitem__(slice(0,2),[99,99]),l) print(" __delitem__ :", l.__delitem__(slice(0,2)),l) l=[0,1,2,3] print(" setitem :", op.setitem(l,slice(0,2),[99,99]),l) print(" delitem :", op.delitem(l,slice(0,2)),l)
def test_setslice(self): a = range(4) self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None) self.assert_(a == [0, 2, 1, 3])
#取得字典的值 同 a[b] a = {0: "zero", 1: "one", 2: "two"} print operator.getitem(a, 0) #取得序列的片段 同 a[b:c] a = [1, 2, 3, 4, 5] 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")
import operator