Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / utils / contextmanagers.py @ 5cef0f13

History | View | Annotate | Download (632 Bytes)

1
from __future__ import absolute_import
2

    
3
from contextlib import contextmanager
4

    
5
__all__ = [
6
    'reversed',
7
]
8

    
9

    
10
@contextmanager
11
def reversed(G):
12
    """A context manager for temporarily reversing a directed graph in place.
13

14
    This is a no-op for undirected graphs.
15

16
    Parameters
17
    ----------
18
    G : graph
19
        A NetworkX graph.
20
    """
21
    directed = G.is_directed()
22
    if directed:
23
        G._pred, G._succ = G._succ, G._pred
24
        G._adj = G._succ
25

    
26
    try:
27
        yield
28
    finally:
29
        if directed:
30
            # Reverse the reverse.
31
            G._pred, G._succ = G._succ, G._pred
32
            G._adj = G._succ