1 """
2 Read and write NetworkX graphs in YAML format.
3 See http://www.yaml.org for documentation.
4
5 """
6 __author__ = """Aric Hagberg (hagberg@lanl.gov)"""
7 __date__ = """"""
8 __credits__ = """"""
9 __revision__ = "$$"
10
11
12
13
14
15
16
17 import cPickle
18 import codecs
19 import locale
20 import string
21 import sys
22 import time
23
24 from networkx.utils import is_string_like, _get_fh
25 import networkx
26
27 -def write_yaml(G, path, default_flow_style=False, **kwds):
28 """Write graph G in YAML text format to path.
29
30 See http://www.yaml.org
31
32 """
33 try:
34 import yaml
35 except ImportError:
36 raise ImportError, \
37 "Import Error: not able to import yaml: http://www.yaml.org "
38 fh=_get_fh(path,mode='w')
39 yaml.dump(G,fh,default_flow_style=default_flow_style,**kwds)
40
41
43 """Read graph from YAML format from path.
44
45 See http://www.yaml.org
46
47 """
48 try:
49 import yaml
50 except ImportError:
51 raise ImportError, \
52 "Import Error: not able to import yaml: http://www.yaml.org "
53
54 fh=_get_fh(path,mode='r')
55 return yaml.load(fh)
56
57
58
60 import doctest
61 import yaml
62 suite = doctest.DocFileSuite('tests/readwrite/nx_yaml.txt',package='networkx')
63 return suite
64
65 if __name__ == "__main__":
66 import os
67 import sys
68 import unittest
69 if sys.version_info[:2] < (2, 4):
70 print "Python version 2.4 or later required for tests (%d.%d detected)." % sys.version_info[:2]
71 sys.exit(-1)
72
73 nxbase=sys.path[0]+os.sep+os.pardir
74 sys.path.insert(0,nxbase)
75 unittest.TextTestRunner().run(_test_suite())
76