Skip to content

Commit 1397c75

Browse files
committed
missing file
1 parent 7e3677c commit 1397c75

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
# Just a helper function to check if two json files are the same
3+
4+
import numpy as np
5+
import json as json
6+
7+
# Compare dictionaries or data to files
8+
def compare_files(student_answer, soln_file_name):
9+
# Student is always the first argument
10+
student = student_answer
11+
# If it's a file name, then load that file
12+
if isinstance(student_answer, str):
13+
with open("Data/" + student_answer, "r") as fp:
14+
student = json.load(fp)
15+
16+
# Second filename is always the solution, stored in a file or a dictionary. Load it if it is a file.
17+
soln = soln_file_name
18+
if isinstance(soln_file_name, str):
19+
with open("Data/" + soln_file_name, "r") as fp:
20+
soln = json.load(fp)
21+
22+
# If soln is a dictionary, then do the first if statement
23+
if isinstance(soln, dict):
24+
# Loop through all elements of the dictionary
25+
for k, v in soln.items():
26+
try:
27+
if k == "Data channels":
28+
# The data channels list
29+
for i, d in enumerate(student[k]):
30+
if d != v[i]:
31+
print(f"miss-match value {d} {v[i]}")
32+
return False
33+
else:
34+
if v != student[k]:
35+
print(f"Miss-match key-item {k} {v} {student[k]}")
36+
return False
37+
except KeyError:
38+
print(f"Missing key {k}")
39+
return False
40+
return True
41+
else:
42+
# soln is a list.
43+
if len(soln) != len(student): # Check lists are same length
44+
print(f"Mismatched number of elements, solution {len(soln)} yours {len(student)}")
45+
return False
46+
47+
# For each item in each list
48+
for ce, cs in zip(soln, student):
49+
# Each item is a dictionary - loop through the solution keys
50+
for k, v in ce.items():
51+
try:
52+
# Check if the values in the keys are similar
53+
if not np.isclose(v, cs[k], atol=0.01):
54+
print(f"Value for key {k} is {cs[k]}, expected {v}")
55+
return False
56+
except KeyError:
57+
print(f"Missing key {k} or not a number {cs[k]}")
58+
return False
59+
return True
60+
61+

0 commit comments

Comments
 (0)