Python Graphlib Module get_ready( )














































Python Graphlib Module get_ready( )




In the previous two articles, we have seen about static_order( ) in Graphlib module.


 In this article, we are going to see about another method get_ready( )


get_ready( )

This method returns a tuple with all the nodes that are ready.  

  *Initially get_ready( ) returns all nodes with no predecessors  and       after  by calling TopologicalSorter.done( ),mark them as processed.

  *Further calls of get_ready( ) will return all new nodes that have all     their  predecessors already processed.





Codesnippet:

from graphlib import TopologicalSorter
  
graph = {1 : {6},2: {3}, 3: {1}, 4: {0, 1}, 5: {0, 2}}
t=TopologicalSorter(graph)
t.prepare( )
resulttuple=t.get_ready( )
print(resulttuple)

Output:
(6, 0)

->If empty tuples are returned that indicates that no progress can be   made

->get_ready( ) raises ValueError if called without  calling prepare( ) 
previously.
Example:
from graphlib import TopologicalSorter
  
graph = {1 : {6},2: {3}, 3: {1}, 4: {0, 1}, 5: {0, 2}}
t=TopologicalSorter(graph)


r=t.get_ready( )
print(r)
Error:
Traceback (most recent call last):
  File "C:\Users\findm\AppData\Local\Programs\Python\Python39\graphlib.py", line 9, in <module>
    r=t.get_ready( )
  File "C:\Users\findm\AppData\Local\Programs\Python\Python39\lib\graphlib.py", line 117, in get_ready
    raise ValueError("prepare() must be called first")
ValueError: prepare() must be called first






Comments