diff env/lib/python3.7/site-packages/networkx/readwrite/gpickle.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/env/lib/python3.7/site-packages/networkx/readwrite/gpickle.py	Thu May 14 14:56:58 2020 -0400
@@ -0,0 +1,107 @@
+"""
+**************
+Pickled Graphs
+**************
+Read and write NetworkX graphs as Python pickles.
+
+"The pickle module implements a fundamental, but powerful algorithm
+for serializing and de-serializing a Python object
+structure. "Pickling" is the process whereby a Python object hierarchy
+is converted into a byte stream, and "unpickling" is the inverse
+operation, whereby a byte stream is converted back into an object
+hierarchy."
+
+Note that NetworkX graphs can contain any hashable Python object as
+node (not just integers and strings).  For arbitrary data types it may
+be difficult to represent the data as text.  In that case using Python
+pickles to store the graph data can be used.
+
+Format
+------
+See https://docs.python.org/2/library/pickle.html
+"""
+__author__ = """Aric Hagberg (hagberg@lanl.gov)\nDan Schult (dschult@colgate.edu)"""
+#    Copyright (C) 2004-2019 by
+#    Aric Hagberg <hagberg@lanl.gov>
+#    Dan Schult <dschult@colgate.edu>
+#    Pieter Swart <swart@lanl.gov>
+#    All rights reserved.
+#    BSD license.
+
+__all__ = ['read_gpickle', 'write_gpickle']
+
+import networkx as nx
+from networkx.utils import open_file
+
+try:
+    import cPickle as pickle
+except ImportError:
+    import pickle
+
+
+@open_file(1, mode='wb')
+def write_gpickle(G, path, protocol=pickle.HIGHEST_PROTOCOL):
+    """Write graph in Python pickle format.
+
+    Pickles are a serialized byte stream of a Python object [1]_.
+    This format will preserve Python objects used as nodes or edges.
+
+    Parameters
+    ----------
+    G : graph
+       A NetworkX graph
+
+    path : file or string
+       File or filename to write.
+       Filenames ending in .gz or .bz2 will be compressed.
+
+    protocol : integer
+        Pickling protocol to use. Default value: ``pickle.HIGHEST_PROTOCOL``.
+
+    Examples
+    --------
+    >>> G = nx.path_graph(4)
+    >>> nx.write_gpickle(G, "test.gpickle")
+
+    References
+    ----------
+    .. [1] https://docs.python.org/2/library/pickle.html
+    """
+    pickle.dump(G, path, protocol)
+
+
+@open_file(0, mode='rb')
+def read_gpickle(path):
+    """Read graph object in Python pickle format.
+
+    Pickles are a serialized byte stream of a Python object [1]_.
+    This format will preserve Python objects used as nodes or edges.
+
+    Parameters
+    ----------
+    path : file or string
+       File or filename to write.
+       Filenames ending in .gz or .bz2 will be uncompressed.
+
+    Returns
+    -------
+    G : graph
+       A NetworkX graph
+
+    Examples
+    --------
+    >>> G = nx.path_graph(4)
+    >>> nx.write_gpickle(G, "test.gpickle")
+    >>> G = nx.read_gpickle("test.gpickle")
+
+    References
+    ----------
+    .. [1] https://docs.python.org/2/library/pickle.html
+    """
+    return pickle.load(path)
+
+
+# fixture for pytest
+def teardown_module(module):
+    import os
+    os.unlink('test.gpickle')