comparison planemo/lib/python3.7/site-packages/networkx/readwrite/nx_yaml.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """
2 ****
3 YAML
4 ****
5 Read and write NetworkX graphs in YAML format.
6
7 "YAML is a data serialization format designed for human readability
8 and interaction with scripting languages."
9 See http://www.yaml.org for documentation.
10
11 Format
12 ------
13 http://pyyaml.org/wiki/PyYAML
14
15 """
16 __author__ = """Aric Hagberg (hagberg@lanl.gov)"""
17 # Copyright (C) 2004-2019 by
18 # Aric Hagberg <hagberg@lanl.gov>
19 # Dan Schult <dschult@colgate.edu>
20 # Pieter Swart <swart@lanl.gov>
21 # All rights reserved.
22 # BSD license.
23
24 __all__ = ['read_yaml', 'write_yaml']
25
26 import networkx as nx
27 from networkx.utils import open_file
28
29
30 @open_file(1, mode='w')
31 def write_yaml(G_to_be_yaml, path_for_yaml_output, **kwds):
32 """Write graph G in YAML format to path.
33
34 YAML is a data serialization format designed for human readability
35 and interaction with scripting languages [1]_.
36
37 Parameters
38 ----------
39 G : graph
40 A NetworkX graph
41 path : file or string
42 File or filename to write.
43 Filenames ending in .gz or .bz2 will be compressed.
44
45 Notes
46 -----
47 To use encoding on the output file include e.g. `encoding='utf-8'`
48 in the keyword arguments.
49
50 Examples
51 --------
52 >>> G=nx.path_graph(4)
53 >>> nx.write_yaml(G,'test.yaml')
54
55 References
56 ----------
57 .. [1] http://www.yaml.org
58 """
59 try:
60 import yaml
61 except ImportError:
62 raise ImportError("write_yaml() requires PyYAML: http://pyyaml.org/")
63 yaml.dump(G_to_be_yaml, path_for_yaml_output, **kwds)
64
65
66 @open_file(0, mode='r')
67 def read_yaml(path):
68 """Read graph in YAML format from path.
69
70 YAML is a data serialization format designed for human readability
71 and interaction with scripting languages [1]_.
72
73 Parameters
74 ----------
75 path : file or string
76 File or filename to read. Filenames ending in .gz or .bz2
77 will be uncompressed.
78
79 Returns
80 -------
81 G : NetworkX graph
82
83 Examples
84 --------
85 >>> G=nx.path_graph(4)
86 >>> nx.write_yaml(G,'test.yaml')
87 >>> G=nx.read_yaml('test.yaml')
88
89 References
90 ----------
91 .. [1] http://www.yaml.org
92
93 """
94 try:
95 import yaml
96 except ImportError:
97 raise ImportError("read_yaml() requires PyYAML: http://pyyaml.org/")
98
99 G = yaml.load(path, Loader=yaml.FullLoader)
100 return G
101
102
103 # fixture for pytest
104 def setup_module(module):
105 try:
106 import yaml
107 except:
108 from pytest import skip
109 skip("PyYAML not available", allow_module_level=True)
110
111 # fixture for pytest
112 def teardown_module(module):
113 import os
114 os.unlink('test.yaml')