Skip to content

Commit ecf6f2f

Browse files
committed
Simplify JSON processing
1 parent af8462b commit ecf6f2f

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

ad.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222
JULIA_COMMAND = ["julia", "--color=yes", "--project=.", "main.jl"]
2323

24+
25+
def try_float(value):
26+
try:
27+
return float(value)
28+
except ValueError:
29+
return value
30+
31+
2432
def run_and_capture(command):
2533
"""Run a command and capture its output."""
2634
result = sp.run(command, text=True, check=True, stdout=sp.PIPE)
@@ -80,7 +88,7 @@ def run_ad(args):
8088
print(f"Running {model_key} with {adtype}...")
8189
try:
8290
output = run_and_capture([*RUN_JULIA_COMMAND, "--run", model_key, adtype])
83-
result = output.splitlines()[-1]
91+
result = try_float(output.splitlines()[-1])
8492
except sp.CalledProcessError as e:
8593
result = "error"
8694

@@ -116,13 +124,6 @@ def get_model_definition(model_key):
116124
return "\n".join(lines)
117125

118126

119-
def try_float(value):
120-
try:
121-
return float(value)
122-
except ValueError:
123-
return value
124-
125-
126127
def html(_args):
127128
web_dir = Path(__file__).parent / "web"
128129
json_output_dirs = [web_dir / "src" / "data", web_dir / "public"]
@@ -149,11 +150,7 @@ def html(_args):
149150
# We do some processing to turn it into a dict of dicts, then dump it
150151
# to the website
151152
results = json.loads(results)
152-
new_data = {}
153-
for entry in results:
154-
model_name = entry["model_name"]
155-
results = {k: try_float(v) for k, v in entry["results"].items()}
156-
new_data[model_name] = results
153+
new_data = {entry["model_name"]: entry["results"] for entry in results}
157154
for dir in json_output_dirs:
158155
with open(dir / "adtests.json", "w") as f:
159156
json.dump(new_data, f, indent=2)

0 commit comments

Comments
 (0)