当前位置: 首页>>代码示例>>Python>>正文


Python operator.setitem方法代码示例

本文整理汇总了Python中operator.setitem方法的典型用法代码示例。如果您正苦于以下问题:Python operator.setitem方法的具体用法?Python operator.setitem怎么用?Python operator.setitem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在operator的用法示例。


在下文中一共展示了operator.setitem方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_setitem_mask_aligned

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_setitem_mask_aligned(self, data, as_callable, setter):
        ser = pd.Series(data)
        mask = np.zeros(len(data), dtype=bool)
        mask[:2] = True

        if as_callable:
            mask2 = lambda x: mask
        else:
            mask2 = mask

        if setter:
            # loc
            target = getattr(ser, setter)
        else:
            # Series.__setitem__
            target = ser

        operator.setitem(target, mask2, data[5:7])

        ser[mask2] = data[5:7]
        assert ser[0] == data[5]
        assert ser[1] == data[6] 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:24,代码来源:setitem.py

示例2: testTopLevelExtensionsForOptionalMessage

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def testTopLevelExtensionsForOptionalMessage(self):
    extendee_proto = unittest_pb2.TestAllExtensions()
    extension = unittest_pb2.optional_foreign_message_extension
    self.assertTrue(not extendee_proto.HasExtension(extension))
    self.assertEqual(0, extendee_proto.Extensions[extension].c)
    # As with normal (non-extension) fields, merely reading from the
    # thing shouldn't set the "has" bit.
    self.assertTrue(not extendee_proto.HasExtension(extension))
    extendee_proto.Extensions[extension].c = 23
    self.assertEqual(23, extendee_proto.Extensions[extension].c)
    self.assertTrue(extendee_proto.HasExtension(extension))
    # Save a reference here.
    foreign_message = extendee_proto.Extensions[extension]
    extendee_proto.ClearExtension(extension)
    self.assertTrue(foreign_message is not extendee_proto.Extensions[extension])
    # Setting a field on foreign_message now shouldn't set
    # any "has" bits on extendee_proto.
    foreign_message.c = 42
    self.assertEqual(42, foreign_message.c)
    self.assertTrue(foreign_message.HasField('c'))
    self.assertTrue(not extendee_proto.HasExtension(extension))
    # Shouldn't be allowed to do Extensions[extension] = 'a'
    self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions,
                      extension, 'a') 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:26,代码来源:reflection_test.py

示例3: testTopLevelExtensionsForRepeatedMessage

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def testTopLevelExtensionsForRepeatedMessage(self):
    extendee_proto = unittest_pb2.TestAllExtensions()
    extension = unittest_pb2.repeatedgroup_extension
    self.assertEqual(0, len(extendee_proto.Extensions[extension]))
    group = extendee_proto.Extensions[extension].add()
    group.a = 23
    self.assertEqual(23, extendee_proto.Extensions[extension][0].a)
    group.a = 42
    self.assertEqual(42, extendee_proto.Extensions[extension][0].a)
    group_list = extendee_proto.Extensions[extension]
    extendee_proto.ClearExtension(extension)
    self.assertEqual(0, len(extendee_proto.Extensions[extension]))
    self.assertTrue(group_list is not extendee_proto.Extensions[extension])
    # Shouldn't be allowed to do Extensions[extension] = 'a'
    self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions,
                      extension, 'a') 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:18,代码来源:reflection_test.py

示例4: setUp

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def setUp(self):
        """
        Patch the L{ls} module's time function so the results of L{lsLine} are
        deterministic.
        """
        self.now = 123456789
        def fakeTime():
            return self.now
        self.patch(ls, 'time', fakeTime)

        # Make sure that the timezone ends up the same after these tests as
        # it was before.
        if 'TZ' in os.environ:
            self.addCleanup(operator.setitem, os.environ, 'TZ', os.environ['TZ'])
            self.addCleanup(time.tzset)
        else:
            def cleanup():
                # os.environ.pop is broken!  Don't use it!  Ever!  Or die!
                try:
                    del os.environ['TZ']
                except KeyError:
                    pass
                time.tzset()
            self.addCleanup(cleanup) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:26,代码来源:test_cftp.py

