Repository navigation
Expand file tree
/
Copy pathfile_comparer.py
More file actions
89 lines (68 loc) · 2.72 KB
/
Copy pathfile_comparer.py
File metadata and controls
89 lines (68 loc) · 2.72 KB
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
#!/usr/bin/env python3
# *_* coding: utf-8 *_*
"""
File comparer.
Checks output from the test program against a verified output.
The presence of all the lines from the verified output text file is checked in the test
executable output. This allows extra lines in the output due to debugging messages,
without causing the test to fail.
Numerical fields are treated differently by allowing slight variation in its value from the
verified one. This accomodates small variations due to external factors.
"""
__author__ = "Abhijit Kale"
import sys
from math import isclose
def check_if_num(word):
"""Check if the input is a number. Removes commas if any.
PARAMETERS
----------
word: str
Word to be checked.
RETURNS
-------
A tuple with a bool `True` and the number as float if converted. Otherwise, returns tuple with bool `False` and the original word.
"""
try:
num = float(word.replace(',', ''))
return (True, num)
except ValueError:
return (False, word)
def num_aware_line_compare(line1, line2, abs_tol=5.0, rel_tol=None):
"""Compares two lines. Numeric fields any are checked with tolerence
PARAMETERS
----------
line{1, 2}: str
Lines to be compared. Expects non-empty lines.
RETURNS
-------
Boolean indicatinf if the lines match.
"""
for (word1, word2) in zip(line1.split(), line2.split()):
r1 = check_if_num(word1)
r2 = check_if_num(word2)
if r1[0] and r2[0]:
if not isclose(r1[1], r2[1], abs_tol=abs_tol):
print(word1," != ", word2)
return False
else:
if word1 != word2:
print(word1," != ", word2)
return False
return True
if __name__ == "__main__":
# Get lines from files while removing empty ones. `num_aware_line_comp` function excpects no empty lines
with open(sys.argv[2], 'r') as verified_file, open(sys.argv[1], 'r') as out_file:
ver_lines = [line for line in verified_file.readlines() if line.strip()]
out_lines = [line for line in out_file.readlines() if line.strip()]
prev_out_line_num = -1
for ver_line_num in range(len(ver_lines)):
for out_line_num in range(prev_out_line_num + 1, len(out_lines)):
if num_aware_line_compare(ver_lines[ver_line_num], out_lines[out_line_num]):
prev_out_line_num = out_line_num
print(prev_out_line_num)
break
else: # Verified line not found in the output file.
print("Files do not match")
sys.exit(-1)
print("Files match")
sys.exit(0) # Files match.