Block Structure API: Replace has_block with __contains__
This commit is contained in:
@@ -79,6 +79,7 @@ class BlockStructure(object):
|
||||
Returns the parents of the block identified by the given
|
||||
usage_key.
|
||||
|
||||
|
||||
Arguments:
|
||||
usage_key - The usage key of the block whose parents
|
||||
are to be returned.
|
||||
@@ -86,7 +87,7 @@ class BlockStructure(object):
|
||||
Returns:
|
||||
[UsageKey] - A list of usage keys of the block's parents.
|
||||
"""
|
||||
return self._block_relations[usage_key].parents if self.has_block(usage_key) else []
|
||||
return self._block_relations[usage_key].parents if usage_key in self else []
|
||||
|
||||
def get_children(self, usage_key):
|
||||
"""
|
||||
@@ -100,7 +101,7 @@ class BlockStructure(object):
|
||||
Returns:
|
||||
[UsageKey] - A list of usage keys of the block's children.
|
||||
"""
|
||||
return self._block_relations[usage_key].children if self.has_block(usage_key) else []
|
||||
return self._block_relations[usage_key].children if usage_key in self else []
|
||||
|
||||
def set_root_block(self, usage_key):
|
||||
"""
|
||||
@@ -117,7 +118,7 @@ class BlockStructure(object):
|
||||
self.root_block_usage_key = usage_key
|
||||
self._block_relations[usage_key].parents = []
|
||||
|
||||
def has_block(self, usage_key):
|
||||
def __contains__(self, usage_key):
|
||||
"""
|
||||
Returns whether a block with the given usage_key is in this
|
||||
block structure.
|
||||
|
||||
@@ -57,7 +57,7 @@ class BlockStructureManager(object):
|
||||
# Override the root_block_usage_key so traversals start at the
|
||||
# requested location. The rest of the structure will be pruned
|
||||
# as part of the transformation.
|
||||
if not block_structure.has_block(starting_block_usage_key):
|
||||
if starting_block_usage_key not in block_structure:
|
||||
raise UsageKeyNotInBlockStructure(
|
||||
"The requested usage_key '{0}' is not found in the block_structure with root '{1}'",
|
||||
unicode(starting_block_usage_key),
|
||||
|
||||
@@ -219,7 +219,7 @@ class ChildrenMapTestMixin(object):
|
||||
for block_key, children in enumerate(children_map):
|
||||
# Verify presence
|
||||
self.assertEquals(
|
||||
block_structure.has_block(block_key),
|
||||
block_key in block_structure,
|
||||
block_key not in missing_blocks,
|
||||
'Expected presence in block_structure for block_key {} to match absence in missing_blocks.'.format(
|
||||
unicode(block_key)
|
||||
|
||||
@@ -37,10 +37,10 @@ class TestBlockStructure(TestCase, ChildrenMapTestMixin):
|
||||
for child, parents in enumerate(self.get_parents_map(children_map)):
|
||||
self.assertSetEqual(set(block_structure.get_parents(child)), set(parents))
|
||||
|
||||
# has_block
|
||||
# __contains__
|
||||
for node in range(len(children_map)):
|
||||
self.assertTrue(block_structure.has_block(node))
|
||||
self.assertFalse(block_structure.has_block(len(children_map) + 1))
|
||||
self.assertIn(node, block_structure)
|
||||
self.assertNotIn(len(children_map) + 1, block_structure)
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
|
||||
Reference in New Issue
Block a user