示例5: test_immutable

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_immutable(self):
        orig = np.ma.masked
        assert_raises(np.ma.core.MaskError, operator.setitem, orig, (), 1)
        assert_raises(ValueError,operator.setitem, orig.data, (), 1)
        assert_raises(ValueError, operator.setitem, orig.mask, (), False)

        view = np.ma.masked.view(np.ma.MaskedArray)
        assert_raises(ValueError, operator.setitem, view, (), 1)
        assert_raises(ValueError, operator.setitem, view.data, (), 1)
        assert_raises(ValueError, operator.setitem, view.mask, (), False) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:12,代码来源:test_core.py

示例6: test_coercion_int

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_coercion_int(self):
        a_i = np.zeros((), int)
        assert_raises(MaskError, operator.setitem, a_i, (), np.ma.masked)
        assert_raises(MaskError, int, np.ma.masked) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:6,代码来源:test_core.py

示例7: test_coercion_float

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_coercion_float(self):
        a_f = np.zeros((), float)
        assert_warns(UserWarning, operator.setitem, a_f, (), np.ma.masked)
        assert_(np.isnan(a_f[()])) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:6,代码来源:test_core.py

示例8: test_setitem_scalar

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_setitem_scalar(self, data, setter):
        arr = pd.Series(data)
        setter = getattr(arr, setter)
        operator.setitem(setter, 0, data[1])
        assert arr[0] == data[1] 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:7,代码来源:setitem.py

示例9: testTopLevelExtensionsForRepeatedScalar

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def testTopLevelExtensionsForRepeatedScalar(self):
    extendee_proto = unittest_pb2.TestAllExtensions()
    extension = unittest_pb2.repeated_string_extension
    self.assertEqual(0, len(extendee_proto.Extensions[extension]))
    extendee_proto.Extensions[extension].append('foo')
    self.assertEqual(['foo'], extendee_proto.Extensions[extension])
    string_list = extendee_proto.Extensions[extension]
    extendee_proto.ClearExtension(extension)
    self.assertEqual(0, len(extendee_proto.Extensions[extension]))
    self.assertTrue(string_list is not extendee_proto.Extensions[extension])
    # Shouldn't be allowed to do Extensions[extension] = 'a'
    self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions,
                      extension, 'a') 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:15,代码来源:reflection_test.py

