import re
times_file = "/sdcard/SpeedCube/3 x 3 x 3 Cube.csv"
def read_file():
solves = []
try :
file = open(times_file)
for line in file :
match = re.search("(.+),(\d\d):(\d\d.\d\d)", line)
if match :
seconds = int(match.group(2)) * 60 + float(match.group(3))
# log is most recent to oldest, store in chronological order
solves.insert(0, (match.group(1), seconds))
pass
file.close();
except IOError :
print "Error reading file."
return []
return solves
# finds the average time of the solves given and returns it along with the start and end time of the set
def average(solves):
solveTimes = [x[1] for x in solves]
total = sum(solveTimes)
avg = total /solves.__len__()
return (avg, solves[0][0], solves[-1][0])
# drops the lowest and highest times, averages the rest, and returns the average with the start and end times
def middle_average(solves):
ordered = sorted(solves, key=lambda x: x[1])
avg = average(ordered[1:-1])[0]
return (avg, solves[0][0], solves[-1][0])
def main() :
solves = read_file()
#calculations
index = 1
total = 0 # for overall average
best = ()
avg5 = ()
of5 = ()
avg10 = ()
of12 = ()
for solve in solves :
total += solve[1]
if index == 1 or best[1] > solve[1]:
best = solve
if (index >= 5) :
avg5Temp = average(solves[index - 5:index])
if index == 5 or avg5Temp[0] < avg5[0] :
avg5 = avg5Temp
of5Temp = middle_average(solves[index - 5:index])
if index == 5 or of5Temp[0] < of5[0] :
of5 = of5Temp
if (index >= 10) :
avg10Temp = average(solves[index - 10:index])
if index == 10 or avg10Temp[0] < avg10[0] :
avg10 = avg10Temp
if (index >= 12) :
of12Temp = middle_average(solves[index - 12:index])
if index == 12 or of12Temp[0] < of12[0] :
of12 = of12Temp
index += 1
#output
if best == ():
print 'No times found.'
return
print ' Average : %.2f seconds' % (total / (index - 1))
print ' Best : %.2f seconds at %s' % (best[1], best[0])
if avg5 == ():
return
print 'Top Averages:'
print ' Avg. 5 : %.2f seconds from %s to %s' % (avg5[0], avg5[1], avg5[2])
print ' 3 of 5 : %.2f seconds from %s to %s' % (of5[0], of5[1], of5[2])
if avg10 == ():
return
print ' Avg. 10 : %.2f seconds from %s to %s' % (avg10[0], avg10[1], avg10[2])
if of12 == ():
return
print '10 of 12 : %.2f seconds from %s to %s' % (of12[0], of12[1], of12[2])
if __name__ == "__main__" :
main()