Skip to content

Commit 1f22c70

Browse files
authored
Update graphlib.py
1 parent bdd590f commit 1f22c70

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

Lib/graphlib.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class _NodeInfo:
1010
__slots__ = "node", "npredecessors", "successors"
11-
11+
1212
def __init__(self, node):
1313
# The node this class is augmenting.
1414
self.node = node
@@ -25,12 +25,12 @@ def __init__(self, node):
2525

2626
class CycleError(ValueError):
2727
"""Subclass of ValueError raised by TopologicalSorter when cycles exist in the graph.
28-
29-
When a cycle is detected, only one arbitrary cycle will be reported and included in the
30-
exception. The cycle can be accessed via the second element in the *args*
31-
attribute of the exception instance. The cycle is represented as a list of nodes
32-
where each node is a direct predecessor of the next node in the list. The first
33-
and last nodes in the list are identical to indicate the cycle's completion.
28+
29+
When a cycle is detected, only one arbitrary cycle will be reported and included in the
30+
exception. The cycle can be accessed via the second element in the *args*
31+
attribute of the exception instance. The cycle is represented as a list of nodes
32+
where each node is a direct predecessor of the next node in the list. The first
33+
and last nodes in the list are identical to indicate the cycle's completion.
3434
"""
3535

3636
pass
@@ -83,7 +83,6 @@ def add(self, node, *predecessors):
8383

8484
def prepare(self):
8585
"""Mark the graph as finished and check for cycles in the graph.
86-
8786
This method must be called exactly once, after all nodes are added and
8887
before calling get_ready(). It checks for cycles and initializes the
8988
internal data structures for processing.
@@ -105,14 +104,13 @@ def prepare(self):
105104
# if the user wants to catch the CycleError, that's fine,
106105
# they can continue using the instance to grab as many
107106
# nodes as possible before cycles block more progress
108-
cycle = self._find_cycle()
109-
if cycle:
110-
raise CycleError(f"nodes are in a cycle", cycle)
107+
if cycle := self._find_cycle()
108+
raise CycleError(f"nodes are in a cycle: {cycle}")
111109

112110
def get_ready(self):
113111
"""Return a tuple of all the ready nodes.
114112
115-
Initially it returns all nodes with no predecessors; once those are marked
113+
Initially, it returns all nodes with no predecessors; once those are marked
116114
as processed by calling "done", further calls will return all new nodes that
117115
have all their predecessors already processed. Once no more progress can be made,
118116
empty tuples are returned.
@@ -138,7 +136,7 @@ def get_ready(self):
138136
def is_active(self):
139137
"""Return ``True`` if more progress can be made and ``False`` otherwise.
140138
141-
Progress can be made if cycles do not block the resolution and either there
139+
Progress can be made if cycles do not block the resolution and if there
142140
are still nodes ready that haven't yet been returned by "get_ready" or the
143141
number of nodes marked "done" is less than the number that have been returned
144142
by "get_ready".

0 commit comments

Comments
 (0)