本文整理汇总了Python中cobra.DictList.union方法的典型用法代码示例。如果您正苦于以下问题:Python DictList.union方法的具体用法?Python DictList.union怎么用?Python DictList.union使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.DictList
的用法示例。
在下文中一共展示了DictList.union方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import union [as 别名]
#.........这里部分代码省略.........
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.assertIsNot(self.list[i], copied[i])
self.assertIsNot(v, copied.get_by_id(v.id))
def testPickle(self):
self.list.append(Object("test2"))
for protocol in range(HIGHEST_PROTOCOL):
pickle_str = dumps(self.list, protocol=protocol)
copied = loads(pickle_str)
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.assertIsNot(self.list[i], copied[i])
self.assertIsNot(v, copied.get_by_id(v.id))
def testQuery(self):
obj2 = Object("test2")
obj2.name = "foobar1"
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("foo", "name") # matches only test2
self.assertEqual(len(result), 1)
self.assertEqual(result[0], obj2)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
# test with a regular expression
result = self.list.query(re.compile("test[0-9]"))
self.assertEqual(len(result), 2)
result = self.list.query(re.compile("test[29]"))
self.assertEqual(len(result), 1)
# test query of name
result = self.list.query(re.compile("foobar."), "name")
self.assertEqual(len(result), 1)
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)
self.assertEqual(len(obj_list), 7)
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)
self.assertEqual(len(obj_list), 5)
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)
self.assertEqual(len(obj_list), 4)
removed = obj_list.pop()
self.assertEqual(removed.id, "test9")
self.assertNotIn(removed.id, obj_list)
self.assertEqual(len(obj_list), 3)
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")])
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)
def testDir(self):
"""makes sure tab complete will work"""
attrs = dir(self.list)
self.assertIn("test1", attrs)
self.assertIn("_dict", attrs) # attribute of DictList
def testUnion(self):
self.list.union([Object("test1"), Object("test2")])
# should only add 1 element
self.assertEqual(len(self.list), 2)
self.assertEqual(self.list.index("test2"), 1)