Exemple #1
0
 def test_data_types(self):
     data = [
         True,
         False,
         10**20,
         -2e33,
         "'",
         '"&&&""',
         [{
             (b"\xfd", ): "\x7f",
             chr(0x4444): (1, 2)
         }, (2, "3")],
     ]
     data.append(chr(0x14444))
     data.append(literal_eval("{2.3j, 1 - 2.3j, ()}"))
     G = eg.Graph()
     G.name = data
     G.graph["data"] = data
     G.add_node(0, int=-1, data=dict(data=data))
     G.add_edge(0, 0, float=-2.5, data=data)
     gml = "\n".join(eg.generate_gml(G, stringizer=literal_stringizer))
     G = eg.parse_gml(gml, destringizer=literal_destringizer)
     assert data == G.name
     assert {"name": data, "data": data} == G.graph
     assert G.nodes == {0: dict(int=-1, data=dict(data=data))}
     assert list(G.edges) == [(0, 0, dict(float=-2.5, data=data))]
     G = eg.Graph()
     G.graph["data"] = "frozenset([1, 2, 3])"
     G = eg.parse_gml(eg.generate_gml(G), destringizer=literal_eval)
     assert G.graph["data"] == "frozenset([1, 2, 3])"
Exemple #2
0
    def test_label_kwarg(self):
        G = eg.parse_gml(self.simple_data, label="id")
        assert sorted(G.nodes) == [1, 2, 3]
        labels = [G.nodes[n]["label"] for n in sorted(G.nodes)]
        assert labels == ["Node 1", "Node 2", "Node 3"]

        G = eg.parse_gml(self.simple_data, label=None)
        assert sorted(G.nodes) == [1, 2, 3]
        labels = [G.nodes[n]["label"] for n in sorted(G.nodes)]
        assert labels == ["Node 1", "Node 2", "Node 3"]
Exemple #3
0
    def test_graph_types(self):
        for directed in [None, False, True]:
            for multigraph in [None, False, True]:
                gml = "graph ["
                if directed is not None:
                    gml += " directed " + str(int(directed))
                if multigraph is not None:
                    gml += " multigraph " + str(int(multigraph))
                gml += ' node [ id 0 label "0" ]'
                gml += " edge [ source 0 target 0 ]"
                gml += " ]"
                G = eg.parse_gml(gml)
                assert bool(directed) == G.is_directed()
                assert bool(multigraph) == G.is_multigraph()
                gml = "graph [\n"
                if directed is True:
                    gml += "  directed 1\n"
                if multigraph is True:
                    gml += "  multigraph 1\n"
                gml += """  node [
    id 0
    label "0"
  ]
  edge [
    source 0
    target 0
"""
                if multigraph:
                    gml += "    key 0\n"
                gml += "  ]\n]"
                assert gml == "\n".join(eg.generate_gml(G))
Exemple #4
0
    def test_parse_gml(self):
        G = eg.parse_gml(self.simple_data, label="label")
        assert sorted(G.nodes) == ["Node 1", "Node 2", "Node 3"]
        assert [e[:2] for e in sorted(G.edges)] == [
            ("Node 1", "Node 2"),
            ("Node 2", "Node 3"),
            ("Node 3", "Node 1"),
        ]

        assert [e for e in sorted(G.edges)] == [
            (
                "Node 1",
                "Node 2",
                {
                    "color": {
                        "line": "blue",
                        "thickness": 3
                    },
                    "label": "Edge from node 1 to node 2",
                },
            ),
            ("Node 2", "Node 3", {
                "label": "Edge from node 2 to node 3"
            }),
            ("Node 3", "Node 1", {
                "label": "Edge from node 3 to node 1"
            }),
        ]
Exemple #5
0
 def test_read_gml(self):
     (fd, fname) = tempfile.mkstemp()
     fh = open(fname, "w")
     fh.write(self.simple_data)
     fh.close()
     Gin = eg.read_gml(fname, label="label")
     G = eg.parse_gml(self.simple_data, label="label")
     assert sorted(G.nodes) == sorted(Gin.nodes)
     assert sorted(G.edges) == sorted(Gin.edges)
     os.close(fd)
     os.unlink(fname)
Exemple #6
0
    def test_escape_unescape(self):
        gml = """graph [
  name "&"䑄��&unknown;"
]"""
        G = eg.parse_gml(gml)
        assert ('&"\x0f' + chr(0x4444) +
                "��&unknown;" == G.name)
        gml = "\n".join(eg.generate_gml(G))
        alnu = "#1234567890;&#x1234567890abcdef"
        answer = ("""graph [
  name "&"䑄&""" + alnu + """;&unknown;"
]""")
        assert answer == gml
Exemple #7
0
    def test_parse_gml_cytoscape_bug(self):
        # example from issue #321, originally #324 in trac
        cytoscape_example = """
Creator "Cytoscape"
Version 1.0
graph   [
    node    [
        root_index  -3
        id  -3
        graphics    [
            x   -96.0
            y   -67.0
            w   40.0
            h   40.0
            fill    "#ff9999"
            type    "ellipse"
            outline "#666666"
            outline_width   1.5
        ]
        label   "node2"
    ]
    node    [
        root_index  -2
        id  -2
        graphics    [
            x   63.0
            y   37.0
            w   40.0
            h   40.0
            fill    "#ff9999"
            type    "ellipse"
            outline "#666666"
            outline_width   1.5
        ]
        label   "node1"
    ]
    node    [
        root_index  -1
        id  -1
        graphics    [
            x   -31.0
            y   -17.0
            w   40.0
            h   40.0
            fill    "#ff9999"
            type    "ellipse"
            outline "#666666"
            outline_width   1.5
        ]
        label   "node0"
    ]
    edge    [
        root_index  -2
        target  -2
        source  -1
        graphics    [
            width   1.5
            fill    "#0000ff"
            type    "line"
            Line    [
            ]
            source_arrow    0
            target_arrow    3
        ]
        label   "DirectedEdge"
    ]
    edge    [
        root_index  -1
        target  -1
        source  -3
        graphics    [
            width   1.5
            fill    "#0000ff"
            type    "line"
            Line    [
            ]
            source_arrow    0
            target_arrow    3
        ]
        label   "DirectedEdge"
    ]
]
"""
        eg.parse_gml(cytoscape_example)
