-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·97 lines (79 loc) · 2.59 KB
/
main.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
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
import getopt
import os
from statistics import stdev
from sys import argv
def usage():
print("Name")
print("\tCalculate the standard deviation for a file")
print("Flags")
print("-h, --help")
print("\tPrint help")
print("-r <rounds>, --rounds <rounds>")
print("\tSpecify the number of rounds")
print("-f <filename>, --file <filename>")
print("\tFilename - required")
print("Usage")
print("\t./main.py -f [filename]")
def calculate_std_read(file_name: str, n_rounds: int):
try:
with open(file_name) as file:
content = file.readlines()
except IOError:
print(f"Error when opening the file '{file_name}'")
exit(1)
new_file = []
for i in range(1, len(content), n_rounds + 4):
test = content[i - 1:i + n_rounds + 2]
times = [float(x.split(":")[1].rstrip()) for x in content[i:i + n_rounds]]
test.append(f"deviation:{'%.4f' % round(stdev(times), 4)}\n\n")
new_file.append(test)
try:
with open(file_name, "w") as file:
for item in new_file:
for x in item:
file.write(x)
except IOError:
print(f"Error when opening the file '{file_name}'")
exit(1)
os.system(f"sed -i '$d' {file_name}")
def calculate_std_mmap(file_name: str, n_rounds: int):
try:
with open(file_name, "r+") as file:
times = [float(next(file).split(":")[1].rstrip()) for _ in range(n_rounds)]
file.write(f"deviation:{'%.4f' % round(stdev(times), 4)}\n")
except IOError:
print(f"Error when opening the file '{file_name}'")
exit(1)
if __name__ == '__main__':
opts = []
try:
opts, _ = getopt.getopt(argv[1:], "hr:f:", ["help", "rounds=", "file="])
except getopt.GetoptError:
usage()
exit(1)
rounds = 10
filename = ""
if opts:
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
exit()
elif opt in ("-r", "--rounds"):
try:
rounds = int(arg)
except ValueError:
print("-r argument has to be a number")
exit(1)
elif opt in ("-f", "--file"):
filename = arg
else:
usage()
exit(1)
else:
usage()
exit(1)
if rounds < 1:
print("-r argument has to be greater than 1")
exit(1)
calculate_std_mmap(filename, rounds) if filename[0:4] == "mmap" else calculate_std_read(filename, rounds)