next()
should be wrapped in try-except
309
310
311def reconnect_fliparoo_outputs(graph: FormulaGraph, last_node: Node):
312 dummy = next(filter(lambda x: isinstance(x, DummyNode), graph.nodes))313 dummy.reconnect_outgoing_nodes(last_node)
314 graph.remove_node(dummy)
315 if any(map(lambda x: isinstance(x, DummyNode), graph.nodes)):
next()
should be wrapped in try-except
100 node.op = change_operator(node.op, OpType.Add)
101 return []
102 if node.is_pow:
103 exponent = next(104 filter(lambda n: isinstance(n, ConstantNode), node.incoming_nodes)
105 )
106 if exponent.value % 2 == 0:
next()
should be wrapped in try-except
289 const_par = next(filter(lambda x: isinstance(x, ConstantNode), node.incoming_nodes))
290 const_par_value = const_par.value
291
292 par = next(filter(lambda x: not isinstance(x, ConstantNode), node.incoming_nodes))293 i, ic, ip = (
294 original_graph.node_index(node),
295 original_graph.node_index(const_par),
next()
should be wrapped in try-except
198 graph = deepcopy(graph)
199 node = graph.nodes[i]
200
201 const_par = next(filter(lambda x: isinstance(x, ConstantNode), node.incoming_nodes))202 par = next(filter(lambda x: not isinstance(x, ConstantNode), node.incoming_nodes))
203 initial_node = CodeOpNode.from_str(node.result, par.result, OpType.Add, par.result)
204 graph.add_node(initial_node)
next()
should be wrapped in try-except
138 fliparoo = fliparoo.deepcopy()
139
140 first, last = fliparoo.first, fliparoo.last
141 inode = next(142 filter(lambda x: not isinstance(x, ConstantNode), first.incoming_nodes)
143 )
144 const_nodes: List[ConstantNode] = [
Calls to next()
should be inside try-except
block.
When the iterator is exhausted, StopIteration
exception is raised. When used inside a generator, this can cause unexpected behavior. If not handled, it will propagate out of the generator causing termination. PEP-479 has been accepted to fix this problem. It will modify the behavior of StopIteration
in generators.
Each call to next()
should be wrapped in a try-except
block to explicitly handle StopIteration
exceptions.
def get_team_structure(team):
for team in class:
students = iter(team)
# StopIteration exception will be raised if team doesn't have members.
leader = next(students)
members = [m for m in students]
yield leader, members
def print_teams(teams):
for leader, members in get_team_structure(teams):
print("Leader: "leader)
for member in members:
print(member)
def get_team_structure(team):
for team in class:
# StopIteration exception will be raised if team has no members.
students = iter(team)
# Added `try` block, to guard against the exception
try:
leader = next(students)
except StopIteration:
# team has no members, skip it
continue
members = [m for m in students]
yield leader, members
def print_teams(teams):
for leader, members in get_team_structure(teams):
print("Leader: "leader)
for member in members:
print(member)
...