Exemple #8
0
    def test_exceptions(self):
        pytest.raises(ValueError, literal_destringizer, "(")
        pytest.raises(ValueError, literal_destringizer, "frozenset([1, 2, 3])")
        pytest.raises(ValueError, literal_destringizer, literal_destringizer)
        pytest.raises(ValueError, literal_stringizer, frozenset([1, 2, 3]))
        pytest.raises(ValueError, literal_stringizer, literal_stringizer)
        with tempfile.TemporaryFile() as f:
            f.write(codecs.BOM_UTF8 + b"graph[]")
            f.seek(0)
            pytest.raises(eg.EasyGraphError, eg.read_gml, f)

        def assert_parse_error(gml):
            pytest.raises(eg.EasyGraphError, eg.parse_gml, gml)

        assert_parse_error(["graph [\n\n", "]"])
        assert_parse_error("")
        assert_parse_error('Creator ""')
        assert_parse_error("0")
        assert_parse_error("graph ]")
        assert_parse_error("graph [ 1 ]")
        assert_parse_error("graph [ 1.E+2 ]")
        assert_parse_error('graph [ "A" ]')
        assert_parse_error("graph [ ] graph ]")
        assert_parse_error("graph [ ] graph [ ]")
        assert_parse_error("graph [ data [1, 2, 3] ]")
        assert_parse_error("graph [ node [ ] ]")
        assert_parse_error("graph [ node [ id 0 ] ]")
        eg.parse_gml('graph [ node [ id "a" ] ]', label="id")
        assert_parse_error(
            "graph [ node [ id 0 label 0 ] node [ id 0 label 1 ] ]")
        assert_parse_error(
            "graph [ node [ id 0 label 0 ] node [ id 1 label 0 ] ]")
        assert_parse_error("graph [ node [ id 0 label 0 ] edge [ ] ]")
        assert_parse_error("graph [ node [ id 0 label 0 ] edge [ source 0 ] ]")
        eg.parse_gml(
            "graph [edge [ source 0 target 0 ] node [ id 0 label 0 ] ]")
        assert_parse_error(
            "graph [ node [ id 0 label 0 ] edge [ source 1 target 0 ] ]")
        assert_parse_error(
            "graph [ node [ id 0 label 0 ] edge [ source 0 target 1 ] ]")
        assert_parse_error(
            "graph [ node [ id 0 label 0 ] node [ id 1 label 1 ] "
            "edge [ source 0 target 1 ] edge [ source 1 target 0 ] ]")
        eg.parse_gml("graph [ node [ id 0 label 0 ] node [ id 1 label 1 ] "
                     "edge [ source 0 target 1 ] edge [ source 1 target 0 ] "
                     "directed 1 ]")
        eg.parse_gml("graph [ node [ id 0 label 0 ] node [ id 1 label 1 ] "
                     "edge [ source 0 target 1 ] edge [ source 0 target 1 ]"
                     "multigraph 1 ]")
        eg.parse_gml(
            "graph [ node [ id 0 label 0 ] node [ id 1 label 1 ] "
            "edge [ source 0 target 1 key 0 ] edge [ source 0 target 1 ]"
            "multigraph 1 ]")
        assert_parse_error(
            "graph [ node [ id 0 label 0 ] node [ id 1 label 1 ] "
            "edge [ source 0 target 1 key 0 ] edge [ source 0 target 1 key 0 ]"
            "multigraph 1 ]")
        eg.parse_gml(
            "graph [ node [ id 0 label 0 ] node [ id 1 label 1 ] "
            "edge [ source 0 target 1 key 0 ] edge [ source 1 target 0 key 0 ]"
            "directed 1 multigraph 1 ]")

        # Tests for string convertable alphanumeric id and label values
        eg.parse_gml(
            "graph [edge [ source a target a ] node [ id a label b ] ]")
        eg.parse_gml("graph [ node [ id n42 label 0 ] node [ id x43 label 1 ]"
                     "edge [ source n42 target x43 key 0 ]"
                     "edge [ source x43 target n42 key 0 ]"
                     "directed 1 multigraph 1 ]")
        assert_parse_error(
            "graph [edge [ source u'u\4200' target u'u\4200' ] " +
            "node [ id u'u\4200' label b ] ]")

        def assert_generate_error(*args, **kwargs):
            pytest.raises(eg.EasyGraphError,
                          lambda: list(eg.generate_gml(*args, **kwargs)))

        G = eg.Graph()
        G.graph[3] = 3
        assert_generate_error(G)
        G = eg.Graph()
        G.graph["3"] = 3
        assert_generate_error(G)
        G = eg.Graph()
        G.graph["data"] = frozenset([1, 2, 3])
        assert_generate_error(G, stringizer=literal_stringizer)
        G = eg.Graph()
        G.graph["data"] = []
        assert_generate_error(G)
        assert_generate_error(G, stringizer=len)
Exemple #9
0
 def test_name(self):
     G = eg.parse_gml('graph [ name "x" node [ id 0 label "x" ] ]')
     assert "x" == G.graph["name"]
     G = eg.parse_gml('graph [ node [ id 0 label "x" ] ]')
     assert "" == G.name
     assert "name" not in G.graph