|
4 | 4 | import numpy as np |
5 | 5 | import json as json |
6 | 6 |
|
7 | | - |
8 | | -def compare_files(fname_1, fname_2): |
9 | | - student = fname_1 |
10 | | - if isinstance(fname_1, str): |
11 | | - with open("Data/" + fname_1, "r") as fp: |
| 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: |
12 | 14 | student = json.load(fp) |
13 | 15 |
|
14 | | - check = fname_2 |
15 | | - if isinstance(fname_2, str): |
16 | | - with open("Data/" + fname_2, "r") as fp: |
17 | | - check = json.load(fp) |
| 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) |
18 | 21 |
|
19 | | - if isinstance(check, dict): |
20 | | - for k, v in check.items(): |
| 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(): |
21 | 26 | try: |
22 | 27 | if k == "Data channels": |
23 | 28 | # The data channels list |
24 | 29 | for i, d in enumerate(student[k]): |
25 | | - if not d == v[i]: |
26 | | - print(f"miss-match {d} {v[i]}") |
| 30 | + if d != v[i]: |
| 31 | + print(f"miss-match value {d} {v[i]}") |
27 | 32 | return False |
28 | 33 | else: |
29 | | - if not v == student[k]: |
| 34 | + if v != student[k]: |
30 | 35 | print(f"Miss-match key-item {k} {v} {student[k]}") |
31 | 36 | return False |
32 | 37 | except KeyError: |
33 | 38 | print(f"Missing key {k}") |
34 | 39 | return False |
35 | 40 | return True |
36 | 41 | else: |
37 | | - if len(check) != len(student): |
38 | | - print(f"Mismatched number of elements") |
| 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)}") |
39 | 45 | return False |
40 | 46 |
|
41 | | - for ce, cs in zip(check, student): |
| 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 |
42 | 50 | for k, v in ce.items(): |
43 | 51 | try: |
44 | | - if not v == cs[k]: |
45 | | - print(f"Miss-match key-item {k} {v} {cs[k]}") |
| 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}") |
46 | 55 | return False |
47 | 56 | except KeyError: |
48 | | - print(f"Missing key {k}") |
| 57 | + print(f"Missing key {k} or not a number {cs[k]}") |
49 | 58 | return False |
50 | 59 | return True |
51 | 60 |
|
|
0 commit comments