本文整理汇总了Python中UnionFind.UnionFind.consolidate方法的典型用法代码示例。如果您正苦于以下问题:Python UnionFind.consolidate方法的具体用法?Python UnionFind.consolidate怎么用?Python UnionFind.consolidate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnionFind.UnionFind
的用法示例。
在下文中一共展示了UnionFind.consolidate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MySQLConsolidateUnionFindTestCase
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import consolidate [as 别名]
class MySQLConsolidateUnionFindTestCase(UnionFindTestCase):
def setUp(self):
with mysql_db:
cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
cur.execute('DROP TABLE IF EXISTS %s' % mysql_table)
self.uf = UnionFind()
def test_consolidate_mysql(self):
self.test_deunion()
self.uf.consolidate(mysql_db, mysql_table)
# instantiate a new unionfind instance that uses db stuff
uf2 = UnionFind(mysql_db, mysql_table, 'mysql')
cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
cur.execute('SELECT * FROM %s' % mysql_table)
for el in cur.fetchall():
# check if everything has been correctly stored into the db
assert el['_id'] in self.uf.parents
assert el['parent'] == self.uf.parents[el['_id']]['parent']
assert el['weight'] == self.uf.parents[el['_id']]['weight']
# check if the new unionfind structure, initialized from
# the db contents, actually contains the same elements
assert el['_id'] in uf2.parents
# does this guy have the same root?
assert self.uf[el['_id']] == uf2[el['_id']]
# and the same weight?
assert self.uf.parents[el['_id']]['weight'] == uf2.parents[el['_id']]['weight']
def test_consolidate_mysql_extra_fields(self):
self.uf.deunion()
self.uf.consolidate(mysql_db, mysql_table, gender='male', country='USA', state='NY')
# instantiate a new unionfind instance that uses db stuff
uf2 = UnionFind(mysql_db, mysql_table, 'mysql', gender='male', country='USA', state='NY')
cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
cur.execute('SELECT * FROM %s' % mysql_table)
for el in cur.fetchall():
# check if everything has been correctly stored into the db
assert el['_id'] in self.uf.parents
assert el['parent'] == self.uf.parents[el['_id']]['parent']
assert el['weight'] == self.uf.parents[el['_id']]['weight']
# check if the new unionfind structure, initialized from
# the db contents, actually contains the same elements
assert el['_id'] in uf2.parents
# does this guy have the same root?
assert self.uf[el['_id']] == uf2[el['_id']]
# and the same weight?
assert self.uf.parents[el['_id']]['weight'] == uf2.parents[el['_id']]['weight']
示例2: test
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import consolidate [as 别名]
def test(self):
uf = UnionFind()
uf.union('alpha', 'bravo', 'charlie', 'delta')
uf.consolidate(mysql_db, mysql_table, role='player', type='individual')
for el in self._select_star():
self.assertSetEqual(set(['role', 'type', '_id', 'parent', 'weight']), set(el.keys()))
self.assertEqual(el['role'], 'player')
self.assertEqual(el['type'], 'individual')
uf = UnionFind(mysql_db, mysql_table, 'mysql', role='player', type='organization')
uf.union('adams', 'boston', 'chicago')
for el in self._select_star():
if el['type'] == 'organization':
self.assertIn(el['_id'], ['adams', 'boston', 'chicago'])
elif el['type'] == 'individual':
self.assertIn(el['_id'], ['alpha', 'bravo', 'charlie', 'delta'])
else:
raise self.failureException