Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_candidate_dict(self, key, crun, use_upper_bound=True):
# First find all nodes where the key matches the (short) name of the node
if crun is None:
return self._nodes_and_leaves[key]
# This can be false in case of links which are not added to the run sorted nodes and leaves
else:
temp_dict = {}
if crun in self._nodes_and_leaves_runs_sorted[key]:
temp_dict = self._nodes_and_leaves_runs_sorted[key][crun]
if use_upper_bound and len(temp_dict) > FAST_UPPER_BOUND:
raise pex.TooManyGroupsError('Too many nodes')
temp_dict2 = {}
if 'trajectory' in self._nodes_and_leaves_runs_sorted[key]:
temp_dict2 = self._nodes_and_leaves_runs_sorted[key]['trajectory']
if use_upper_bound and len(temp_dict) + len(temp_dict2) > FAST_UPPER_BOUND:
raise pex.TooManyGroupsError('Too many nodes')
return ChainMap(temp_dict, temp_dict2)
If search cannot performed fast enough, an alternative search method is needed.
NotUniqueNodeError:
If several nodes match the key criterion
"""
parent_full_name = node.v_full_name
candidate_dict = self._get_candidate_dict(key, crun)
# If there are to many potential candidates sequential search might be too slow
if with_links and len(candidate_dict) > 1:
raise pex.TooManyGroupsError('More than one target, '
'I might possibly be missing links.')
elif len(candidate_dict) > FAST_UPPER_BOUND:
raise pex.TooManyGroupsError('Too many nodes')
# Next check if the found candidates could be reached from the parent node
result_node = None
for goal_name in candidate_dict:
# Check if we have found a matching node
if goal_name.startswith(parent_full_name):
# In case of several solutions raise an error:
if result_node is not None:
raise pex.NotUniqueNodeError('Node `%s` has been found more than once,'
'full name of first occurrence is `%s` and of'
"""
if key in self._links_count:
return
parent_full_name = node.v_full_name
starting_depth = node.v_depth
candidate_dict = self._get_candidate_dict(key, crun)
# If there are to many potential candidates sequential search might be too slow
if with_links:
upper_bound = 1
else:
upper_bound = FAST_UPPER_BOUND
if len(candidate_dict) > upper_bound:
raise pex.TooManyGroupsError('Too many nodes')
# Next check if the found candidates could be reached from the parent node
result_node = None
for goal_name in candidate_dict:
# Check if we have found a matching node
if goal_name.startswith(parent_full_name):
candidate = candidate_dict[goal_name]
if candidate.v_depth - starting_depth <= max_depth:
# In case of several solutions raise an error:
if result_node is not None:
raise pex.NotUniqueNodeError('Node `%s` has been found more than once, '
'full name of first occurrence is `%s` and of'
'second `%s`'
% (key, goal_name, result_node.v_full_name))
result_node = candidate
NotUniqueNodeError:
If several nodes match the key criterion
"""
parent_full_name = node.v_full_name
candidate_dict = self._get_candidate_dict(key, crun)
# If there are to many potential candidates sequential search might be too slow
if with_links and len(candidate_dict) > 1:
raise pex.TooManyGroupsError('More than one target, '
'I might possibly be missing links.')
elif len(candidate_dict) > FAST_UPPER_BOUND:
raise pex.TooManyGroupsError('Too many nodes')
# Next check if the found candidates could be reached from the parent node
result_node = None
for goal_name in candidate_dict:
# Check if we have found a matching node
if goal_name.startswith(parent_full_name):
# In case of several solutions raise an error:
if result_node is not None:
raise pex.NotUniqueNodeError('Node `%s` has been found more than once,'
'full name of first occurrence is `%s` and of'
'second `%s`'
% (key, goal_name, result_node.v_full_name))
def _get_candidate_dict(self, key, crun, use_upper_bound=True):
# First find all nodes where the key matches the (short) name of the node
try:
if crun is None:
return self._nodes_and_leaves[key]
# This can be false in case of links which are not added to the run sorted nodes and leaves
else:
temp_dict = {}
if crun in self._nodes_and_leaves_runs_sorted[key]:
temp_dict = self._nodes_and_leaves_runs_sorted[key][crun]
if use_upper_bound and len(temp_dict) > FAST_UPPER_BOUND:
raise pex.TooManyGroupsError('Too many nodes')
temp_dict2 = {}
if 'trajectory' in self._nodes_and_leaves_runs_sorted[key]:
temp_dict2 = self._nodes_and_leaves_runs_sorted[key]['trajectory']
if use_upper_bound and len(temp_dict) + len(temp_dict2) > FAST_UPPER_BOUND:
raise pex.TooManyGroupsError('Too many nodes')
return ChainMap(temp_dict, temp_dict2)
except KeyError:
# We end up here if `key` is actually a link
return {}
"""
# If we find it directly there is no need for an exhaustive search
if key in node._children and (with_links or key not in node._links):
return node._children[key], 1
crun = self._run_filter
# First the very fast search is tried that does not need tree traversal.
if max_depth == float('inf'):
try:
result_node = self._very_fast_search(node, key, crun, with_links)
if result_node is not None:
return result_node, 0
except pex.TooManyGroupsError:
pass
except pex.NotUniqueNodeError:
pass
# Slowly traverse the entire tree
nodes_iterator = self._iter_nodes(node, recursive=True,
max_depth=max_depth, in_search=True,
with_links=with_links)
result_node = None
result_depth = float('inf')
for depth, name, child in nodes_iterator:
if depth > result_depth:
# We can break here because we enter a deeper stage of the tree and we
# cannot find matching node of the same depth as the one we found
break