Call to
next()
should be wrapped in try-except
459
460 # Default to predict() method if present
461 if method is None and any(m.name == "predict" for m in package.methods):
462 method = next(x for x in package.methods if x.name == "predict")463
464 # Fall back to score() method if present
465 if method is None and any(m.name == "score" for m in package.methods):
Call to
next()
should be wrapped in try-except
463
464 # Fall back to score() method if present
465 if method is None and any(m.name == "score" for m in package.methods):
466 method = next(x for x in package.methods if x.name == "score")467
468 # Assume first method is init(), so fall back to next method
469 if method is None and len(package.methods) > 1:
Call to
next()
should be wrapped in try-except
484
485 # If passing column data into Python as an array, need extra assignment statements to set values
486 if array_input and self.column_names is not None:
487 array = next(filter(lambda v: v.is_array, self.variables))488 var_assignments = [
489 "{}[{}] = {};".format(array.name, i + 1, self.column_names[i])
490 for i in range(min(array.size, len(self.column_names)))
Call to
next()
should be wrapped in try-except
1702 # Go ahead and add the items that were initially returned.
1703 # Do this by "paging" so iterator remains at the correct spot.
1704 for _ in range(len(obj["items"])):
1705 self.append(next(self._paged_items))1706
1707 # Assume that server has more items available
1708 self._has_more = True
Call to
next()
should be wrapped in try-except
1627 def __next__(self):
1628 # Get next page of items if we're currently out
1629 if not self._cache:
1630 self._cache = next(self._pager)1631
1632 # Return the next item
1633 if self._cache:
Description
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.
Bad practice
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)
Recommended:
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)
...