1 """
2 Read and write NetworkX graphs.
3
4 Note that NetworkX graphs can contain any hashable Python object as
5 node (not just integers and strings). So writing a NetworkX graph
6 as a text file may not always be what you want: see write_gpickle
7 and gread_gpickle for that case.
8
9 This module provides the following :
10
11 Python pickled format:
12 Useful for graphs with non text representable data.
13
14 write_gpickle(G, path)
15 read_gpickle(path)
16
17 """
18 __author__ = """Aric Hagberg (hagberg@lanl.gov)\nDan Schult (dschult@colgate.edu)"""
19 __date__ = """"""
20 __credits__ = """"""
21 __revision__ = "$$"
22
23
24
25
26
27
28
29 import cPickle
30 import codecs
31 import locale
32 import string
33 import sys
34 import time
35
36 from networkx.utils import is_string_like,_get_fh
37 import networkx
38
40 """
41 Write graph object in Python pickle format.
42
43 This will preserve Python objects used as nodes or edges.
44
45 >>> write_gpickle(G,"file.gpickle")
46
47 See cPickle.
48
49 """
50 fh=_get_fh(path,mode='wb')
51 cPickle.dump(G,fh,cPickle.HIGHEST_PROTOCOL)
52
54 """
55 Read graph object in Python pickle format
56
57 >>> G=read_gpickle("file.gpickle")
58
59 See cPickle.
60
61 """
62 fh=_get_fh(path,'rb')
63 return cPickle.load(fh)
64
66 import doctest
67 suite = doctest.DocFileSuite('tests/readwrite/gpickle.txt',
68 package='networkx')
69 return suite
70
71
72 if __name__ == "__main__":
73 import os
74 import sys
75 import unittest
76 if sys.version_info[:2] < (2, 4):
77 print "Python version 2.4 or later required for tests (%d.%d detected)." % sys.version_info[:2]
78 sys.exit(-1)
79
80 nxbase=sys.path[0]+os.sep+os.pardir
81 sys.path.insert(0,nxbase)
82 unittest.TextTestRunner().run(_test_suite())
83