Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / subclass / plot_printgraph.py @ 5cef0f13

History | View | Annotate | Download (2.59 KB)

1
"""
2
===========
3
Print Graph
4
===========
5

6
Example subclass of the Graph class.
7
"""
8
# Author: Aric Hagberg (hagberg@lanl.gov)
9

    
10
#    Copyright (C) 2004-2019 by
11
#    Aric Hagberg <hagberg@lanl.gov>
12
#    Dan Schult <dschult@colgate.edu>
13
#    Pieter Swart <swart@lanl.gov>
14
#    All rights reserved.
15
#    BSD license.
16
#
17
__docformat__ = "restructuredtext en"
18

    
19
from copy import deepcopy
20

    
21
import matplotlib.pyplot as plt
22
import networkx as nx
23
from networkx import Graph
24

    
25
class PrintGraph(Graph):
26
    """
27
    Example subclass of the Graph class.
28

29
    Prints activity log to file or standard output.
30
    """
31

    
32
    def __init__(self, data=None, name='', file=None, **attr):
33
        Graph.__init__(self, data=data, name=name, **attr)
34
        if file is None:
35
            import sys
36
            self.fh = sys.stdout
37
        else:
38
            self.fh = open(file, 'w')
39

    
40
    def add_node(self, n, attr_dict=None, **attr):
41
        Graph.add_node(self, n, attr_dict=attr_dict, **attr)
42
        self.fh.write("Add node: %s\n" % n)
43

    
44
    def add_nodes_from(self, nodes, **attr):
45
        for n in nodes:
46
            self.add_node(n, **attr)
47

    
48
    def remove_node(self, n):
49
        Graph.remove_node(self, n)
50
        self.fh.write("Remove node: %s\n" % n)
51

    
52
    def remove_nodes_from(self, nodes):
53
        for n in nodes:
54
            self.remove_node(n)
55

    
56
    def add_edge(self, u, v, attr_dict=None, **attr):
57
        Graph.add_edge(self, u, v, attr_dict=attr_dict, **attr)
58
        self.fh.write("Add edge: %s-%s\n" % (u, v))
59

    
60
    def add_edges_from(self, ebunch, attr_dict=None, **attr):
61
        for e in ebunch:
62
            u, v = e[0:2]
63
            self.add_edge(u, v, attr_dict=attr_dict, **attr)
64

    
65
    def remove_edge(self, u, v):
66
        Graph.remove_edge(self, u, v)
67
        self.fh.write("Remove edge: %s-%s\n" % (u, v))
68

    
69
    def remove_edges_from(self, ebunch):
70
        for e in ebunch:
71
            u, v = e[0:2]
72
            self.remove_edge(u, v)
73

    
74
    def clear(self):
75
        Graph.clear(self)
76
        self.fh.write("Clear graph\n")
77

    
78

    
79
if __name__ == '__main__':
80
    G = PrintGraph()
81
    G.add_node('foo')
82
    G.add_nodes_from('bar', weight=8)
83
    G.remove_node('b')
84
    G.remove_nodes_from('ar')
85
    print("Nodes in G: ", G.nodes(data=True))
86
    G.add_edge(0, 1, weight=10)
87
    print("Edges in G: ", G.edges(data=True))
88
    G.remove_edge(0, 1)
89
    G.add_edges_from(zip(range(0, 3), range(1, 4)), weight=10)
90
    print("Edges in G: ", G.edges(data=True))
91
    G.remove_edges_from(zip(range(0, 3), range(1, 4)))
92
    print("Edges in G: ", G.edges(data=True))
93

    
94
    G = PrintGraph()
95
    nx.add_path(G, range(10))
96
    nx.add_star(G, range(9, 13))
97
    nx.draw(G)
98
    plt.show()