Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / readwrite / gpickle.py @ 5cef0f13

History | View | Annotate | Download (2.75 KB)

1
"""
2
**************
3
Pickled Graphs
4
**************
5
Read and write NetworkX graphs as Python pickles.
6

7
"The pickle module implements a fundamental, but powerful algorithm
8
for serializing and de-serializing a Python object
9
structure. "Pickling" is the process whereby a Python object hierarchy
10
is converted into a byte stream, and "unpickling" is the inverse
11
operation, whereby a byte stream is converted back into an object
12
hierarchy."
13

14
Note that NetworkX graphs can contain any hashable Python object as
15
node (not just integers and strings).  For arbitrary data types it may
16
be difficult to represent the data as text.  In that case using Python
17
pickles to store the graph data can be used.
18

19
Format
20
------
21
See https://docs.python.org/2/library/pickle.html
22
"""
23
__author__ = """Aric Hagberg (hagberg@lanl.gov)\nDan Schult (dschult@colgate.edu)"""
24
#    Copyright (C) 2004-2019 by
25
#    Aric Hagberg <hagberg@lanl.gov>
26
#    Dan Schult <dschult@colgate.edu>
27
#    Pieter Swart <swart@lanl.gov>
28
#    All rights reserved.
29
#    BSD license.
30

    
31
__all__ = ['read_gpickle', 'write_gpickle']
32

    
33
import networkx as nx
34
from networkx.utils import open_file
35

    
36
try:
37
    import cPickle as pickle
38
except ImportError:
39
    import pickle
40

    
41

    
42
@open_file(1, mode='wb')
43
def write_gpickle(G, path, protocol=pickle.HIGHEST_PROTOCOL):
44
    """Write graph in Python pickle format.
45

46
    Pickles are a serialized byte stream of a Python object [1]_.
47
    This format will preserve Python objects used as nodes or edges.
48

49
    Parameters
50
    ----------
51
    G : graph
52
       A NetworkX graph
53

54
    path : file or string
55
       File or filename to write.
56
       Filenames ending in .gz or .bz2 will be compressed.
57

58
    protocol : integer
59
        Pickling protocol to use. Default value: ``pickle.HIGHEST_PROTOCOL``.
60

61
    Examples
62
    --------
63
    >>> G = nx.path_graph(4)
64
    >>> nx.write_gpickle(G, "test.gpickle")
65

66
    References
67
    ----------
68
    .. [1] https://docs.python.org/2/library/pickle.html
69
    """
70
    pickle.dump(G, path, protocol)
71

    
72

    
73
@open_file(0, mode='rb')
74
def read_gpickle(path):
75
    """Read graph object in Python pickle format.
76

77
    Pickles are a serialized byte stream of a Python object [1]_.
78
    This format will preserve Python objects used as nodes or edges.
79

80
    Parameters
81
    ----------
82
    path : file or string
83
       File or filename to write.
84
       Filenames ending in .gz or .bz2 will be uncompressed.
85

86
    Returns
87
    -------
88
    G : graph
89
       A NetworkX graph
90

91
    Examples
92
    --------
93
    >>> G = nx.path_graph(4)
94
    >>> nx.write_gpickle(G, "test.gpickle")
95
    >>> G = nx.read_gpickle("test.gpickle")
96

97
    References
98
    ----------
99
    .. [1] https://docs.python.org/2/library/pickle.html
100
    """
101
    return pickle.load(path)
102

    
103
# fixture for nose tests
104

    
105

    
106
def teardown_module(module):
107
    import os
108
    os.unlink('test.gpickle')