示例10: test_setslice_cint

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_setslice_cint(self):
        a = (c_int * 100)(*xrange(1100, 1200))
        b = range(1100, 1200)

        a[32:47] = range(32, 47)
        self.assertEqual(a[32:47], range(32, 47))
        a[32:47] = range(132, 147)
        self.assertEqual(a[32:47:], range(132, 147))
        a[46:31:-1] = range(232, 247)
        self.assertEqual(a[32:47:1], range(246, 231, -1))

        a[32:47] = range(1132, 1147)
        self.assertEqual(a[:], b)
        a[32:47:7] = range(3)
        b[32:47:7] = range(3)
        self.assertEqual(a[:], b)
        a[33::-3] = range(12)
        b[33::-3] = range(12)
        self.assertEqual(a[:], b)

        from operator import setslice, setitem

        # TypeError: int expected instead of str instance
        self.assertRaises(TypeError, setslice, a, 0, 5, "abcde")
        self.assertRaises(TypeError, setitem, a, slice(0, 5), "abcde")
        # TypeError: int expected instead of str instance
        self.assertRaises(TypeError, setslice, a, 0, 5, ["a", "b", "c", "d", "e"])
        self.assertRaises(TypeError, setitem, a, slice(0, 5),
                          ["a", "b", "c", "d", "e"])
        # TypeError: int expected instead of float instance
        self.assertRaises(TypeError, setslice, a, 0, 5, [1, 2, 3, 4, 3.14])
        self.assertRaises(TypeError, setitem, a, slice(0, 5),
                          [1, 2, 3, 4, 3.14])
        # ValueError: Can only assign sequence of same size
        self.assertRaises(ValueError, setslice, a, 0, 5, range(32))
        self.assertRaises(ValueError, setitem, a, slice(0, 5), range(32)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:38,代码来源:test_slicing.py

示例11: test_char_ptr

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_char_ptr(self):
        s = "abcdefghijklmnopqrstuvwxyz"

        dll = CDLL(_ctypes_test.__file__)
        dll.my_strdup.restype = POINTER(c_char)
        dll.my_free.restype = None
        res = dll.my_strdup(s)
        self.assertEqual(res[:len(s)], s)
        self.assertEqual(res[:3], s[:3])
        self.assertEqual(res[:len(s):], s)
        self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
        self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
        self.assertEqual(res[0:-1:-1], s[0::-1])

        import operator
        self.assertRaises(ValueError, operator.getitem,
                          res, slice(None, None, None))
        self.assertRaises(ValueError, operator.getitem,
                          res, slice(0, None, None))
        self.assertRaises(ValueError, operator.getitem,
                          res, slice(None, 5, -1))
        self.assertRaises(ValueError, operator.getitem,
                          res, slice(-5, None, None))

        self.assertRaises(TypeError, operator.setslice,
                          res, 0, 5, u"abcde")
        self.assertRaises(TypeError, operator.setitem,
                          res, slice(0, 5), u"abcde")
        dll.my_free(res)

        dll.my_strdup.restype = POINTER(c_byte)
        res = dll.my_strdup(s)
        self.assertEqual(res[:len(s)], range(ord("a"), ord("z")+1))
        self.assertEqual(res[:len(s):], range(ord("a"), ord("z")+1))
        dll.my_free(res) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:37,代码来源:test_slicing.py

示例12: test_wchar_ptr

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_wchar_ptr(self):
        s = u"abcdefghijklmnopqrstuvwxyz\0"

        dll = CDLL(_ctypes_test.__file__)
        dll.my_wcsdup.restype = POINTER(c_wchar)
        dll.my_wcsdup.argtypes = POINTER(c_wchar),
        dll.my_free.restype = None
        res = dll.my_wcsdup(s)
        self.assertEqual(res[:len(s)], s)
        self.assertEqual(res[:len(s):], s)
        self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
        self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])

        import operator
        self.assertRaises(TypeError, operator.setslice,
                          res, 0, 5, u"abcde")
        self.assertRaises(TypeError, operator.setitem,
                          res, slice(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:
            self.skipTest('Pointers to c_wchar are not supported')
        res = dll.my_wcsdup(s)
        tmpl = range(ord("a"), ord("z")+1)
        self.assertEqual(res[:len(s)-1], tmpl)
        self.assertEqual(res[:len(s)-1:], tmpl)
        self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1])
        self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
        dll.my_free(res)

################################################################ 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:39,代码来源:test_slicing.py

示例13: test_memory_not_writable

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_memory_not_writable(self):
        # Check the buffer object fetched by PyGetMemory isn't writable.
        test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6")
        c = array.array("b", test_data)
        addr, buflen = c.buffer_info()
        got = win32gui.PyGetMemory(addr, buflen)
        new = pywin32_testutil.str2bytes('\1')
        self.failUnlessRaises(TypeError, operator.setitem, got, 0, new) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:10,代码来源:test_win32gui.py

示例14: test_setitem

# 需要导入模块: import operator [as 别名]
# 或者: from operator import setitem [as 别名]
def test_setitem(self):
        a = range(3)
        self.assertRaises(TypeError, operator.setitem, a)
        self.assertRaises(TypeError, operator.setitem, a, None, None)
        self.assertTrue(operator.setitem(a, 0, 2) is None)
        self.assertTrue(a == [2, 1, 2])
        self.assertRaises(IndexError, operator.setitem, a, 4, 2) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_operator.py


注:本文中的operator.setitem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。