Added graph store and read functionality

This commit is contained in:
Bugra Onal
2022-08-30 09:15:35 -07:00
parent d3753556c1
commit 05ab45f39b
4 changed files with 46 additions and 0 deletions
+30
View File
@@ -1,6 +1,7 @@
import copy
from collections import defaultdict
import debug
import json
class timing_graph():
@@ -139,6 +140,35 @@ class timing_graph():
return [self.edge_mods[(path[i], path[i+1])] for i in range(len(path)-1)]
def write(self, filename):
"""
Export graph to a JSON file
"""
# TODO: Find a proper way to store edge_mods values
with open(filename, 'w') as f:
f.write(
json.dumps(
{
'graph':
{key: list(val) for key, val in self.graph.items()},
'edge_mods':
{
', '.join(key): str(value)
for key, value in self.edge_mods.items()
}
}
)
)
def read(self, filename):
"""
Read graph from JSON file
"""
with open(filename, 'r') as f:
d = json.loads(f.read())
self.graph = {key: set(value) for key, value in d['graph'].items()}
self.edge_mods = {tuple(key.split(', ')): value for key, value in d['edge_mods'].items()}
def __str__(self):
""" override print function output """