예제 #1
0
 def test_edges(self):
     json_graph = {"label": "my graph", "graph": {"A": ["B"], "B": []}}
     graph = Graph(input_graph=json.dumps(json_graph))
     self.assertEqual(json_graph['label'], graph.get_label())
     self.assertEqual(False, graph.is_directed())
     self.assertEqual(json_graph['graph'], graph.get_graph())
     self.assertEqual([Edge('A', 'B')], graph.edges())
예제 #2
0
 def test_read_graph_from_file(self):
     json_graph = {"label": "my graph", "graph": {"A": ["B"], "B": []}}
     with open(path.join(self.test_dir, 'test.txt'), 'w') as out_file:
         out_file.write(json.dumps(json_graph))
     graph = Graph(input_file=str(path.join(self.test_dir, 'test.txt')))
     self.assertEqual(json_graph["label"], graph.get_label())
     self.assertEqual(False, graph.is_directed())
     self.assertEqual(json_graph["graph"], graph.get_graph())
예제 #3
0
def save_to_json(graph: Graph, out_dir):
    """

    :param graph: the graph to write to json
    :param out_dir: the absolute path to the dir to write the file
    :return:
    """
    g_dict = {
        "label": graph.get_label(),
        "directed": graph.is_directed(),
        "graph": graph.get_graph()
    }

    with open(path.join(out_dir, f"{graph.get_label()}.json"),
              'w',
              encoding="utf8") as out:
        out.write(json.dumps(g_dict))