-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeResults.py
175 lines (142 loc) · 6.38 KB
/
TimeResults.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Author: Thomas Brown
# CSS 527 Cryptography Final Project
# Records the time results for utilizing a cryptographic function to encrypt/
# decrypt a message file.
# Section for allowing import of custom classes
import sys
import os
if sys.path[0] != os.path.dirname(os.path.realpath(sys.argv[0])):
sys.path.insert(0, os.path.dirname(os.path.realpath(sys.argv[0])))
######################################
# IMPORTS #
######################################
from BinaryFileHandler import OpenAppendByteDataFile
class TimeResults:
def __init__(self, algorithm_used_str, surrounding_library_str):
# Algorithm used
self.algorithmUsed = algorithm_used_str
# Surrounding library
self.surroundingLibrary = surrounding_library_str
# Time taken
self.secondsTaken = []
# Process iteration number
self.nRuns = 0
def __len__(self):
return self.nRuns
def __str__(self):
'''String representation of the time results. Displays algorithm used,
the encapsulating library, the time taken in microseconds, and the
process iteration number.'''
string = "Algorithm: " + str(self.algorithmUsed) \
+ os.linesep + "Library used: " + str(self.surroundingLibrary) \
+ os.linesep + "Time taken (seconds): " + os.linesep + "\t" + str(self.secondsTaken) \
+ os.linesep + "Number of runs tracked: " + str(self.nRuns)
return string
def __repr__(self):
return str(self)
def append(self, seconds_taken):
'''Appends a time result that has the same algorithm used and surrounding
libraries.'''
originalLen = len(self.secondsTaken)
self.secondsTaken.append(seconds_taken)
self.nRuns += 1
return len(self.secondsTaken) == originalLen + 1
def Average(self):
'''Method designed to find the average of recorded time results.'''
if self.nRuns < 1:
return 0
avgSum = 0
for result in self.secondsTaken:
avgSum += result
return avgSum/self.nRuns
def AverageReadable(self):
'''Creates a readable string representing the average of the time
results passed to it. Returns a utf-8 encoded string representing the
average time in seconds as well as the number of time results passed into it.'''
# Create result string
string = "For entries:"
for timeEntry in self.secondsTaken:
string += os.linesep + "\t" + str(timeEntry) + " seconds"
# Append average
string += os.linesep + os.linesep + "Average = " + str(self.Average()) + " seconds (" + str(self.nRuns) + " time entries)"
return string.encode("utf-8")
def AppendToFile(self, filename, n_line_breaks=0):
'''Appends user-friendly description of this time result object to
a specified text file. If line breaks are desired, they will be
appended to the file before the data.'''
# Guard conditions
if n_line_breaks < 0:
n_line_breaks = 0
# Open binary file in append mode
filehandler = OpenAppendByteDataFile(filename)
# If line breaks are desired, append them before the data
lineBreak = tuple(os.linesep)
if n_line_breaks > 0:
lineBreakBytes = bytearray()
for i in range(n_line_breaks):
lineBreakBytes.extend(ord(char) for char in lineBreak)
filehandler.write(lineBreakBytes)
# Append byte format of string representation of self to file
filehandler.write(bytes(str(self).encode("utf-8")))
# Insert a newline to prepare for readable format considering future
# data appending
filehandler.write(bytearray(ord(char) for char in lineBreak))
# Close file
filehandler.close()
def AppendAverageToFile(self, filename, n_line_breaks=0):
'''Appends user-friendly description of a time result average to
a specified text file.'''
# Guard conditions
if n_line_breaks < 0:
n_line_breaks = 0
# Open binary file in append mode
filehandler = OpenAppendByteDataFile(filename)
# If line breaks are desired, append them before the data
lineBreak = tuple(os.linesep)
if n_line_breaks > 0:
lineBreakBytes = bytearray()
for i in range(n_line_breaks):
lineBreakBytes.extend(ord(char) for char in lineBreak)
filehandler.write(lineBreakBytes)
# Append byte format of string representation of average to file
filehandler.write(bytes(self.AverageReadable()))
# Insert a newline to prepare for readable format considering future
# data appending
filehandler.write(bytearray(ord(char) for char in lineBreak))
# Close file
filehandler.close()
######################################
# MAIN #
######################################
if __name__ == "__main__":
print("Running main for TimeResults module.")
print("Module found in " + str(os.path.dirname(os.path.realpath(sys.argv[0]))))
print()
algorithmUsed = "SHA256"
surroundingLibrary = "hashlib"
secondsTaken = 6.2903e-7
print("Testing module with:" \
+ os.linesep + "\t" + str(algorithmUsed) + " algorithm" \
+ os.linesep + "\t" + str(surroundingLibrary) + " library")
print()
myResults = TimeResults(algorithmUsed, surroundingLibrary)
print("Time results object displays as:")
print(str(myResults))
print()
filename = "TimeResultTest.txt"
print("Testing writing of file to: " + str(filename))
myResults.AppendToFile(filename)
print("Testing appending of Time result object with 2 breaklines to: " + str(filename))
myResults.AppendToFile(filename, 2)
print()
print("Testing appendage of time result (" + str(secondsTaken) + ")")
myResults.append(secondsTaken)
print(str(myResults))
print("Appending change to file (" + str(filename) + ").")
myResults.AppendToFile(filename, 1)
print()
print("Testing method Average with Time result object: ")
print(str(myResults.Average))
print("Testing appendage of average's information to file (" + str(filename) + ")")
myResults.AppendAverageToFile(filename, 1)
print()