-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_green.py
89 lines (67 loc) · 2.39 KB
/
extract_green.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
def dump(o, fout):
json.dump(o, fout, separators=(',', ':'))
data = []
import glob, os
for filename in glob.glob("./data/green-virtual/*.json"):
with open(os.path.join(os.getcwd(), filename)) as fin:
jentry = json.load(fin)
entry = {}
entry['name'] = jentry['name']
entry['components'] = 1
entry['crossing_number'] = jentry['crossing_number']
entry['alexander'] = jentry['alex']
arrow = jentry['arrow']
for i in range(1,4):
entry[f'arrow{i}'] = arrow[i-1] if i-1 < len(arrow) else None
data.append(entry)
print("Loaded " + str(len(data)) + " data files")
print("Writing data files")
data.sort(key=lambda o: [int(n) for n in o['name'].split('.')])
# Lazy loader description file
f_defs = open("build/green.js", "w")
f_defs.write("// Generated by extract_green.py from Green database\n")
f_defs.write("// Descriptions of available data files\n")
def write_knots(filename, crossings, components, properties):
f_defs.write("provides_knot_data(")
dump(filename, f_defs)
f_defs.write(", 'green', ")
dump(components, f_defs)
f_defs.write(", ")
dump(crossings, f_defs)
f_defs.write(", ")
dump(properties, f_defs)
f_defs.write(");\n")
fout = open("build/" + filename, "w")
fout.write("// generated by extract_green.py from Green database\n")
fout.write("add_knot_data('green', ")
dump(properties, fout)
fout.write(",[\n")
first = True
for entry in data:
if entry['crossing_number'] in crossings and entry['components'] in components:
if not first:
fout.write(",\n")
first = False
dump(entry['name'], fout)
for p in properties:
fout.write(",")
dump(entry[p], fout)
fout.write("]);\n")
fout.write("loaded_knot_data('green', ")
dump(components, fout)
fout.write(", ")
dump(crossings, fout)
fout.write(", ")
dump(properties, fout)
fout.write(");\n")
fout.close()
properties = ['crossing_number', 'components',
'alexander',
'arrow1', 'arrow2']
write_knots("green_0-4.js", [0,1,2,3,4], [1], properties);
write_knots("green_5.js", [5], [1], properties);
write_knots("green_0-4_extra.js", [0,1,2,3,4], [1], ['arrow3']);
write_knots("green_5_extra.js", [5], [1], ['arrow3']);
f_defs.close()
print("Done")