~ubuntu-branches/ubuntu/saucy/python-networkx/saucy

1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
1
"""
1.2.2 by Cyril Brulebois
Import upstream version 0.99
2
****
3
YAML
4
****
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
5
Read and write NetworkX graphs in YAML format.
1.2.4 by Sandro Tosi
Import upstream version 1.3
6
7
"YAML is a data serialization format designed for human readability 
8
and interaction with scripting languages."
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
9
See http://www.yaml.org for documentation.
10
1.2.4 by Sandro Tosi
Import upstream version 1.3
11
Format
12
------
13
http://pyyaml.org/wiki/PyYAML
14
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
15
"""
16
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
1.2.4 by Sandro Tosi
Import upstream version 1.3
17
#    Copyright (C) 2004-2010 by 
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
18
#    Aric Hagberg <hagberg@lanl.gov>
19
#    Dan Schult <dschult@colgate.edu>
20
#    Pieter Swart <swart@lanl.gov>
1.2.3 by Cyril Brulebois
Import upstream version 1.0~rc1~svn1492
21
#    All rights reserved.
22
#    BSD license.
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
23
1.2.2 by Cyril Brulebois
Import upstream version 0.99
24
__all__ = ['read_yaml', 'write_yaml']
25
1.2.4 by Sandro Tosi
Import upstream version 1.3
26
import networkx as nx
1.2.7 by Sandro Tosi
Import upstream version 1.6
27
from networkx.utils import open_file
1.2.4 by Sandro Tosi
Import upstream version 1.3
28
1.2.7 by Sandro Tosi
Import upstream version 1.6
29
@open_file(1,mode='w')
1.2.4 by Sandro Tosi
Import upstream version 1.3
30
def write_yaml(G, path, encoding='UTF-8', **kwds):
31
    """Write graph G in YAML format to path. 
32
33
    YAML is a data serialization format designed for human readability 
34
    and interaction with scripting languages [1]_.
35
36
    Parameters
37
    ----------
38
    G : graph
39
       A NetworkX graph
40
    path : file or string
41
       File or filename to write. 
42
       Filenames ending in .gz or .bz2 will be compressed.
43
    encoding: string, optional
44
       Specify which encoding to use when writing file.
45
46
    Examples
47
    --------
48
    >>> G=nx.path_graph(4)
49
    >>> nx.write_yaml(G,'test.yaml')
50
51
    References
52
    ----------
53
    .. [1] http://www.yaml.org
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
54
    """
55
    try:
56
        import yaml
57
    except ImportError:
1.2.4 by Sandro Tosi
Import upstream version 1.3
58
        raise ImportError("write_yaml() requires PyYAML: http://pyyaml.org/")
1.2.7 by Sandro Tosi
Import upstream version 1.6
59
    yaml.dump(G, path, **kwds)
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
60
    
1.2.7 by Sandro Tosi
Import upstream version 1.6
61
@open_file(0,mode='r')
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
62
def read_yaml(path):
1.2.4 by Sandro Tosi
Import upstream version 1.3
63
    """Read graph in YAML format from path.
64
65
    YAML is a data serialization format designed for human readability 
66
    and interaction with scripting languages [1]_.
67
68
    Parameters
69
    ----------
70
    path : file or string
71
       File or filename to read.  Filenames ending in .gz or .bz2 
72
       will be uncompressed.
73
74
    Returns
75
    -------
76
    G : NetworkX graph
77
78
    Examples
79
    --------
80
    >>> G=nx.path_graph(4)
81
    >>> nx.write_yaml(G,'test.yaml')
82
    >>> G=nx.read_yaml('test.yaml')
83
 
84
    References
85
    ----------
86
    .. [1] http://www.yaml.org
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
87
88
    """
89
    try:
90
        import yaml
91
    except ImportError:
1.2.4 by Sandro Tosi
Import upstream version 1.3
92
        raise ImportError("read_yaml() requires PyYAML: http://pyyaml.org/")
1.1.2 by Cyril Brulebois
Import upstream version 0.35.1.dfsg
93
1.2.7 by Sandro Tosi
Import upstream version 1.6
94
    G=yaml.load(path)
1.2.4 by Sandro Tosi
Import upstream version 1.3
95
    return G
96
97
98
# fixture for nose tests
99
def setup_module(module):
100
    from nose import SkipTest
101
    try:
102
        import yaml
103
    except:
104
        raise SkipTest("PyYAML not available")
1.2.5 by Sandro Tosi
Import upstream version 1.4
105
106
# fixture for nose tests
107
def teardown_module(module):
108
    import os
109
    os.unlink('test.yaml')