本文整理汇总了Python中cobra.DictList.index方法的典型用法代码示例。如果您正苦于以下问题:Python DictList.index方法的具体用法?Python DictList.index怎么用?Python DictList.index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.DictList
的用法示例。
在下文中一共展示了DictList.index方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSortandReverse
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def testSortandReverse(self):
dl = DictList(Object("test%d" % (i)) for i in reversed(range(10)))
self.assertEqual(dl[0].id, "test9")
dl.sort()
self.assertEqual(len(dl), 10)
self.assertEqual(dl[0].id, "test0")
self.assertEqual(dl.index("test0"), 0)
dl.reverse()
self.assertEqual(dl[0].id, "test9")
self.assertEqual(dl.index("test0"), 9)
示例2: testSet
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def testSet(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(10))
obj_list[4] = Object("testa")
self.assertEqual(obj_list.index("testa"), 4)
self.assertEqual(obj_list[4].id, "testa")
obj_list[5:7] = [Object("testb"), Object("testc")]
self.assertEqual(obj_list.index("testb"), 5)
self.assertEqual(obj_list[5].id, "testb")
self.assertEqual(obj_list.index("testc"), 6)
self.assertEqual(obj_list[6].id, "testc")
示例3: test_sort_and_reverse
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def test_sort_and_reverse(self):
dl = DictList(Object("test%d" % (i)) for i in reversed(range(10)))
assert dl[0].id == "test9"
dl.sort()
assert len(dl) == 10
assert dl[0].id == "test0"
assert dl.index("test0") == 0
dl.reverse()
assert dl[0].id == "test9"
assert dl.index("test0") == 9
示例4: testRemoval
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def testRemoval(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
del obj_list[3]
self.assertNotIn("test5", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
del obj_list[3:5]
self.assertNotIn("test6", obj_list)
self.assertNotIn("test7", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
removed = obj_list.pop(1)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(removed.id, "test3")
self.assertNotIn("test3", obj_list)
示例5: testSet
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def testSet(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(10))
obj_list[4] = Object("testa")
self.assertEqual(obj_list.index("testa"), 4)
self.assertEqual(obj_list[4].id, "testa")
obj_list[5:7] = [Object("testb"), Object("testc")]
self.assertEqual(obj_list.index("testb"), 5)
self.assertEqual(obj_list[5].id, "testb")
self.assertEqual(obj_list.index("testc"), 6)
self.assertEqual(obj_list[6].id, "testc")
# Even if the object is unique, if it is present twice in the new
# list, it should still raise an exception
self.assertRaises(ValueError, obj_list.__setitem__, slice(5, 7),
[Object("testd"), Object("testd")])
示例6: test_set
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def test_set(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(10))
obj_list[4] = Object("testa")
assert obj_list.index("testa") == 4
assert obj_list[4].id == "testa"
obj_list[5:7] = [Object("testb"), Object("testc")]
assert obj_list.index("testb") == 5
assert obj_list[5].id == "testb"
assert obj_list.index("testc") == 6
assert obj_list[6].id == "testc"
# Even if the object is unique, if it is present twice in the new
# list, it should still raise an exception
with pytest.raises(ValueError):
obj_list.__setitem__(slice(5, 7),
[Object("testd"), Object("testd")])
示例7: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append,
Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testDeepcopy(self):
from copy import deepcopy
copied = deepcopy(self.list)
for i, v in enumerate(self.list):
assert self.list[i].id == copied[i].id
assert self.list[i] is not copied[i]
def testQuery(self):
obj2 = Object("test2")
self.list.append(obj2)
result = self.list.query("test1") # matches only test1
self.assertEqual(len(result), 1)
self.assertEqual(result[0], self.obj)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
示例8: testInitCopy
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def testInitCopy(self):
self.list.append(Object("test2"))
copied = DictList(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIs(self.list[i], copied[i])
self.assertIs(v, copied.get_by_id(v.id))
示例9: test_removal
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def test_removal(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
del obj_list[3]
assert "test5" not in obj_list
assert obj_list.index(obj_list[-1]) == len(obj_list) - 1
assert len(obj_list) == 7
del obj_list[3:5]
assert "test6" not in obj_list
assert "test7" not in obj_list
assert obj_list.index(obj_list[-1]) == len(obj_list) - 1
assert len(obj_list) == 5
removed = obj_list.pop(1)
assert obj_list.index(obj_list[-1]) == len(obj_list) - 1
assert removed.id == "test3"
assert "test3" not in obj_list
assert len(obj_list) == 4
removed = obj_list.pop()
assert removed.id == "test9"
assert removed.id not in obj_list
assert len(obj_list) == 3
示例10: test_init_copy
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
def test_init_copy(self, dict_list):
obj, test_list = dict_list
test_list.append(Object("test2"))
copied = DictList(test_list)
assert test_list is not copied
assert isinstance(copied, test_list.__class__)
assert len(test_list) == len(copied)
for i, v in enumerate(test_list):
assert test_list[i].id == copied[i].id
assert i == copied.index(v.id)
assert test_list[i] is copied[i]
assert v is copied.get_by_id(v.id)
示例11: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append,
Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
from os import name as __name
if __name == 'java':
warn('\t\n**cobra.test.unit_tests.testAdd does not yet work with %s'%__name)
return
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testDeepcopy(self):
from copy import deepcopy
copied = deepcopy(self.list)
for i, v in enumerate(self.list):
assert self.list[i].id == copied[i].id
assert self.list[i] is not copied[i]
示例12: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append, Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
示例13: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testContains(self):
self.assertIn(self.obj, self.list)
self.assertIn(self.obj.id, self.list)
self.assertNotIn(Object("not_in"), self.list)
self.assertNotIn('not_in', self.list)
def testIndex(self):
self.assertEqual(self.list.index("test1"), 0)
self.assertEqual(self.list.index(self.obj), 0)
self.assertRaises(ValueError, self.list.index, "f")
self.assertRaises(ValueError, self.list.index, Object("f"))
# ensure trying to index with an object that is a different object
# also raises an error
self.assertRaises(ValueError, self.list.index, Object("test1"))
def testIndependent(self):
a = DictList([Object("o1"), Object("o2")])
b = DictList()
self.assertIn("o1", a)
self.assertNotIn("o1", b)
b.append(Object("o3"))
self.assertNotIn("o3", a)
self.assertIn("o3", b)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append, Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertIs(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testInsert(self):
obj2 = Object("a")
self.list.insert(0, obj2)
self.assertEqual(self.list.index(obj2), 0)
self.assertEqual(self.list.index("test1"), 1)
self.assertIs(self.list.get_by_id("a"), obj2)
self.assertEqual(len(self.list), 2)
self.assertRaises(ValueError, self.list.append, obj2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
self.assertRaises(ValueError, self.list.extend, [Object("test1")])
# Even if the object is unique, if it is present twice in the new
# list, it should still raise an exception
self.assertRaises(ValueError, self.list.extend,
[Object("testd"), Object("testd")])
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertIsNot(sum, self.list)
self.assertIsNot(sum, obj_list)
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testInitCopy(self):
self.list.append(Object("test2"))
copied = DictList(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIs(self.list[i], copied[i])
self.assertIs(v, copied.get_by_id(v.id))
def testSlice(self):
self.list.append(Object("test2"))
self.list.append(Object("test3"))
sliced = self.list[:-1]
self.assertIsNot(self.list, sliced)
self.assertIsInstance(sliced, self.list.__class__)
self.assertEqual(len(self.list), len(sliced) + 1)
#.........这里部分代码省略.........
示例14: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testContains(self):
self.assertIn(self.obj, self.list)
self.assertIn(self.obj.id, self.list)
self.assertNotIn(Object("not_in"), self.list)
self.assertNotIn('not_in', self.list)
def testIndex(self):
self.assertEqual(self.list.index("test1"), 0)
self.assertEqual(self.list.index(self.obj), 0)
self.assertRaises(ValueError, self.list.index, "f")
self.assertRaises(ValueError, self.list.index, Object("f"))
def testIndependent(self):
a = DictList([Object("o1"), Object("o2")])
b = DictList()
self.assertIn("o1", a)
self.assertNotIn("o1", b)
b.append(Object("o3"))
self.assertNotIn("o3", a)
self.assertIn("o3", b)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append, Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
self.assertRaises(ValueError, self.list.extend, [Object("test1")])
# Even if the object is unique, if it is present twice in the new
# list, it should still raise an exception
self.assertRaises(ValueError, self.list.extend,
[Object("testd"), Object("testd")])
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertIsNot(sum, self.list)
self.assertIsNot(sum, obj_list)
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testInitCopy(self):
self.list.append(Object("test2"))
copied = DictList(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIs(self.list[i], copied[i])
self.assertIs(v, copied.get_by_id(v.id))
def testSlice(self):
self.list.append(Object("test2"))
self.list.append(Object("test3"))
sliced = self.list[:-1]
self.assertIsNot(self.list, sliced)
self.assertIsInstance(sliced, self.list.__class__)
self.assertEqual(len(self.list), len(sliced) + 1)
for i, v in enumerate(sliced):
self.assertEqual(self.list[i].id, sliced[i].id)
self.assertEqual(i, sliced.index(v.id))
self.assertIs(self.list[i], sliced[i])
self.assertIs(self.list[i], sliced.get_by_id(v.id))
def testCopy(self):
self.list.append(Object("test2"))
copied = copy(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
#.........这里部分代码省略.........
示例15: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import index [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testIndependent(self):
a = DictList([Object("o1"), Object("o2")])
b = DictList()
self.assertIn("o1", a)
self.assertNotIn("o1", b)
b.append(Object("o3"))
self.assertNotIn("o3", a)
self.assertIn("o3", b)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append,
Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testDeepcopy(self):
from copy import deepcopy
copied = deepcopy(self.list)
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertIsNot(self.list[i], copied[i])
def testQuery(self):
obj2 = Object("test2")
self.list.append(obj2)
result = self.list.query("test1") # matches only test1
self.assertEqual(len(result), 1)
self.assertEqual(result[0], self.obj)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
def testRemoval(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
del obj_list[3]
self.assertNotIn("test5", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
del obj_list[3:5]
self.assertNotIn("test6", obj_list)
self.assertNotIn("test7", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
removed = obj_list.pop(1)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(removed.id, "test3")
self.assertNotIn("test3", obj_list)
def testSet(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(10))
obj_list[4] = Object("testa")
self.assertEqual(obj_list.index("testa"), 4)
self.assertEqual(obj_list[4].id, "testa")
obj_list[5:7] = [Object("testb"), Object("testc")]
self.assertEqual(obj_list.index("testb"), 5)
self.assertEqual(obj_list[5].id, "testb")
self.assertEqual(obj_list.index("testc"), 6)
self.assertEqual(obj_list[6].id, "testc")