/usr/lib64/python3.9
NameSizeModeActions
asyncio/-0755rm
collections/-0755rm
concurrent/-0755rm
config-3.9-x86_64-linux-gnu/-0755rm
ctypes/-0755rm
curses/-0755rm
dbm/-0755rm
distutils/-0755rm
email/-0755rm
encodings/-0755rm
ensurepip/-0755rm
html/-0755rm
http/-0755rm
importlib/-0755rm
json/-0755rm
lib-dynload/-0755rm
lib2to3/-0755rm
logging/-0755rm
multiprocessing/-0755rm
pydoc_data/-0755rm
site-packages/-0755rm
sqlite3/-0755rm
unittest/-0755rm
urllib/-0755rm
venv/-0755rm
wsgiref/-0755rm
xml/-0755rm
xmlrpc/-0755rm
zoneinfo/-0755rm
__pycache__/-0755rm
abc.py49200644editdlrm
aifc.py326050644editdlrm
antigravity.py5000644editdlrm
argparse.py981190644editdlrm
ast.py562560644editdlrm
asynchat.py113210644editdlrm
asyncore.py201020644editdlrm
base64.py198590755editdlrm
bdb.py313890644editdlrm
binhex.py147840644editdlrm
bisect.py23500644editdlrm
bz2.py124470644editdlrm
calendar.py248320644editdlrm
cgi.py339320755editdlrm
cgitb.py120960644editdlrm
chunk.py54350644editdlrm
cmd.py148600644editdlrm
code.py106220644editdlrm
codecs.py366730644editdlrm
codeop.py63260644editdlrm
colorsys.py40640644editdlrm
compileall.py201050644editdlrm
configparser.py545840644editdlrm
contextlib.py246240644editdlrm
contextvars.py1290644editdlrm
copy.py86500644editdlrm
copyreg.py72740644editdlrm
cProfile.py63450755editdlrm
crypt.py38190644editdlrm
csv.py161440644editdlrm
dataclasses.py495860644editdlrm
datetime.py891770644editdlrm
decimal.py3200644editdlrm
difflib.py833070644editdlrm
dis.py205700644editdlrm
doctest.py1045680644editdlrm
enum.py394400644editdlrm
filecmp.py100240644editdlrm
fileinput.py147910644editdlrm
fnmatch.py60040644editdlrm
formatter.py151430644editdlrm
fractions.py243230644editdlrm
ftplib.py354960644editdlrm
functools.py388810644editdlrm
genericpath.py52460644editdlrm
getopt.py74890644editdlrm
getpass.py59900644editdlrm
gettext.py272660644editdlrm
glob.py58230644editdlrm
graphlib.py95730644editdlrm
gzip.py217720644editdlrm
hashlib.py80690644editdlrm
heapq.py228770644editdlrm
hmac.py80430644editdlrm
imaplib.py552560644editdlrm
imghdr.py38080644editdlrm
imp.py105360644editdlrm
inspect.py1182350644editdlrm
io.py35410644editdlrm
ipaddress.py809240644editdlrm
keyword.py10470644editdlrm
LICENSE.txt139370644editdlrm
linecache.py54610644editdlrm
locale.py782710644editdlrm
lzma.py132310644editdlrm
mailbox.py787940644editdlrm
mailcap.py91160644editdlrm
mimetypes.py215640644editdlrm
modulefinder.py244010644editdlrm
netrc.py55660644editdlrm
nntplib.py410230644editdlrm
ntpath.py264600644editdlrm
nturl2path.py28870644editdlrm
numbers.py103380644editdlrm
opcode.py56600644editdlrm
operator.py107510644editdlrm
optparse.py603690644editdlrm
os.py390650644editdlrm
pathlib.py511340644editdlrm
pdb.py632370755editdlrm
pickle.py649200644editdlrm
pickletools.py934860644editdlrm
pipes.py89160644editdlrm
pkgutil.py242760644editdlrm
platform.py406010755editdlrm
plistlib.py282480644editdlrm
poplib.py151980644editdlrm
posixpath.py161460644editdlrm
pprint.py225270644editdlrm
profile.py228810755editdlrm
pstats.py293260644editdlrm
pty.py48070644editdlrm
pyclbr.py152550644editdlrm
pydoc.py1095990755editdlrm
py_compile.py82030644editdlrm
queue.py114960644editdlrm
quopri.py72660755editdlrm
random.py314840644editdlrm
re.py158610644editdlrm
reprlib.py52670644editdlrm
rlcompleter.py76480644editdlrm
runpy.py130840644editdlrm
sched.py64420644editdlrm
secrets.py20360644editdlrm
selectors.py195360644editdlrm
shelve.py85270644editdlrm
shlex.py135010644editdlrm
shutil.py530300644editdlrm
signal.py24380644editdlrm
site.py220850644editdlrm
smtpd.py348210755editdlrm
smtplib.py454050755editdlrm
sndhdr.py70990644editdlrm
socket.py369150644editdlrm
socketserver.py272960644editdlrm
sre_compile.py279730644editdlrm
sre_constants.py71770644editdlrm
sre_parse.py407790644editdlrm
ssl.py526220644editdlrm
stat.py54850644editdlrm
statistics.py380670644editdlrm
string.py105660644editdlrm
stringprep.py129170644editdlrm
struct.py2570644editdlrm
subprocess.py835640644editdlrm
sunau.py181580644editdlrm
symbol.py22810644editdlrm
symtable.py79050644editdlrm
sysconfig.py255570644editdlrm
tabnanny.py114060755editdlrm
tarfile.py1129390755editdlrm
telnetlib.py232540644editdlrm
tempfile.py279630644editdlrm
textwrap.py194070644editdlrm
this.py10030644editdlrm
threading.py541760644editdlrm
timeit.py134800755editdlrm
token.py23680644editdlrm
tokenize.py258830644editdlrm
trace.py292070755editdlrm
traceback.py246600644editdlrm
tracemalloc.py180470644editdlrm
tty.py8790644editdlrm
types.py97850644editdlrm
typing.py770440644editdlrm
uu.py72770644editdlrm
uuid.py273240644editdlrm
warnings.py196880644editdlrm
wave.py180040644editdlrm
weakref.py215600644editdlrm
webbrowser.py240830755editdlrm
xdrlib.py59130644editdlrm
zipapp.py75350644editdlrm
zipfile.py888990644editdlrm
zipimport.py307650644editdlrm
_aix_support.py33890644editdlrm
_bootlocale.py18010644editdlrm
_bootsubprocess.py26750644editdlrm
_collections_abc.py293740644editdlrm
_compat_pickle.py87490644editdlrm
_compression.py53400644editdlrm
_markupbase.py146230644editdlrm
_osx_support.py217730644editdlrm
_pydecimal.py2286660644editdlrm
_pyio.py933160644editdlrm
_py_abc.py61890644editdlrm
_sitebuiltins.py31150644editdlrm
_strptime.py252770644editdlrm
_sysconfigdata__linux_x86_64-linux-gnu.py412360644editdlrm
_threading_local.py72200644editdlrm
_weakrefset.py59230644editdlrm
__future__.py51470644editdlrm
__phello__.foo.py640644editdlrm
Edit: /usr/lib64/python3.9/graphlib.py (9573B)
__all__ = ["TopologicalSorter", "CycleError"] _NODE_OUT = -1 _NODE_DONE = -2 class _NodeInfo: __slots__ = "node", "npredecessors", "successors" def __init__(self, node): # The node this class is augmenting. self.node = node # Number of predecessors, generally >= 0. When this value falls to 0, # and is returned by get_ready(), this is set to _NODE_OUT and when the # node is marked done by a call to done(), set to _NODE_DONE. self.npredecessors = 0 # List of successor nodes. The list can contain duplicated elements as # long as they're all reflected in the successor's npredecessors attribute. self.successors = [] class CycleError(ValueError): """Subclass of ValueError raised by TopologicalSorter.prepare if cycles exist in the working graph. If multiple cycles exist, only one undefined choice among them will be reported and included in the exception. The detected cycle can be accessed via the second element in the *args* attribute of the exception instance and consists in a list of nodes, such that each node is, in the graph, an immediate predecessor of the next node in the list. In the reported list, the first and the last node will be the same, to make it clear that it is cyclic. """ pass class TopologicalSorter: """Provides functionality to topologically sort a graph of hashable nodes""" def __init__(self, graph=None): self._node2info = {} self._ready_nodes = None self._npassedout = 0 self._nfinished = 0 if graph is not None: for node, predecessors in graph.items(): self.add(node, *predecessors) def _get_nodeinfo(self, node): if (result := self._node2info.get(node)) is None: self._node2info[node] = result = _NodeInfo(node) return result def add(self, node, *predecessors): """Add a new node and its predecessors to the graph. Both the *node* and all elements in *predecessors* must be hashable. If called multiple times with the same node argument, the set of dependencies will be the union of all dependencies passed in. It is possible to add a node with no dependencies (*predecessors* is not provided) as well as provide a dependency twice. If a node that has not been provided before is included among *predecessors* it will be automatically added to the graph with no predecessors of its own. Raises ValueError if called after "prepare". """ if self._ready_nodes is not None: raise ValueError("Nodes cannot be added after a call to prepare()") # Create the node -> predecessor edges nodeinfo = self._get_nodeinfo(node) nodeinfo.npredecessors += len(predecessors) # Create the predecessor -> node edges for pred in predecessors: pred_info = self._get_nodeinfo(pred) pred_info.successors.append(node) def prepare(self): """Mark the graph as finished and check for cycles in the graph. If any cycle is detected, "CycleError" will be raised, but "get_ready" can still be used to obtain as many nodes as possible until cycles block more progress. After a call to this function, the graph cannot be modified and therefore no more nodes can be added using "add". """ if self._ready_nodes is not None: raise ValueError("cannot prepare() more than once") self._ready_nodes = [ i.node for i in self._node2info.values() if i.npredecessors == 0 ] # ready_nodes is set before we look for cycles on purpose: # if the user wants to catch the CycleError, that's fine, # they can continue using the instance to grab as many # nodes as possible before cycles block more progress cycle = self._find_cycle() if cycle: raise CycleError(f"nodes are in a cycle", cycle) def get_ready(self): """Return a tuple of all the nodes that are ready. Initially it returns all nodes with no predecessors; once those are marked as processed by calling "done", further calls will return all new nodes that have all their predecessors already processed. Once no more progress can be made, empty tuples are returned. Raises ValueError if called without calling "prepare" previously. """ if self._ready_nodes is None: raise ValueError("prepare() must be called first") # Get the nodes that are ready and mark them result = tuple(self._ready_nodes) n2i = self._node2info for node in result: n2i[node].npredecessors = _NODE_OUT # Clean the list of nodes that are ready and update # the counter of nodes that we have returned. self._ready_nodes.clear() self._npassedout += len(result) return result def is_active(self): """Return ``True`` if more progress can be made and ``False`` otherwise. Progress can be made if cycles do not block the resolution and either there are still nodes ready that haven't yet been returned by "get_ready" or the number of nodes marked "done" is less than the number that have been returned by "get_ready". Raises ValueError if called without calling "prepare" previously. """ if self._ready_nodes is None: raise ValueError("prepare() must be called first") return self._nfinished < self._npassedout or bool(self._ready_nodes) def __bool__(self): return self.is_active() def done(self, *nodes): """Marks a set of nodes returned by "get_ready" as processed. This method unblocks any successor of each node in *nodes* for being returned in the future by a call to "get_ready". Raises :exec:`ValueError` if any node in *nodes* has already been marked as processed by a previous call to this method, if a node was not added to the graph by using "add" or if called without calling "prepare" previously or if node has not yet been returned by "get_ready". """ if self._ready_nodes is None: raise ValueError("prepare() must be called first") n2i = self._node2info for node in nodes: # Check if we know about this node (it was added previously using add() if (nodeinfo := n2i.get(node)) is None: raise ValueError(f"node {node!r} was not added using add()") # If the node has not being returned (marked as ready) previously, inform the user. stat = nodeinfo.npredecessors if stat != _NODE_OUT: if stat >= 0: raise ValueError( f"node {node!r} was not passed out (still not ready)" ) elif stat == _NODE_DONE: raise ValueError(f"node {node!r} was already marked done") else: assert False, f"node {node!r}: unknown status {stat}" # Mark the node as processed nodeinfo.npredecessors = _NODE_DONE # Go to all the successors and reduce the number of predecessors, collecting all the ones # that are ready to be returned in the next get_ready() call. for successor in nodeinfo.successors: successor_info = n2i[successor] successor_info.npredecessors -= 1 if successor_info.npredecessors == 0: self._ready_nodes.append(successor) self._nfinished += 1 def _find_cycle(self): n2i = self._node2info stack = [] itstack = [] seen = set() node2stacki = {} for node in n2i: if node in seen: continue while True: if node in seen: # If we have seen already the node and is in the # current stack we have found a cycle. if node in node2stacki: return stack[node2stacki[node] :] + [node] # else go on to get next successor else: seen.add(node) itstack.append(iter(n2i[node].successors).__next__) node2stacki[node] = len(stack) stack.append(node) # Backtrack to the topmost stack entry with # at least another successor. while stack: try: node = itstack[-1]() break except StopIteration: del node2stacki[stack.pop()] itstack.pop() else: break return None def static_order(self): """Returns an iterable of nodes in a topological order. The particular order that is returned may depend on the specific order in which the items were inserted in the graph. Using this method does not require to call "prepare" or "done". If any cycle is detected, :exc:`CycleError` will be raised. """ self.prepare() while self.is_active(): node_group = self.get_ready() yield from node_group self.done(*node_group)