-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_graph.py
38 lines (30 loc) · 940 Bytes
/
clean_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from pathlib import Path
from argparse import ArgumentParser
import re
def next_id_str(i):
# convert number to string, 1 = a, 2 = b, 26 = z, 27 = aa, 28 = ab, etc.
result = ""
while i > 0:
i = i - 1
result = chr(ord('a') + int((i % 26))) + result
i = i // 26
return result
def main(file):
i = 0
mapping = {}
graph = Path(file).read_text()
regex = re.compile(r"-?[0-9]{10,}")
for match in regex.finditer(graph):
node = match.group()
if not node in mapping.keys():
i = i + 1
mapping[node] = next_id_str(i)
graph = graph.replace(match.group(), mapping[node])
print(graph)
def parse_args():
parser = ArgumentParser()
parser.add_argument("file", help="graph file to process. Should be a graphviz dotfile")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
main(args.file)