This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/networkx/utils/contextmanagers.py

32 lines
593 B
Python
Raw Normal View History

2020-07-30 01:16:18 +02:00
from contextlib import contextmanager
__all__ = [
'reversed',
]
@contextmanager
def reversed(G):
"""A context manager for temporarily reversing a directed graph in place.
This is a no-op for undirected graphs.
Parameters
----------
G : graph
A NetworkX graph.
"""
directed = G.is_directed()
if directed:
G._pred, G._succ = G._succ, G._pred
G._adj = G._succ
try:
yield
finally:
if directed:
# Reverse the reverse.
G._pred, G._succ = G._succ, G._pred
G._adj = G._succ