comparison env/lib/python3.9/site-packages/networkx/readwrite/gpickle.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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/3/library/pickle.html
22 """
23
24 __all__ = ["read_gpickle", "write_gpickle"]
25
26 from networkx.utils import open_file
27
28 import pickle
29
30
31 @open_file(1, mode="wb")
32 def write_gpickle(G, path, protocol=pickle.HIGHEST_PROTOCOL):
33 """Write graph in Python pickle format.
34
35 Pickles are a serialized byte stream of a Python object [1]_.
36 This format will preserve Python objects used as nodes or edges.
37
38 Parameters
39 ----------
40 G : graph
41 A NetworkX graph
42
43 path : file or string
44 File or filename to write.
45 Filenames ending in .gz or .bz2 will be compressed.
46
47 protocol : integer
48 Pickling protocol to use. Default value: ``pickle.HIGHEST_PROTOCOL``.
49
50 Examples
51 --------
52 >>> G = nx.path_graph(4)
53 >>> nx.write_gpickle(G, "test.gpickle")
54
55 References
56 ----------
57 .. [1] https://docs.python.org/3/library/pickle.html
58 """
59 pickle.dump(G, path, protocol)
60
61
62 @open_file(0, mode="rb")
63 def read_gpickle(path):
64 """Read graph object in Python pickle format.
65
66 Pickles are a serialized byte stream of a Python object [1]_.
67 This format will preserve Python objects used as nodes or edges.
68
69 Parameters
70 ----------
71 path : file or string
72 File or filename to write.
73 Filenames ending in .gz or .bz2 will be uncompressed.
74
75 Returns
76 -------
77 G : graph
78 A NetworkX graph
79
80 Examples
81 --------
82 >>> G = nx.path_graph(4)
83 >>> nx.write_gpickle(G, "test.gpickle")
84 >>> G = nx.read_gpickle("test.gpickle")
85
86 References
87 ----------
88 .. [1] https://docs.python.org/3/library/pickle.html
89 """
90 return pickle.load(path)