view env/lib/python3.7/site-packages/networkx/utils/contextmanagers.py @ 3:758bc20232e8 draft

"planemo upload commit 2a0fe2cc28b09e101d37293e53e82f61762262ec"
author shellac
date Thu, 14 May 2020 16:20:52 -0400
parents 26e78fe6e8c4
children
line wrap: on
line source


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