comparison env/lib/python3.9/site-packages/networkx/readwrite/nx_yaml.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 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
17 __all__ = ["read_yaml", "write_yaml"]
18
19 from networkx.utils import open_file
20
21
22 @open_file(1, mode="w")
23 def write_yaml(G_to_be_yaml, path_for_yaml_output, **kwds):
24 """Write graph G in YAML format to path.
25
26 YAML is a data serialization format designed for human readability
27 and interaction with scripting languages [1]_.
28
29 Parameters
30 ----------
31 G : graph
32 A NetworkX graph
33 path : file or string
34 File or filename to write.
35 Filenames ending in .gz or .bz2 will be compressed.
36
37 Notes
38 -----
39 To use encoding on the output file include e.g. `encoding='utf-8'`
40 in the keyword arguments.
41
42 Examples
43 --------
44 >>> G = nx.path_graph(4)
45 >>> nx.write_yaml(G, "test.yaml")
46
47 References
48 ----------
49 .. [1] http://www.yaml.org
50 """
51 try:
52 import yaml
53 except ImportError as e:
54 raise ImportError("write_yaml() requires PyYAML: http://pyyaml.org/") from e
55 yaml.dump(G_to_be_yaml, path_for_yaml_output, **kwds)
56
57
58 @open_file(0, mode="r")
59 def read_yaml(path):
60 """Read graph in YAML format from path.
61
62 YAML is a data serialization format designed for human readability
63 and interaction with scripting languages [1]_.
64
65 Parameters
66 ----------
67 path : file or string
68 File or filename to read. Filenames ending in .gz or .bz2
69 will be uncompressed.
70
71 Returns
72 -------
73 G : NetworkX graph
74
75 Examples
76 --------
77 >>> G = nx.path_graph(4)
78 >>> nx.write_yaml(G, "test.yaml")
79 >>> G = nx.read_yaml("test.yaml")
80
81 References
82 ----------
83 .. [1] http://www.yaml.org
84
85 """
86 try:
87 import yaml
88 except ImportError as e:
89 raise ImportError("read_yaml() requires PyYAML: http://pyyaml.org/") from e
90
91 G = yaml.load(path, Loader=yaml.FullLoader)
92 return G